## Monday, November 7, 2011

### Using Vector Mathematics, finding angle between two vectors

In the last blog post we learned about Dot Products, and how it could be used to find relative differences between two vectors, but now we will learn how to find the exact angle between those two vectors.

For this example we will use two very simple vectors. To get the angle between two vectors you will need to unitize your vectors or make sure they're already unit-length, for simplicity I will just use two unit-length vectors to start with in this example:

Vector a = (1, 0, 0)
Vector b = (0, 0, 1)

You'll notice the above vectors are both along an axis, which means they're perfectly perpendicular, which also means we know already that they're 90 degrees apart ( π/2 radians apart ), but we'll use that knowledge as proof that what I'm about to show you does in fact work. Also, for future reference, please note that any vectors that are perpendicular to each other are also known as orthogonal, this term is often used when referring to vectors, so it's important to know.

Ok, so first we'll get the dot product of the vectors, which should end up being 0 since the vectors are orthogonal.

a (dot) b = (1*0 + 0*0 + 0*1) = 0

And now to get the angle between the vectors, we will use the trigonometric function for arc-cosine, called acos(). Please note that acos() requires a value between -1 and 1, anything outside of that range will cause acos() to return a non-number return value, which you don't want. It's safest to range check what you're about to pass it just in case.

acos(0) = π/2 radians, which is 90 degrees.

If you have two vectors that are parallel, you'll always get a dot product of ±1, and acos(-1) or acos(1) is always 0 radians/degrees as you would expect. But lets test it with a dot product just for fun:

Vector c = (1, 0, 0)
Vector d = (1, 0, 0)

c (dot) d = (1*1 + 0*0 + 0*0) = 1

Vector e = (-1, 0, 0)

c (dot) e = (1*-1 + 0*0 + 0*0) = -1

And.....now some code:
```inline float FindAngleBetweenVectors( const Vector3& first,
const Vector3& second )
{
// You might leave these Unitize calls out if you know
// you're always passing in Unitized vectors. However
// I recommend having two versions of this function,
// one that takes unit vectors, and one that will unitize
// them for you.
// Copies are required because we're passing in 'first'
// and 'second' by const reference, which allows the
// user to pass in vectors and not worry about them
// unintentionally getting unitized.
Vector3 firstCopy = first;
firstCopy.Unitize();

Vector3 secondCopy = second;
secondCopy.Unitize();

const float dotProduct = firstCopy.Dot(secondCopy);

// A range check here is a good idea for safe code, I'll
// leave it out for this sample though.

return acos(dotProduct);
}
```