PDA

View Full Version : Help with Angles



shack
June 1st, 2004, 04:02 PM
Hi everybody!
I need some help with getting angles, of two movieclips that are on my stage. With instance names: circle1,circle2.
I read that I should use the function Math.atan2 to get the angle, in radians, then I convert it to degrees, but it doesn't make sense, the angles aren't correct.. Here I paste the code I use (its AS not PHP don't know how to add the AS tags):
_global.Math.degree = function(radians) {
return (radians * 180/Math.PI);
}
trace(Math.round(Math.degree(Math.atan2(_root.circ le1._x,_root.circle2._y))));

Any help would be apreciated :) cheers everybody!

jbum
June 1st, 2004, 08:21 PM
Assuming you want to get the angle from circle1 to circle2, you would do this:



_global.Math.degree = function(radians) {
return (radians * 180/Math.PI);
}

// get the vector from circle1 to circle2 in dx,dy
dx = _root.circle2._x - _root.circle1._x;
dy = _root.circle2._y - _root.circle1._y;
rad = Math.atan2(dy,dx);
// this corrects for atan2 which returns values ranging from -PI to +PI
// rather than 0 to 2*PI
if (rad < 0) {
rad += 2*Math.PI;
}
trace(Math.round(Math.degree(rad)));


If circle 2 is directly to the right of the circle 1, this will show 0 degrees. As circle2 moves clockwise around circle 1, it willl go up to 359.99 and then back to zero. If you wish zero to be at some other position (such as at the top) than add (or subtract) a constant from the result to fix it.

shack
June 2nd, 2004, 01:51 AM
It works!! thank you very much :) I'll post the thing I'm trying to make when I finish it this afternoon!