One number that compares two directions
The dot product of two vectors multiplies matching parts and adds them: (a, b) · (c, d) = ac + bd. The answer is a single number, not a vector. That number answers a surprisingly useful question: how much do these two arrows point the same way?
(1, 0) · (3, 4) = 1×3 + 0×4 = 3.
(1, 0) · (−2, 5) = −2.
(1, 0) · (0, 7) = 0.
Positive means “roughly the same way,” negative means “roughly opposite,” and zero means “at a right angle.”
With unit vectors, it is the cosine of the angle
If both vectors have length 1, the dot product equals cos θ, where θ is the angle between them. It is 1 when they point the same way, 0 at a right angle, and −1 when they are opposite. For (1, 0) and the unit vector (0.6, 0.8), the dot product is 0.6, and the angle is the arccosine of 0.6, about 53°.
Turn the enemy’s position around a guard facing right.
Can the guard see me?
A stealth game asks this every frame. Take the guard’s facing direction (a unit vector) and the unit vector from the guard to the player. If their dot product is greater than the cosine of half the field of view, the player is inside the vision cone. For a 90° field of view, half is 45°, and cos 45° ≈ 0.707.
func can_see(guard_pos: Vector2, facing: Vector2, target: Vector2) -> bool:
var to_target := (target - guard_pos).normalized()
return facing.dot(to_target) > cos(deg_to_rad(45)) # 90-degree coneMore uses for one small formula
Front or behind? The sign of the dot product alone tells whether something is ahead (positive) or behind (negative). Lighting: 3D graphics brighten a surface by the dot product of its outward direction and the direction toward the light: facing the light is bright, edge-on is dark. Projection: the dot product with a unit vector measures how far a vector reaches along that direction, which is how a game splits a velocity into “along the wall” and “into the wall” when a character slides.
Forgetting to normalize. The cosine interpretation only works for unit vectors. (10, 0) · (30, 40) = 300, which is not a cosine at all. Normalize first, or divide by both lengths.
The dot product is usually introduced in precalculus or physics, but it rests on two ideas you already know: multiplying and adding, and the cosine of an angle from right-triangle trigonometry. In physics, work equals force · displacement, the same calculation.
Where you will see it
- Stealth and AI: vision cones and “is the player in front of me?”
- 3D lighting: how bright each surface should be.
- Physics: sliding along walls and bouncing off them.
- Machine learning: measuring how similar two lists of numbers are.
Exercises
Compute (2, 3) · (4, −1).
2×4 + 3×(−1) = 8 − 3 = 5.
A guard faces (0, 1). A player is in direction (0, −1). Is the player in front or behind?
The dot product is −1, so directly behind.
Are (3, 6) and (−2, 1) perpendicular?
3×(−2) + 6×1 = 0, so yes.
