How can I calculate the angle between two 2D vectors?

Posted by Error 454 on Game Development See other posts from Game Development or by Error 454
Published on 2011-01-07T02:58:06Z Indexed on 2012/11/25 23:24 UTC
Read the original article Hit count: 177

Filed under:
|

I am working on some movement AI where there are no obstacles and movement is restricted to the XY plane. I am calculating two vectors, v, the facing direction of ship 1, and w, the vector pointing from the position of ship 1 to ship 2.

I am then calculating the angle between these two vectors using the formula

arccos((v · w) / (|v| · |w|))

The problem I'm having is that arccos only returns values between 0° and 180°. This makes it impossible to determine whether I should turn left or right to face the other ship.

Is there a better way to do this?

© Game Development or respective owner

Related posts about 2d

Related posts about vector