Brian Chia
       
 
categories
actionscript
actionscript 1.0
actionscript 2.0
misc
 
processing
 
links
brian chia
kelvin zhao
ronald leong
 
 
Coordinate Rotation v01
 

This formula seems more direct and easier to implement but there certain conditions that needs to be met before we can implement it. We need to know the center point of rotation, the radius from the center point and of course the angle to rotate.

x=cos(angle)*radius;
y=sin(angle)*radius;


// radius from center pt
var r:Number=100;

// inital angle
var a:Number=0;

// define center pts
var centerX:Number=cross._x;
var centerY:Number=cross._y;

this.onEnterFrame=function():Void{

rotate();

}

function rotate():Void{

ball._x=centerX+Math.cos(a*(Math.PI/180))*r;
ball._y=centerY+Math.sin(a*(Math.PI/180))*r;

// increase in angle in degrees
a+=1;

}



For the above example, it may seem fine when we are just rotating 1 object. But if we were to rotate multiple objects and their positions to the center point could be changing. ie. Momentum v02. If we were to apply the above formula, then we need to calculate each object radius from the center point, the angle and the center point x and y values. Imagine if there were 10 objects, then we need to create 10 set of variables.

So this is the time to implement the next formula:

newX=cos(angle)*x-sin(angle)*y;
newY=cos(angle)*y+sin(angle)*x;

Where x and y is the distance from the object to the center point


// angle to rotate in degrees
var a:Number=1;
var ca:Number=Math.cos(a*(Math.PI/180));
var sa:Number=Math.sin(a*(Math.PI/180));

// define center pts
var centerX:Number=cross._x;
var centerY:Number=cross._y;

this.onEnterFrame=function():Void{

rotate();

}

function rotate():Void{

// return ball position relative to center pt
var dx:Number=ball._x-centerX;
var dy:Number=ball._y-centerY;

// rotate ball and return new x and y values relative to center pt
var x2:Number=ca*dx-sa*dy;
var y2:Number=ca*dy+sa*dx;

// add new x and y values to center pt to get final position
ball._x=centerX+x2;
ball._y=centerY+y2;

}



suggestion

 
 
 
         
  © copyright 2007 THINKLUNATIC.    
Brian Chia Jonathan Ng