10
$\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
  • $\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
    Commented Jan 28, 2014 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$ Commented Jan 28, 2014 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.
    Commented Jan 28, 2014 at 9:13

2 Answers 2

17
$\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
  • 4
    $\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$ Commented Jan 28, 2014 at 14:47
  • 1
    $\begingroup$ So what you're saying is that the angle between two normalized vectors, is the arccos of their dot product? $\endgroup$ Commented Jan 28, 2014 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
    Commented Jan 29, 2014 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
    Commented Sep 23, 2015 at 3:17
12
$\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
  • $\begingroup$ If you came looking for how to work out camera angles in shader code, this was the right answer $\endgroup$ Commented Jun 30, 2021 at 23:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .