line

Drag and Release

Monday, November 24th, 2008 | examples | Comments

Sproing! I’ll go into “camera.unproject” and “rays” in more detail in the future.


source

package
{
	import flash.events.Event;
	import flash.events.MouseEvent;
 
	import gs.TweenMax;
	import gs.easing.Elastic;
 
	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.core.geom.renderables.Line3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class DragAndRelease extends BasicView
	{
		private var sphere:Sphere;
		private var line:Line3D;
		private const LENGTH:Number = 2000;
 
		private var isSpherePressed:Boolean = false;
 
		public function DragAndRelease()
		{
			viewport.interactive = true;
			camera.fov = 110;
			var light:PointLight3D = new PointLight3D();
			light.x = -1000, light.y = 1000;
 
 
			var material:FlatShadeMaterial = new FlatShadeMaterial(light, 0xcc0000);
			material.interactive = true;
			sphere = new Sphere(material, 200, 12, 12);
 
			sphere.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, objectPressHandler);
			sphere.z = LENGTH + camera.z;
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
 
			var lineMaterial:LineMaterial = new LineMaterial(0x00cc00);
			var lines:Lines3D = new Lines3D();
			line = new Line3D(lines, lineMaterial, 1, new Vertex3D(1000,1000,1000), new Vertex3D(-1000,0,0));
			lines.addLine(line);
 
			scene.addChild(lines);
			scene.addChild(sphere);
 
			startRendering();
		}
 
		override protected function onRenderTick(event:Event=null):void
		{
			var ray:Number3D = camera.unproject(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY);
			line.v0.x = ray.x, line.v0.y = ray.y, line.v0.z = ray.z;
			line.v1.x = sphere.x, line.v1.y = sphere.y, line.v1.z = sphere.z;
 
			ray.normalize();
			ray.multiplyEq(LENGTH);
			ray = Number3D.add(ray, new Number3D(camera.x, camera.y, camera.z));
 
			if(isSpherePressed)
			{
				sphere.position = ray;
			}
 
			renderer.renderScene(scene, camera, viewport);
		}
 
		private function objectPressHandler(event:InteractiveScene3DEvent):void
		{
			TweenMax.killTweensOf(sphere);
			isSpherePressed = true;
		}
 
		private function mouseUpHandler(event:MouseEvent):void
		{
			isSpherePressed = false;
			TweenMax.to(sphere, 1, {x:0, y:0, z:LENGTH + camera.z, ease:Elastic.easeOut});
		}
	}
}

Tags: , , , ,

Following line

Friday, November 21st, 2008 | examples, requests | Comments

Line3Ds have a starting Vertex3D and an ending Vertex3D. You can simply update those vertices using the following properties to move the line around:

//starting
line.v0.x = 0;
line.v0.x = 0;
line.v0.x = 0;
//ending
line.v1.x = 100;
line.v1.x = 100;
line.v1.x = 100;


source

package
{
	import gs.TweenMax;
 
	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.core.geom.renderables.Line3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.view.BasicView;
	import org.papervision3d.view.stats.StatsView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class LinesBetweenTwo3DPoints extends BasicView
	{
		private var sphere1:Sphere;
		private var sphere2:Sphere;
		private var line:Line3D;
		private var startVertex3D:Vertex3D;
		private var endVertex3D:Vertex3D;
 
		public function LinesBetweenTwo3DPoints()
		{
			var stats:StatsView = new StatsView(renderer);
			stats.x = 440;
			addChild(stats);
 
			var material:WireframeMaterial = new WireframeMaterial(0xcc0000);
			sphere1 = new Sphere(material, 50);
			sphere2 = new Sphere(material, 50);
			sphere2.x = 500;
			sphere2.y = 500;
 
			var lines:Lines3D = new Lines3D();
 
			startVertex3D = new Vertex3D(sphere1.x, sphere1.y, sphere1.z);
			endVertex3D = new Vertex3D(sphere2.x, sphere2.y, sphere2.z);
			var lineMaterial:LineMaterial = new LineMaterial(0x00cc00);
			line = new Line3D(lines, lineMaterial, 4, startVertex3D, endVertex3D);
 
			lines.addLine(line);
 
			scene.addChild(sphere1);
			scene.addChild(sphere2);
			scene.addChild(lines);
 
			startRendering();
 
			//Tween stuff
			var bezierThrough:Array = []; 
			for(var i:int = 0; i < 10; i++)
			{
				var bezierPoint:Object = {};
				bezierPoint.x = Math.random() * 1000 - 500;
				bezierPoint.y = Math.random() * 1000 - 500;
				bezierPoint.z = Math.random() * 500;
				bezierThrough.push(bezierPoint);
			}
 
			TweenMax.to(sphere2, 10, {x:500, y:500, z:0, bezierThrough:bezierThrough, loop:true, onUpdate:tweenUpdate});
		}
 
		private function tweenUpdate():void
		{
			sphere1.lookAt(sphere2); //just for added efffect :)
			//v1 is the end point, so have it follow sphere2
			line.v1.x = sphere2.x;
			line.v1.y = sphere2.y;
			line.v1.z = sphere2.z;
		}
	}
}

Tags:

Vectors show length and direction

Tuesday, November 18th, 2008 | examples | Comments

Vectors do not have a location. That means you can start them from anywhere and they’ll still travel the same length and direction regardless. The following example demonstrates by using random starting points, but placing planes and lines along the same vector from those different starting points.

View example
source

package {
 
	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.core.geom.renderables.Line3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
	import org.papervision3d.core.math.Number3D;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class VectorsShowLengthAndDirection extends BasicView
	{
		private const NUM_POINTS:int = 50;
		private const NUM_STARTING_POINTS:int = 20;
 
		public function VectorsShowLengthAndDirection()
		{
			var pointA:Number3D = new Number3D(-1000, -800, 500);
			var pointB:Number3D = new Number3D(12000, 8000, 20000);
 
			var vector:Number3D = Number3D.sub(pointB, pointA);
			var vectorCopy:Number3D = new Number3D();
 
			for(var i:int = 0; i < NUM_STARTING_POINTS; i++)
			{
				//create a random starting point
				var startingPoint:Number3D = new Number3D(Math.random() * 2000 - 1000, Math.random() * 2000 - 1000, Math.random() * 2000 );
				var randColor:Number = Math.random() * 0xffffff;
				var lines:Lines3D = new Lines3D();
				var lineMaterial:LineMaterial = new LineMaterial(randColor);
				for(var j:int = 0; j < NUM_POINTS; j++)
				{
					vectorCopy.copyFrom(vector);
					var pointAlongVector:DisplayObject3D = new Plane(new ColorMaterial(randColor), 100, 100);
					vectorCopy.multiplyEq(j/NUM_POINTS);
					//use the startingPoint as the first position
					pointAlongVector.position = Number3D.add(startingPoint, vectorCopy);
					scene.addChild(pointAlongVector);
				}	
				var line:Line3D = new Line3D(lines, lineMaterial, 5, 
					new Vertex3D(startingPoint.x, startingPoint.y, startingPoint.z),
					new Vertex3D(pointAlongVector.x, pointAlongVector.y, pointAlongVector.z));
 
				lines.addLine(line);
				scene.addChild(lines);
			}
 
			singleRender();
		}
	}
}

Tags: , ,

Points along a displacement vector – Part 2

Tuesday, November 18th, 2008 | snippets | Comments

While the last example was supposed to be simple to read and understand, this shows how to do the same thing with much less code by using the built-in vector methods:

Number3D.add(vectorA, vectorB); //adds two vectors
//which is much faster than writing
vector = vectorA.x + vectorB.x;
vector = vectorA.y + vectorB.y;
vector = vectorA.z + vectorB.z;
 
Number3D.sub(vectorA, vectorB); //subtracts two vectors
 
vector.multiplyEq(number); //multiplies the x, y, and z by the given number

Look at the following code and compare it to the first example to try and match up what’s happening.

private const NUM_POINTS:int = 150;
 
public function PointsAlongAVector()
{
	var pointA:Number3D = new Number3D(-1000, -800, 500);
	var pointB:Number3D = new Number3D(12000, 8000, 20000);
 
	var vector:Number3D = Number3D.sub(pointB, pointA);
	var vectorCopy:Number3D = new Number3D();
 
	for(var i:int = 0; i < NUM_POINTS; i++)
	{
		vectorCopy.copyFrom(vector);
		var pointAlongVector:DisplayObject3D = new Plane(null, 10, 10);
		vectorCopy.multiplyEq(i/NUM_POINTS);
		pointAlongVector.position = Number3D.add(pointA, vectorCopy);
 
		scene.addChild(pointAlongVector);
	}
 
	singleRender();
}

*note – you will need the latest version of Papervision3D to be able to use the “position” property of a DisplayObject3D.

Tags: , ,

Search

Recommended Books

Speaking at FITC Toronto

 

May 2012
M T W T F S S
« May    
 123456
78910111213
14151617181920
21222324252627
28293031  

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 ...