5
$\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$
  • $\begingroup$ From your setting it seems that the vectors are of length one. Then the angle $\theta$ is just $\cos\theta = (v_1\cdot v_2)$. I am more interested in what games you are playing though. $\endgroup$ – user99914 Jan 28 '14 at 9:08
  • $\begingroup$ I meant games I'm making :) . And yes, I'm talking about normalized vectors with length 1. So you're saying that in order to get the angle between two vectors, I don't even need their dot product? $\endgroup$ – user3150201 Jan 28 '14 at 9:12
  • 1
    $\begingroup$ @user3150201: No this is precisely not what he said. In order to get the angle between two vectors you need to take the arccos of the dot product. $\endgroup$ – J.R. Jan 28 '14 at 9:13
12
$\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$
  • 3
    $\begingroup$ Worth noting is that the angle yielded would currently be in radians and is the range is the absolute [0,pi] angle between the vectors, not the [0,2pi) angle you need to do a planar rotation in the same direction to get from a to b. To reconstruct the full range you need to determine whether to pick the small or the complementary large angle between the vectors. $\endgroup$ – Lars Viklund Jan 28 '14 at 14:47
  • $\begingroup$ So what you're saying is that the angle between two normalized vectors, is the arccos of their dot product? $\endgroup$ – user3150201 Jan 28 '14 at 21:01
  • $\begingroup$ That is exactly what I am saying. If they aren't normalized, just divide the dot product by their norms. $\endgroup$ – 5xum Jan 29 '14 at 7:56
  • 1
    $\begingroup$ @5xum and how exactly can I determine if the angle should be small or complementary? I mean, in e.g. a computer program that should return the angle from [0, 2pi) range for any two given a and b vectors? $\endgroup$ – PolGraphic Sep 23 '15 at 3:17
5
$\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$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.