Chapter 07

Polar Coordinate Systems

Radius and angle instead of x and y — and how games represent directions in 3D with heading and pitch.

In this chapter

  1. 2D Polar Space
  2. Aliasing & Canonical Coordinates
  3. 2D Coordinate Conversion
  4. 3D Polar: Cylindrical Coordinates
  5. 3D Polar: Spherical Coordinates
  6. Spherical Coordinates for Games
  7. 3D Coordinate Conversion
  8. Exercises
1

2D Polar Space

Instead of (x, y), polar coordinates describe a point with:

Point: (r, θ)
Polar coordinates are natural when you care about distance and direction rather than x/y offsets — radar sweeps, orbit paths, and circular spawn patterns are all more cleanly expressed in polar form.

2

Aliasing & Canonical Coordinates

For any given point there are infinitely many polar coordinate pairs that describe it. This is known as aliasing:

(1, 45°) == (1, 45° + 360°) == (1, 45° + 720°) ... (1, 45°) == (-1, -135°) (negative r flips direction)

For any integer k, all aliases of (r, θ) are given by:

(-1)ᵏ · r, θ + k·180°

Canonical Coordinates

Canonical coordinates are the "preferred" unique alias. They follow these rules:


3

2D Coordinate Conversion

Polar → Cartesian

x = r · cos θ y = r · sin θ

Cartesian → Polar

r = √(x² + y²) θ = atan2(y, x)

What is atan2?

Basic trig gives θ = arctan(y/x), but this has two problems: division by zero when x = 0, and the result is locked to [−90°, 90°]. atan2(y, x) solves both by branching on the signs of x and y:

atan2(y, x) = { 0 if x = 0, y = 0 90° if x = 0, y > 0 -90° if x = 0, y < 0 arctan(y/x) if x > 0 arctan(y/x) + 180° if x < 0, y ≥ 0 arctan(y/x) - 180° if x < 0, y < 0 }
In C++ / game engines: std::atan2(y, x) returns radians in the range (−π, π]. This is the standard function for converting a 2D direction vector to an angle. Note the argument order: y first, x second.

4

3D Polar: Cylindrical Coordinates

Extend 2D polar into 3D by adding a linear Z axis. A point in cylindrical space is (r, θ, z):

Think of it as choosing which horizontal "slice" of the cylinder you're on (z), then using 2D polar within that slice (r, θ).

Cylindrical → Cartesian: x = r·cos θ y = r·sin θ z = z Cartesian → Cylindrical: r = √(x² + y²) θ = atan2(y, x) z = z

5

3D Polar: Spherical Coordinates

The most common 3D polar form. Uses two angles to specify direction and one value for distance:

Point: (r, θ, φ) Spherical → Cartesian: x = r·sin φ·cos θ y = r·cos φ z = r·sin φ·sin θ Cartesian → Spherical: r = √(x² + y² + z²) θ = atan2(x, z) φ = arcsin(-y / r) (works with canonical conversions)

6

Spherical Coordinates for Games

Standard math spherical coordinates aren't ideal for games because:

Game engines remap the angles into more intuitive terms:

Heading (h)

Pitch (p)

Canonical Spherical Coordinates for Games

r ≥ 0 -180° < h ≤ 180° -90° ≤ p ≤ 90° Special case: if |p| = 90° → h = 0 (dealing with aliasing at poles)
Heading and pitch appear constantly in game dev: camera rotation, character look direction, AI facing, turret elevation. They're also the basis for Euler angles (heading = yaw, pitch = pitch, bank = roll).

7

3D Coordinate Conversion

Spherical (Game Convention) → Cartesian

x = r·cos p·sin h y = -r·sin p z = r·cos p·cos h

Cartesian → Spherical (Game Convention)

r = √(x² + y² + z²) h = atan2(x, z) p = arcsin(-y / r)
The negative sign on y in the pitch formulas accounts for the left-handed convention where positive pitch looks downward.

8

Exercises

1. Polar → Cartesian

Convert (r=5, θ=60°) to Cartesian coordinates.

x = 5·cos 60° = 5·0.5 = 2.5.  y = 5·sin 60° = 5·(√3/2) ≈ 4.33.  Result: (2.5, 4.33).

2. Cartesian → Polar Canonical

Convert (-3, -3) to canonical polar form.

r = √(9 + 9) = 3√2 ≈ 4.24.  θ = atan2(-3, -3) = -135°. Canonical: r ≥ 0, -180° < θ ≤ 180°. Result: (4.24, -135°).

3. Game Spherical Conversion

A unit vector pointing forward and slightly down: (0, -sin15°, cos15°). Find its heading and pitch.

h = atan2(x, z) = atan2(0, cos15°) = 0° (straight ahead). p = arcsin(-y/r) = arcsin(sin15°) = 15° (pitching down).

Interview Question

What is aliasing in polar coordinates, and how is it resolved?

Multiple polar coordinate pairs can describe the same point: (1, 45°) and (1, 405°) are identical, as are (1, 45°) and (-1, -135°). This creates ambiguity in interpolation and storage. It's resolved by defining canonical coordinates — restricting r ≥ 0 and θ to (−180°, 180°] — so each point has exactly one representation.

Interview Question

Why do game engines use heading/pitch instead of standard θ/φ for representing 3D directions?

Standard spherical coordinates use a right-handed system with +Y as the pole and measure angles from the top down, which doesn't match how artists think or how typical game coordinate systems work. Heading/pitch maps naturally to "look left/right" and "look up/down," uses the engine's native handedness, and transfers cleanly from the 2D case (heading is just the 2D polar angle on the ground plane).
← Chapter 6 ↑ Index Chapter 8 →