$\begingroup$

I am currently learning to use normalized vectors in the computer games I'm creating.

I've learned that in order to know "the angle" between two vectors, I need to use Dot Product. This gives me a value between $1$ and $-1$. $1$ means they're parallel to each other, facing same direction (aka the angle between them is $0^\circ$). $-1$ means they're parallel and facing opposite directions ($180^\circ$). And $0$ means the angle between them is $90^\circ$.

But I want to know, how to convert the dot product of two vectors, to an actual angle in degrees.

For example, if the dot product of two vectors is $0.28$. How can I convert it to an actual angle, between $0^\circ$ to $360^\circ$?

Thank you

$\endgroup$3

2 Answers

$\begingroup$

The dot product of two normalized vectors is equal to the cosine of the angle between them. In general $$\cos \phi = \frac{a\cdot b}{|a||b|},$$ since your vectors are normalized, $|a|=|b|=1$ and $\phi = \arccos(a\cdot b)$

$\endgroup$4$\begingroup$

Because you want an answer in the range [0,2pi) and not [0,pi], I think that the question's title is misleading. I think that you are asking, "How can I calculate the angle that will change the direction from vector a to vector b?"

I would not use the arccos formula for dot products, but instead use the arctan2 function for both vectors and subtract the angles. The arctan2 function is given both x and y of the vector so that it can give an angle in the full range [0,2pi) and not just [-pi,pi] which is typical for arctan.

The angle you are looing for would be given by:

arctan2(b_y, b_x) - arctan2(a_y, a_x) 

The result may be a negative angle, but at least it will go from vector a to vector b. If you want only positive angles, then add 2pi when the angle is negative.

Beware that arctan2 takes y then x. This is to be similar to arctan which takes y/x as input.

$\endgroup$1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy