Math Tools Math Tools

The Math Behind 3D Games

The Math Behind 3D Games

By Math Tools ·

The Math Behind 3D Games

A game running at 60 frames per second has 16.67 milliseconds to do everything for each frame: read your controller, move every character, check thousands of possible collisions, simulate physics, and draw the scene. At 144 fps, the budget shrinks to under 7 milliseconds.

Games fit into that budget by choosing mathematics that's both correct and fast. Sometimes that means elegant ideas from 19th-century algebra. Sometimes it means a mysterious hexadecimal constant.


Games Rotate Things Using Four Dimensions

The obvious way to describe a 3D rotation is with three angles: yaw, pitch and roll, known as Euler angles. Many game engines avoid them for internal calculations. Instead they use quaternions, four-component numbers that live in a four-dimensional number system.

Why take the detour through four dimensions? Because three angles can lose a degree of freedom, a problem called gimbal lock, and they interpolate badly between orientations. Quaternions have neither problem.


Vectors: Movement and Direction

Every position, velocity and direction in a game is a vector. Moving a character each frame is:

position = position + velocity × Δt

Where Δt is the time since the last frame. Using Δt means movement is the same whether the game runs at 30 or 144 fps.

The dot product answers questions games ask constantly:

  • Is the enemy in front of me? If forward · (enemy − me) > 0, yes.
  • How wide is the angle? cos θ = (a · b) / (|a| |b|)

If an enemy's field of view is 90° wide, it can see the player when the angle to the player is at most 45°, meaning cos θ ≥ cos 45° ≈ 0.707. Check with the arc cosine calculator.


Quaternions and the Bridge Carving

In 1843, Irish mathematician William Rowan Hamilton had been trying for years to extend complex numbers to three dimensions. Walking along Dublin's Royal Canal on October 16, he realized it needed four components. He carved the defining formula into the stone of Broom Bridge:

i² = j² = k² = ijk = −1

A unit quaternion representing a rotation by angle θ around an axis (x, y, z) is:

q = (cos(θ/2),  x·sin(θ/2),  y·sin(θ/2),  z·sin(θ/2))

Rotations compose by quaternion multiplication, and spherical linear interpolation (slerp) gives perfectly smooth camera and character turns. Ken Shoemake introduced slerp for animation in 1985.


Gimbal Lock

With Euler angles, if you pitch exactly 90° up, the yaw and roll axes line up and become the same rotation. You've lost a degree of freedom. The Apollo 11 guidance system famously had to avoid gimbal lock in its physical gyroscope gimbals; astronaut Michael Collins joked about asking for "a fourth gimbal for Christmas."


Collision Detection

Checking every triangle against every other triangle would be far too slow. Games use layers of cheap tests first.

Bounding Spheres

Two spheres overlap if the distance between their centers is less than the sum of their radii. To skip a square root, compare squared values:

(x₂ − x₁)² + (y₂ − y₁)² + (z₂ − z₁)² < (r₁ + r₂)²

Axis-Aligned Bounding Boxes (AABB)

Two boxes overlap only if their ranges overlap on all three axes:

a.minX ≤ b.maxX and a.maxX ≥ b.minX   (and the same for y and z)

The Separating Axis Theorem

For convex shapes, the separating axis theorem says two shapes don't collide if and only if there's some line onto which their projections don't overlap. It turns a hard 3D question into a series of simple 1D interval checks.


Physics: Numerical Integration

Physics engines update motion step by step using numerical integration. The simplest method is explicit Euler:

velocity = velocity + acceleration × Δt
position = position + velocity × Δt

It's fast but can gain energy over time, so springs explode and orbits spiral outward. Many games use semi-implicit Euler or Verlet integration, which are just as cheap but far more stable. Verlet integration was popularized for games by Thomas Jakobsen's 2001 paper on the physics of Hitman: Codename 47.


An Insider Reference: The Fast Inverse Square Root

Lighting needs normalized vectors, which means computing 1/√x constantly. When the source code of id Software's Quake III Arena (1999) was released in 2005, programmers found this line:

i = 0x5f3759df - ( i >> 1 );

It approximates 1/√x using the bit representation of a floating-point number, followed by one step of Newton's method. The constant 0x5F3759DF (1,597,463,007 in decimal) was a magic number whose origin puzzled programmers for years. The code is often associated with John Carmack, but he has said he didn't write it; its history traces back through several earlier programmers. Modern CPUs have dedicated instructions for this, but it remains a legendary example of math-driven optimization.


Two Concepts Worth Knowing

Linear Interpolation

Lerp blends between two values: lerp(a, b, t) = a + (b − a) × t, for t from 0 to 1. Games use it for everything from health bars to camera movement.

Newton's Method

Newton's method refines a guess for a root of f(x) with x ← x − f(x) / f′(x). Each step roughly doubles the number of correct digits, which is why one step was enough for Quake.


Quick Answer: What Math Do 3D Games Use?

3D games use vectors for positions and movement, matrices and quaternions for rotation, dot products for lighting and visibility checks, geometric tests like bounding boxes and the separating axis theorem for collisions, and numerical integration for physics, all within a frame budget of a few milliseconds.


Try Them Yourself

Next time a game stutters, remember: something in that frame took longer than 16.67 milliseconds of math.