0

I'm too dumb to solve basic algebra, would be cool if you could help me out with this

usually I got the rotation and try to get position I want to rotate the object to

var rads : Number = rotation / 180 * Math.PI;
position.x = Math.cos(rads);
position.y = Math.sin(rads);

this time I got the position but I want to get the rotation but everything I tried is false, thanks in advance for your help

EDIT: for those who got the same problem and use actionscript 3 (or anything that also has this function) experiment with Math.atan2() it can save you a lot of trouble - faster and easier

BenMorel
  • 34,448
  • 50
  • 182
  • 322
tschery
  • 153
  • 2
  • 15

2 Answers2

1

I think that you're asking how to get the angle between the positive x-axis and the line segment going from the origin to (x,y). I'll assume that x and y are positive since that simplifies the discussion and, if they aren't, you can make them positive and then just reflect that angle across the appropriate axis.

You can get the cosine of the angle with x/Math.sqrt(x*x + y*y). Then Math.Acos on that value will calculate the arccos (which is the value of the angle you're looking for) in radians.

David
  • 1,429
  • 8
  • 8
  • jeah you are right I didnt know how to ask for that. X and Y are not positive so how do I reflect the angle across the appropriate axis? thanks alot =) – tschery Dec 12 '12 at 04:38
  • If X is negative, multiply it by -1, then do the calculation above, then subtract the result from Math.PI. If Y is negative, multiply it by -1, then do the calculation above, then subtract the result from 2*Math.PI. If X and Y are both negative, multiply both by -1, then do the calculation above, then add the result to Math.PI. (I'm having to write this quickly, but I think that should work.) – David Dec 12 '12 at 04:43
  • thanks alot you explained everything very well and jeah it should work, gonna try it now – tschery Dec 12 '12 at 05:00
1

Most languages provide a function called atan2() which takes the rise and run and does all the heavy lifting for you, giving the correct answer in all four quadrants.

>>> math.atan2(-1, 1)
-0.7853981633974483
>>> math.atan2(-1, 1) * 180 / math.pi
-45.0
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358