1

What is the formula used to determining moving object's rotation angle from its velocity or vector direction of travel?

For example, if an airplane is moving toward position (x1,y1) = (200, 100) from position (x0, y0) = (0,0), which angle would the plane be facing?

Gene
  • 46,253
  • 4
  • 58
  • 96
Elonoa
  • 467
  • 7
  • 19
  • Take a look at [this example](http://stackoverflow.com/questions/14886232/swing-animation-running-extremely-slow/14902184#14902184). It basically calculates the direction an object must have in order to follow a given path. – MadProgrammer Apr 03 '13 at 01:07
  • Velocity is how fast the speed increases or decreases, it doesn't say of the curve the object is taking. For example the plane could go in a straight line, therefore the angle would be 0, but the plane could go in a wide curve that would make the angle be 90... – JScoobyCed Apr 03 '13 at 01:07
  • Depends what you mean by "angle of rotation". If you mean the angle made by the ray emanating from (0,0) along the x-axis and the ray emanating from (0,0) and passing through (200,100), then simple trigonometry can tell you. If you mean something else, I'm afraid you'll need to say what that is before your question is answerable. – dlev Apr 03 '13 at 01:11
  • this would be SAS - Side Angle Side trig – Randy Apr 03 '13 at 01:12
  • Thanks for the replies. lol I think I really suck at math. What I meant was simply getting angle(in 0-360degrees) from an object that moves by, for example, x:2 y:3 every second. I remember the formula was pretty simple but dont remember exactly what it was. – Elonoa Apr 03 '13 at 01:16

2 Answers2

3

By rotation angle you apparently mean azimuth. The function Math.atan2(dy, dx) is the right approach. In your example with p1 = (x1, y1) = (200,100) and p0 = (x0, y0) = (0,0), you'd want Math.atan2(y1 - y0, x1 - x0). This will return the angle in radians. To convert to degrees, multiply by 180 / Math.PI.

Gene
  • 46,253
  • 4
  • 58
  • 96
  • 1
    +1. Assuming you're working in Flash from the question tags, one thing to bear in mind is that `Math.atan2` returns an angle relative to negative x, but `DisplayObject.rotation` is relative to negative y. – David Mear Apr 03 '13 at 01:28
2

To get the angle between two points, you plug the points into this code:

var _radians:Number = Math.atan2(y2-y1, x2-x1);

"_radians" is the angle in radians.

To rotate a DisplayObject, you can use its rotation property, after converting the angle into degrees.

var _degrees:Number = _radians * ( 180 / Math.PI );
_displayObject.rotation = _degrees;

You may need to add a contant to the rotation to line up with your art.

Lars Blåsjö
  • 6,118
  • 2
  • 19
  • 23
Mike Bedar
  • 632
  • 5
  • 14