Find the angle formed by a b2vec2

Hi everyone,

In my game I want a missile's angle to be the same as the angle formed by its mother ship's linear velocity.

So really what I need is a way to calculate the angle formed by a b2Vec2. I can't seem to get it right by using Math.atan(vector.y, vector.x). It would be nice to know of some function within the box2d classes that calculates a box2d-ready angle based on a given b2Vec2.
Math.atan2(x,y);
Thanks for telling me about atan2, I didn't know it existed.

angle = Math.atan2(y, x);
angle += Math.PI/2

this seems to do the trick for me
Hi,

Whats the difference between atan and atan2?

Regards

i
atan takes only the value of y/x as a parameter and returns a value in the interval [-pi/2,+pi/2] radians.
atan2 takes y and x separately so it can use their sign to determine the quadrant (see unit circle) and returns a value in the interval [-pi,+pi] radians.
Hi,

Thanks! Are there any other trig functions hidden away we should know about!?

p.s. when would you use atan over atan2?

Regards

i
indy2005 wrote:
Are there any other trig functions hidden away we should know about!?

That's more related to general math knowledge and your programming language of choice than it is to Box2D, so I'd recommend checking the relevant docs for more info (for example, if you're programming in C++, look online for references on the C++ standard library math functions).

Quote:
p.s. when would you use atan over atan2?

In game programming, just about never. (Generally speaking, atan2() is more stable and gives you more useful information.)
Jesse wrote:
(for example, if you're programming in C++, look online for references on the C++ standard library math functions).


http://www.cplusplus.com/reference/ 😉

Original thread: https://box2d.org/forum/viewtopic.php?t=5208