I wrote this 5 years ago but never posted it anywhere. A friend’s tweet reminded me it was in my drafts and I figured it could be useful to someone.

The dot product is a function that can be applied to two equal dimension vectors and is sometimes referred to as the scalar or inner product by people of lower moral fiber. Typically you’ll see the dot product defined as the following:

bold letters are vectors, ||‘s surrounding a vector means the magnitude or length of the vector, and 𝜃 (pronounced “theta”) is the angle between the two vectors.

Look scary? In other words the dot product is the cosine of the angle between two vectors multiplied by their lengths. Initially it seems to be of pretty limited use but in practice it pops up all over. Let us look at a few:

Measuring direction

If you looked closely the first thing you may have noticed is that you can get the cosine of the angle between two vectors with a bit of rearranging:

And then use arccos function (also known as cos^-1)to get the angle or just test against cosine values for typical angles if you want to be cheaper than IKEA furniture with your CPU cycles. If both vectors x and y are unit length (we usually indicate this by putting a ^ symbol above the vector, but I’ve omitted the ^’s in the paragraphs here since medium doesn’t support it) then we don’t need the division or calculation of the vector’s length and can save square roots. If we look a little closer at this equation and the properties of the cosine we can see some shortcuts we can take:

Since the only way a negative number can be introduced to this equation is the cosine function, the result of the dot product is negative if and only if the vectors point in a direction greater than pi/2 radians (90 degrees) apart from one another. The simple takeaway: negative dot product means the vectors point in different directions.

If the dot product is zero the two vectors are orthogonal (perpendicular).

If the vectors are unit length and the result of the dot product is 1, the vectors are equal.

Projecting Vectors

Say we want to know how much a vector is displacing in a certain direction.

Example uses include calculating the speed at which a character is moving in the direction of a slope or wall, finding how much of a character’s velocity is in the direction of gravity, finding how long until a movement crosses a threshold, and more.

When at least one vector is of unit length the dot product is the length of the projection of the non-unit length vector onto the axis of the normalized vector (When they are both unit length, they are both moving the same amount in the direction of each other).

A reader pointed out that the term normalized vector (referring to a vector that is unit length pointing in the same direction of some vector, an operation called normalizing) and a normal vector (referring to a vector perpendicular to a surface’s local plane) can be confusing for people newer to vector math. Its an unfortunate naming collision, but now you know — they are two different things. Ok. Let’s move on, because I’m about to refer to the other one now.

Planes (aka walls/slopes/triangles)

Given a plane defined by a unit normal vector n and point p, we can find which side of the plane a vector x is by seeing if the result of the following equation is positive, zero or negative:

positive indicates in front of the plane, negative behind, and zero indicates the point is coincident with the plane.

We might also want to reflect a vector about a plane. Say we have a point mass at point x travelling with velocity v. We test whether a collision will occur in time slice dt by checking if x + vdt is on the opposite side of the plane as x. Once we have confirmed a collision (for our purposes it will be an elastic collision), we need to find out where the point mass has ended up. Assuming the plane is fixed (has a weight of infinity) the point mass will reflect with the same velocity about the normal of the plane. The new velocity is given by:

The new position is given by the following equation:

This diagram shows the relationships above:

Can I see it?

We frequently want to know if some object is in the field of view of some observer in our game. Given an observer at point p, an object at point o, and assuming the object is not obstructed from view (test this with one or several raycasts), take the dot product of the observers viewing direction v with the normalized vector from the observer to the object. The result is the cosine of the angle between them. Test against the cosine of the max viewing angle cphi of the observer.

Next Steps

Here’s a few dot product tricks but there are many more to discover. Drop a comment if you have a useful trick or interpretation. If you’re feeling confident maybe you’d like to venture on to the cross product next?