Archive for November, 2008

Dancing Spheres

Wednesday, November 26th, 2008 | examples | Comments

This was supposed to be something completely different :)




source

package
{
	import flash.events.Event;
 
	import org.papervision3d.core.effects.BitmapColorEffect;
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.view.layer.BitmapEffectLayer;
	import org.papervision3d.view.layer.ViewportLayer;
 
	[SWF(width="800", height="600", backgroundColor="#000000", frameRate="60")]
	public class DancingSpheres extends BasicView
	{
		private const LENGTH:Number = 2000;
		private var yellowSphere:Sphere;
 
 
		public function DancingSpheres()
		{
			super(800, 600);			
			camera.fov = 140;
 
			var light:PointLight3D = new PointLight3D();
			light.x = -1000, light.y = 1000;
 
			var yellowMaterial:FlatShadeMaterial = new FlatShadeMaterial(light, 0xcccc00);
			yellowSphere = new Sphere(yellowMaterial, 120);
 
			var redMaterial:FlatShadeMaterial = new FlatShadeMaterial(light, 0xcc0000);
			var redSphere:Sphere = new Sphere(redMaterial, 120);
			redSphere.x = -400;
 
			yellowSphere.addChild(redSphere);
 
			var bitmapEffectLayer:BitmapEffectLayer = new BitmapEffectLayer(viewport, 800, 600);
			bitmapEffectLayer.addDisplayObject3D(yellowSphere);
			bitmapEffectLayer.addDisplayObject3D(redSphere);
			bitmapEffectLayer.addEffect(new BitmapColorEffect());
			viewport.containerSprite.addLayer(bitmapEffectLayer);
 
			scene.addChild(yellowSphere);
 
			startRendering();
		}
 
		override protected function onRenderTick(event:Event=null):void
		{
			var ray:Number3D = camera.unproject(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY);
			ray.normalize();
			ray.multiplyEq(LENGTH);
			ray = Number3D.add(ray, new Number3D(camera.x, camera.y, camera.z));
 
			var velocityX:Number = (ray.x - yellowSphere.x) * .1; 
			var velocityY:Number = (ray.y - yellowSphere.y) * .1; 
			var velocityZ:Number = (ray.z - yellowSphere.z) * .1; 
 
			yellowSphere.x += velocityX;
			yellowSphere.y += velocityY;
			yellowSphere.z += velocityZ;
 
			if(velocityX > 20) velocityX = 20;
			if(velocityX < -20) velocityX = -20;
 
			yellowSphere.roll(velocityX);
 
			renderer.renderScene(scene, camera, viewport);
		}
	}
}

Tags:

The State Pattern – source files

Wednesday, November 26th, 2008 | examples | Comments

I’m planning on doing a video tutorial on using the state pattern for better control over the state of your flash app. I put together what I’ll most likely use as the source files and I thought I’d share them here in case I don’t get around to making the video:

Part 1 – A typical “Document Class” approach to changing state
Focus on how it’s calling functions like “setStateAsWall()” to change state. Also notice how the .swf simply allows you to move from state to state.
View example
StatePattern – Part 1.zip

Part 2 – Adding the power of the State Pattern
Focus on how state is now changing using state.setStateAsWall(), then look into the individual State classes to set how they’re managing the state. In the .swf you’ll see you now how more control of how states move from state to state.
View example
StatePattern – Part 2.zip

Part 3 – Enforcing with an Interface
Focus on the interface “IState” (it’s like a class, but instead lists functions that any class that uses it has to have). Also notice that the State classes “implement” IState to enforce that they have the same functions. In the main class, you’ll see that they states are now typed as “IState”.
No change visually from part 2
StatePattern – Part 3.zip

I hope I get around to explaining this more later, but I thought I would push this up here before I go eat a bunch of turkey for Thanksgiving.

Tags: ,

Mouse follower

Tuesday, November 25th, 2008 | examples | Comments

Saw this question come up on the mailing list. I thought it would make a nice demo:


source

package
{
	import flash.events.Event;
 
	import org.papervision3d.core.utils.Mouse3D;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.view.layer.ViewportLayer;
	import org.papervision3d.view.layer.util.ViewportLayerSortMode;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class Mouse3DFloor extends BasicView
	{
		private var mouse3D:Mouse3D;
		private var follower:Plane;
 
		public function Mouse3DFloor() 
		{
			viewport.interactive = true;
			Mouse3D.enabled = true;
			mouse3D = viewport.interactiveSceneManager.mouse3D;
 
			var redColorMaterial:ColorMaterial = new ColorMaterial(0xcc0000);
			redColorMaterial.interactive = true; 
			//10, 10 represent the number of segments in the plane
			var floor:Plane = new Plane(redColorMaterial, 2000, 2000, 10, 10);			
			floor.rotationX = 90;
 
			var greenColorMaterial:ColorMaterial = new ColorMaterial(0x00cc00);
			follower = new Plane(greenColorMaterial, 300, 300);
			follower.rotationX = 90;			
			scene.addChild(floor);			
			scene.addChild(follower);
 
			camera.y = 1000;
			camera.lookAt(floor);
 
			var floorViewportLayer:ViewportLayer = new ViewportLayer(viewport, floor);
			var followerViewportLayer:ViewportLayer = new ViewportLayer(viewport, follower);
 
			viewport.containerSprite.sortMode = ViewportLayerSortMode.INDEX_SORT;	
			floorViewportLayer.layerIndex = 0;
			followerViewportLayer.layerIndex = 1;
 
			viewport.containerSprite.addLayer(floorViewportLayer);
			viewport.containerSprite.addLayer(followerViewportLayer);
 
 
			startRendering();
		}
 
		override protected function onRenderTick(event:Event=null):void
		{
			follower.x += (mouse3D.x - follower.x) * .1;
			follower.y += (mouse3D.y - follower.y) * .1;
			follower.z += (mouse3D.z - follower.z) * .1;
			renderer.renderScene(scene, camera, viewport);
		}
 
	}
 
}

Tags: ,

Understanding Papervision3D: Matrix3D

Monday, November 24th, 2008 | how it works | Comments

This post will cover how you use a Matrix3D to scale, pitch, yaw, roll, and translate 3d objects:

Every 3d object within Papervision3D has a Matrix3D that defines its scale, pitch, yaw, roll, x, y, and z (changing x, y, and z is referred to as “translation” because they move a group of points equally). You will find the Matrix3D in the “transform” property of your 3d object.

The default 4 column by 4 row Matrix 3d with no scale, yaw, pitch, roll, or translation is set up as follows:

1, 0, 0, 0
0, 1, 0, 0
0, 0, 1, 0
0, 0, 0, 1

These values are stored in the “transform” property of your 3d object using a “n”, a integer defining the row, and an integer for the column:

n11, n12, n13, n14
n21, n22, n23, n24
n31, n32, n33, n34
n41, n42, n43, n44

For example: cube.transform.n11

Scale, pitch, yaw, and roll are handled in the first 3 columns and rows of the matrix (play with the explorer above to figure out how it works):

1, 0, 0
0, 1, 0
0, 0, 1

x, y, and z are all handled first 3 numbers in the last column:

0
0
0

The last row hangs around to make Matrix multiplication possible. Have fun playing with the explorer to get a hang of how 3d objects are moved around. I hope to cover some of the Matrix3D math functions in the future.

Tags:

Search

Recommended Books

Speaking at FITC Toronto

 

November 2008
M T W T F S S
    Dec »
 12
3456789
10111213141516
17181920212223
24252627282930

Preferred Video Tutorial Resolution

  • 1024x768 (53%, 85 Votes)
  • 1280x1024 (15%, 24 Votes)
  • 1920x1080 (15%, 24 Votes)
  • 800x600 (13%, 20 Votes)
  • 480x320 (4%, 6 Votes)
  • 640x480 (0%, 2 Votes)

Total Voters: 160

Loading ... Loading ...