Animating with Vector3D Class

Move your mouse over the word to see the effect.

I have been getting back into OpenFrameworks lately and I have been using the OF vector library for all of the recent animations that I have been doing. I thought that I should give the AS3 Vector3D library a go. There are some limited, but useful functions built in. Making the move over to vector animation has made my coded animations more streamlined, if you are thinking about making the switch, consider the following code samples :


Without Vectors


var locX = 0, locY = 0;
var loc2X = 100;
var loc2Y = 50;

var dx = loc2X – locX;
var dy = loc2Y – locY;
var dist = Math.sqrt( (dx * dx) + (dy * dy) );


Utilizing Vectors


var loc:Vector3D = new Vector3D();
var loc2:Vector3D = new Vector3D(100, 50);

var dist2 = (loc2.subtract( loc )).length;

// or //
var dist3 = Vector3D.distance( loc, loc2 );

Both of the approaches yield the same result. But you can see that the vector approach will be far easier to manage when more variables and more complex operations are involved. A quick example of adding some values together.

var xLoc = 30;
var yLoc = 15;
var zLoc = 20;
var xLoc2 = 2;
var yLoc2 = 12;
var zLoc2 = 0;

// add the values together //
xLoc += xLoc2;
yLoc += yLoc2;
zLoc += zLoc2;


Vector Solution


var loc:Vector3D = new Vector3D(30, 15, 20);
var loc2:Vector3D = new Vector3D(2, 12, 0);

loc = loc.add( loc2 );

As you can see, much easier to read and follow, a great resource for getting started with vectors is Daniel Shiffman’s Nature of Code Series. The examples are done with Processing, eventually resulting in this book.

This entry was posted in AS3, Flash and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. Posted September 29, 2009 at 3:04 am | Permalink

    Hi,
    Thanks for this code.I am currently learning about the vector 3d and this code is great for utilisation.Thanks for this post.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*