Chapter 02

Vectors

The fundamental building block of game math — representing directions, displacements, velocities, and more.

In this chapter

  1. What is a Vector?
  2. Vector Operations
  3. Magnitude
  4. Unit Vectors & Normalization
  5. Distance Between Points
  6. Dot Product
  7. Exercises
1

What is a Vector?

A vector is simultaneously two things:

┌ 1 ┐ a = │ 2 │ (a 3D column vector) └ 3 ┘
Point vs Vector: A point specifies a position in space. A vector specifies a displacement — how far and in what direction to move. They use the same notation but mean different things.

Key Definitions

┌ 0 ┐ 0 = │ 0 │ └ 0 ┘

2

Vector Operations

Negation

Flipping the sign of each component. The result has the same magnitude but points in the opposite direction.

┌ ax ┐ ┌ -ax ┐ -a= │ ay │ = │ -ay │ └ az ┘ └ -az ┘

Scalar Multiplication

Multiply every component by scalar k. Scales the length by |k|. The result is parallel to the original — possibly in the opposite direction if k < 0. Works for division as well. Occurs before addition/subtraction (order of operations).

┌ ax ┐ ┌ k·ax ┐ k · │ ay │ = │ k·ay │ └ az ┘ └ k·az ┘

Addition & Subtraction

Add or subtract component-by-component:

┌ ax ┐ ┌ bx ┐ ┌ ax + bx ┐ │ ay │ + │ by │ = │ ay + by │ └ az ┘ └ bz ┘ └ az + bz ┘

Geometrically: Place the tail of b at the head of a. The sum a + b is the vector from the tail of a to the head of b. This is called the Triangle Rule.

To find the vector from point a to point b, put both at the origin and compute b - a. The result points from a toward b.

3

Magnitude

The magnitude (also called length or norm) of a vector is the Euclidean length of the directed line segment. Denoted with double bars: ‖v‖.

‖v‖ = √(vx² + vy² + vz²)
Magnitude is always non-negative. Only the zero vector has magnitude 0. When you only need to compare magnitudes (e.g. "which is closer?"), compare the squared magnitudes — it avoids the expensive square root.
‖v‖² = vx² + vy² + vz² ← cheaper, use for comparisons

4

Unit Vectors & Normalization

A unit vector has a magnitude of exactly 1. The hat notation ("v-hat") indicates a unit vector.

Normalizing a vector means scaling it to unit length. It "touches" the unit circle (or unit sphere in 3D).

v̂ = v / ‖v‖
Never normalize the zero vector — dividing by zero is undefined. In game code, always guard: if (‖v‖ > epsilon) { v̂ = v / ‖v‖; }

Unit vectors are used constantly in games to represent pure directions without encoding any length — forward vectors, surface normals, light directions, ray directions, etc.


5

Distance Between Points

The distance between two points a and b is the magnitude of the displacement vector from a to b:

dist(a, b) = ‖b - a‖ = √((bx-ax)² + (by-ay)² + (bz-az)²)
In game collision and AI, you almost always want distance squared to avoid the square root: distSq = (bx-ax)² + (by-ay)² + (bz-az)². Compare against radius² instead of radius.

6

Dot Product

The dot product of two vectors produces a scalar. It is computed as the sum of component-wise products:

a · b = ax·bx + ay·by + az·bz

Geometric Interpretation

The dot product equals the signed length of the projection of b onto the line of a:

a · b = ‖a‖ · ‖b‖ · cos θ

Where θ is the angle between the two vectors. The sign tells you their relative direction:

a · b > 0 → vectors point in the same general direction (θ < 90°) a · b = 0 → vectors are perpendicular (θ = 90°) a · b < 0 → vectors point in opposite general directions (θ > 90°)

Scaling and the Dot Product

Scaling either vector scales the numeric result, but does not change the geometric projection direction:

(k·a) · b = k(a · b) = a · (k·b)
When a is a unit vector (‖a‖ = 1), a · b gives exactly the scalar projection of b onto a. This is the version used in lighting (Lambert), collision response, and AI line-of-sight tests.

Vector Projection

To decompose vector v into a component parallel to unit vector and a perpendicular remainder:

v∥ = (v · n̂) · n̂ ← component along n̂ v⊥ = v - v∥ ← component perpendicular to n̂

This decomposition is used in reflection calculations, slide-along-wall movement, and the derivation of rotation and scaling matrices.


7

Exercises

1. Normalize a Vector

Normalize the vector v = (3, 0, 4).

‖v‖ = √(9 + 0 + 16) = √25 = 5.  v̂ = (3/5, 0, 4/5) = (0.6, 0, 0.8).

2. Dot Product

Compute a · b for a = (1, 2, 3) and b = (4, -5, 6). Are they perpendicular?

a · b = 4 - 10 + 18 = 12. Not perpendicular (would need to equal 0).

3. Distance Check

Player is at A = (1, 0, 1) and a collectible is at B = (4, 0, 5). The pickup radius is 3 units. Is the player close enough to collect it? Use squared distance to avoid the square root.

distSq = (4-1)² + (5-1)² = 9 + 16 = 25. radiusSq = 9. 25 > 9, so no pickup yet.

4. Vector Projection

A character's velocity is v = (3, 0, 4) and a wall's normal is n̂ = (1, 0, 0). Find the component of velocity pressing into the wall (v∥) and the slide component (v⊥).

v∥ = (v · n̂)·n̂ = 3·(1,0,0) = (3,0,0).  v⊥ = v - v∥ = (0,0,4). The character slides along the wall at speed 4 in the Z direction.

Interview Question

How would you check if an enemy is in front of the player using the dot product?

Compute the normalized direction from the player to the enemy: d̂ = normalize(enemy - player). Then dot it with the player's normalized forward vector: f̂ · d̂. If the result is positive, the enemy is in front (angle < 90°). If > some threshold like 0.7, they're within the player's FOV cone.

Interview Question

Why do we compare squared distance instead of distance for proximity checks in games?

sqrt() is one of the more expensive math operations. When you only need to know which of two distances is larger, or whether a distance exceeds a threshold, comparing the squared values gives the same answer without paying the square-root cost.
← Chapter 1 ↑ Index Chapter 3 →