Introduction

If graphics programming had a favourite calculation, it would be the dot product. It takes two vectors (the previous post) and gives back a single number that says how much they point the same way. Lighting, shadows, cameras, game AI and physics all lean on it. This post covers:

  • How to calculate a dot product (it’s easy)
  • What the answer means, especially for unit vectors
  • The projection: “how far does this arrow go in that direction?”
  • A practical example: can a guard see the player?

How to Calculate It

The dot product of two vectors is written with a dot: a · b. To work it out, multiply the matching numbers together and add up the results:

a · b = ax × bx + ay × by   (and + az × bz in 3D)

For example, (3, 1) · (2, 4) = 3 × 2 + 1 × 4 = 6 + 4 = 10. The answer is a single number, not a vector. That’s all the arithmetic there is. In shader code it’s the dot function.

What It Means

The magic is in what that number tells you. Drag the tips of a and b below.

The dot product of two vectors, and b’s “shadow” along a

Watch the sign of the answer:

  • Positive when the arrows point roughly the same way (less than 90° apart).
  • Zero when they’re exactly at right angles.
  • Negative when they point roughly opposite ways (more than 90° apart).

That sign alone answers questions like “is this surface facing the light?” and “is that object in front of me or behind me?”.

For unit vectors, it’s the angle

Tick Make both unit vectors. Now the answer always lies between −1 and 1: exactly 1 when they point the same way, 0 at right angles, −1 when opposite, and smoothly in between. This number is called the cosine of the angle between them (the next-but-one post explains cosines properly), but you don’t need to know that to use it: think of it as an “agreement score” from −1 to 1.

That’s why graphics normalises directions so often: with unit vectors, one dot product gives you a ready-made measure of how closely two directions line up, with no angles involved.

The projection: “how far along?”

Untick the box and look at the green arrow. It shows b’s shadow along a’s direction, as if a light were shining straight down onto the line through a. Its length is how far b reaches in the direction of a. When a is a unit vector, that length is exactly a · b. (Otherwise, divide by the length of a.)

This is one of the most useful tricks in geometry. “How fast is the car moving forwards?” is the car’s velocity projected onto its forward direction. “How high above the floor is this point?” is the point projected onto the floor’s upward direction.

A Practical Example: Can the Guard See You?

A guard is looking in a direction (the blue arrow; drag its tip), with a field of view: a cone in which it can see. Drag yourself (the circle) around, or drag the guard itself.

A field-of-view check using one dot product

The test is: make a unit vector for the way the guard faces, and a unit vector from the guard towards you, then take their dot product. If it’s bigger than a threshold (worked out once from the field of view), you’re in the cone. No angles, no trigonometry: three multiplications, two additions and a comparison, which a game can afford to do for hundreds of guards every frame.

Under the hood

Dot products are so common that graphics cards are built to do them fast: a dot product is just a chain of multiply-and-add steps, which every shader core can do in a single instruction each. A typical fragment shader for lighting might do a dozen of them per pixel. The same operation is at the heart of matrix multiplication: each number in the result of matrix × vector is the dot product of one row of the matrix with the vector (Transformation Matrices). It’s also the core operation of modern AI chips, which do enormous numbers of dot products every second.

Where You’ll Use This

  • Lighting: how much light a surface receives is N · L, the dot product of its normal with the direction to the light (Basic Lighting); shiny highlights use another one.
  • Facing and culling: whether a surface faces towards or away from the camera.
  • Shadows and edges: shadow mapping bias is often scaled by a dot product; outline effects use one to find edges facing sideways to the camera.
  • Matrices: every number in a matrix multiplication is a dot product (Transformation Matrices).
  • Games: field-of-view checks, “is the target in front of me?”, splitting a velocity into forwards and sideways parts.

Questions You Might Have

Does the order matter in a dot product?

No: a · b = b · a, because it just multiplies matching parts and adds them up, and multiplication doesn’t care about order. (The cross product is different: swapping its order flips the answer.)

Summary

  • To find a · b, multiply matching numbers and add: axbx + ayby + azbz.
  • Positive = same-ish direction, zero = right angles, negative = opposite-ish.
  • For unit vectors, it’s an agreement score from −1 to 1 (the cosine of the angle).
  • With a unit vector, a · b is how far b reaches in a’s direction (the projection).

Next: the cross product, which finds the direction at right angles to two others.

Further Reading