snippets

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