Calculating coordinates to rotate
There are two formulas to calculate coordinates (x, y) with trigonometric functions by specifying angle ø. The first is “polar coordinates” defined with a radius, distance r from the origin (0, 0), and an angle ø between the x axis.
[1] polar: radius r from the origin (0, 0) and angle ø between the x axis.
x: r cosø
y: r sinø

The following code places a MovieClip instance my_mc at the point of which the radius is radius and the angle is radians.
my_mc.x = radius * Math.cos(radians); my_mc.y = radius * Math.sin(radians);
The second is the formula of transforming coordinates (x, y), which rotates the point around the origin (0, 0) by angle ø.
[2] transform: rotating point (x, y) around the origin (0, 0) by angle ø.
x: x cosø – y sinø
y: x sinø + y cosø
The following code rotates a MovieClip instance my_mc around the origin of the parent instance by angle theta.
var nSin:Number = Math.sin(theta); var nCos:Number = Math.cos(theta); var nX:Number = my_mc.x; var nY:Number = my_mc.y; my_mc.x = nCos * nX - nSin * nY; my_mc.y = nSin * nX + nCos * nY;
Formula [2] is superior to [1] to rotate many instances around the origin by the same angle. The Math.sin() and Math.cos() methods should not be called every time to rotate instances because the angle remains the same.
var nSin:Number = Math.sin(theta);
var nCos:Number = Math.cos(theta);
//
for (var i:uint = 0; i < count; i++) {
// get reference of my_mc
var nX:Number = my_mc.x;
var nY:Number = my_mc.y;
my_mc.x = nCos * nX - nSin * nY;
my_mc.y = nSin * nX + nCos * nY;
}
See the wonderfl test code to compare two ways.
Calculating coordinates to rotate - wonderfl build flash online
2 Comments
And I started to think jactionscripters.com is abandoned
[...] Calculating coordinates to rotate [...]