The fundamental building block of game math — representing directions, displacements, velocities, and more.
A vector is simultaneously two things:
Flipping the sign of each component. The result has the same magnitude but points in the opposite direction.
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).
Add or subtract component-by-component:
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.
b - a. The result points from a toward b.
The magnitude (also called length or norm) of a vector is the Euclidean length of the directed line segment. Denoted with double bars: ‖v‖.
A unit vector has a magnitude of exactly 1. The hat notation v̂ ("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).
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.
The distance between two points a and b is the magnitude of the displacement vector from a to b:
distSq = (bx-ax)² + (by-ay)² + (bz-az)². Compare against radius² instead of radius.
The dot product of two vectors produces a scalar. It is computed as the sum of component-wise products:
The dot product equals the signed length of the projection of b onto the line of a:
Where θ is the angle between the two vectors. The sign tells you their relative direction:
Scaling either vector scales the numeric result, but does not change the geometric projection direction:
‖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.
To decompose vector v into a component parallel to unit vector n̂ and a perpendicular remainder:
This decomposition is used in reflection calculations, slide-along-wall movement, and the derivation of rotation and scaling matrices.
Normalize the vector v = (3, 0, 4).
Compute a · b for a = (1, 2, 3) and b = (4, -5, 6). Are they perpendicular?
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.
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⊥).
How would you check if an enemy is in front of the player using the dot product?
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.
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.