### How do I display a full 360 degrees using Actionscript 2 and Trigonometry?

0

I'm creating a game that uses trigonometry to calculate and display distance and degrees in dynamic text boxes. I'm calculating the distance of my cursor from center of a movie clip. And using that center of the movie clip, I'm trying to calculate and display a full 360º as my cursor moves around the swf. I have the distance part of the game working but the part that displays degrees is not working properly. The dynamic text box only display from 90º thru 270º. Instead of going past 270º to 360º/0º to 90º, it just counts back down from 270º to 90º. Below is my actionscript. I'd greatly appreciate any help or suggestions. Thanks!

``````//Mouse and Dynamic Text Boxes-------------------------

Mouse.hide();

onMouseMove = function () {
feedback.text = "You are moving your mouse";
cursor._x = _xmouse;
cursor._y = _ymouse;
updateAfterEvent();
xmouse_value.text = Math.atan2((a), (b));
updateAfterEvent();
};

//distance (RANGE)
_root.onEnterFrame = function () {
xmid = Stage.width/2;
ymid = Stage.height/2;

a = _root._ymouse-ymid;
b = _root._xmouse-xmid;
c = Math.sqrt(Math.pow(a, 2)+Math.pow(b, 2));
feedbacka.text = Math.round(a);
feedbackb.text = Math.round(b);
feedbackc.text = Math.round(c/30.4);

updateAfterEvent();

var degrees:Number;

//Radians specify an angle by measuring the length around the path of the circle.
radians = Math.atan2((c), (b))

//calculate degrees
//the angle the circle is in relation to the center point
//update text box inside circle

updateAfterEvent();

//getting past 270 degrees

}
``````
2012-04-04 21:18
by devinbyrnes

0

The parameters to `atan2` should be the delta-y and delta-x between the two points, but you are passing the distance between the two points and the delta-x. Try this instead:

``````radians = Math.atan2(a, b);
``````

The next problem is to convert the radians into degrees. To convert radians to degrees, you can do this:

``````degrees_txt = radians * 180 / Math.PI;
``````

Note that `atan2` returns from between `-Math.PI / 2` to `Math.PI / 2`. When converted to degrees, this range becomes -180 to 180. To convert to 0 to 360, you can add 360 to the result if it is negative:

``````if(degrees_txt < 0) degrees_txt += 360;
``````
2012-04-05 02:27
by Mike Welsh