(Flash) Rotation around a center - Math and (Quasi) physics in AS3

| No Comments | No TrackBacks

This sample demonstrates two ways of ratating coordinate around a center point

The first example(P1 and C1) is straight forward while the second(P2 and C2) is more versatile and can be more efficent when treating more than one point in the same angle, in that case sine and cosine need to be computed only once before applied to each coordinate calculation of the points.

// P1 and C1 
var angle:Number = Math.atan2(P1.y - C1.y, P1.x - C1.x);
var radius:Number = Point.distance(C1, P1);
P1.x = C1.x + Math.cos(angle + vr) * radius;
P1.y = C2.y + Math.sin(angle + vr) * radius;

// P2 and C2
var distX:Number = P2.x - C2.x;
var distY:Number = P2.y - C2.y;
P2.x = Math.cos(vr) * distX - Math.sin(vr) * distY + C2.x;
P2.y = Math.cos(vr) * distY + Math.sin(vr) * distX + C2.y;

In action script 3, the Matrix class's rotate method works in the same way as the second example.

sample movie and scripts

No TrackBacks

TrackBack URL: http://www.kynd.info/cp-bin/mt/mt-tb.cgi/42

Leave a comment