Chapter 09

Geometric Primitives

Lines, rays, spheres, planes, AABBs, and the representations used to test them in game engines.

In this chapter

  1. Lines and Rays
  2. Line Representations
  3. Spheres and Circles
  4. Bounding Boxes (AABB & OBB)
  5. AABB Properties
  6. Planes
  7. Exercises
1

Lines and Rays

Straightforward Form

A ray defined by two points: its origin P₀ and its endpoint Pend.

(P₀, Pend)

Parametric Form

The standard form used in ray casting and intersection tests:

P(t) = P₀ + t·d P₀ = ray origin point d = "delta vector" — encodes both direction and length t = how far along d to travel (typically in [0, 1]) P(0) = P₀ (at origin) P(1) = P₀ + d (at endpoint) P(0.5) = P₀ + d/2 (midpoint)

Normalized Parametric Form

When is a unit vector, t directly equals distance from the origin in world units:

P(t) = P₀ + t·d̂ (d̂ is unit length, so t = distance traveled)
Normalized parametric form is preferred for ray-intersection tests because t values are directly comparable to distances and radii. Always normalize the direction vector when storing rays used in intersection queries.

2

Line Representations

Slope-Intercept Form (2D)

y = m·x + y₀ m = slope (rise / run) y₀ = y-intercept (where the line crosses the y-axis) * Cannot represent vertical lines (undefined slope)

Implicit Form (2D)

A boolean function — true for all points on the line, false for all others:

ax + by = d (sometimes written ax + by + c = 0) P · n̂ = d (dot product form, where n̂ = [a, b] and P = [x, y]) n̂ = [a, b] = unit vector orthogonal to the line (the normal) d = signed distance from origin to the line in the direction of n̂ When d > 0: origin is "behind" the plane of n̂ When n̂ is not unit: d = signed distance × ‖n̂‖

Converting Between Representations

Straightforward (P₀, Pend) → Parametric P(t) = P₀ + t·d: d = Pend - P₀ Parametric → Straightforward: P₀ = P₀, Pend = P₀ + d Parametric → Implicit (ax + by = d): a = dy, b = -dx, d = x₀·dy - y₀·dx Implicit → Normal/Distance: n̂ = [a, b] / √(a² + b²), distance = d / √(a² + b²)

3

Spheres and Circles

Straightforward Form

(C, r) C = center point, r = radius
Spheres are extremely cheap for collision detection — a single distance check determines containment or intersection. Prefer spheres over AABBs for collision broadphase when objects are roughly spherical.

Implicit Form

‖P - C‖ = r (on the surface) (x - Cx)² + (y - Cy)² + (z - Cz)² = r² For P inside the sphere, change "=" to "≤"

Classic Formulas

Diameter = 2r Circumference (2D) = 2πr Circle Area = πr² Surface Area (sphere)= 4πr² Volume (sphere) = (4/3)πr³

4

Bounding Boxes (AABB & OBB)

Axis-Aligned Bounding Box (AABB)

A box whose sides are parallel to the principal axes. Defined by two corner points or by a center and size vector:

AABB: { Pmin, Pmax } or { C, s } Pmin = [xmin, ymin, zmin] (the "lower-left-back" corner) Pmax = [xmax, ymax, zmax] (the "upper-right-front" corner)
AABB Advantages
  • Very fast intersection tests
  • Simple to compute from a set of points
  • Only 2 points to store (or 1 center + 1 size)
  • Easy to transform (expand to encompass new corners)
AABB Disadvantages
  • Can fit poorly for rotated or thin objects
  • Must be recomputed when object rotates
  • Larger than necessary for diagonal objects

Oriented Bounding Box (OBB)

An AABB with an orientation — it's aligned to the object's local space rather than world space. Tighter fit for rotated objects, but more expensive to test.

Typical collision pipeline: use large AABBs for the broadphase (quickly reject non-overlapping pairs), then use OBBs or convex hulls for the narrowphase (precise overlap + contact manifold). This layered approach keeps average-case cost low.

5

AABB Properties

All points P = (x, y, z) inside or on an AABB satisfy:

xmin ≤ x ≤ xmax ymin ≤ y ≤ ymax zmin ≤ z ≤ zmax

Center and Size

Center C = (Pmin + Pmax) / 2 Size vector S = Pmax - Pmin (width, height, depth)

You only need two of these values (Pmin + Pmax, C + S, or C + Pmin, etc.) to fully define an AABB.

Recomputing AABBs After Transformation

After rotating or transforming an object, the AABB must be recomputed. You must transform all 8 corner points and find the new min/max. This can be optimized using the absolute values of the rotation matrix columns to avoid explicitly transforming all 8 corners.


6

Planes

A plane in 3D is analogous to an implicit line in 2D. It shares many properties and extends naturally to one more dimension.

ax + by + cz = d or equivalently P · n = d n = [a, b, c] = plane normal (often normalized, but not required) d = signed distance from origin to the plane in the direction of n When d > 0, origin is "behind" the plane (opposite side from n)

Defining a Plane from Three Points

Given three non-collinear points P₁, P₂, P₃ (in clockwise order for a left-handed system):

e₁ = P₂ - P₁ e₂ = P₃ - P₁ n̂ = normalize(e₁ × e₂) d = n̂ · P₁ (works for any of the three points)

"Best Fit" Plane from Many Points

When fitting a plane to a cloud of points:

  1. Process each point in clockwise order
  2. Accumulate edge cross products to find the best-fit normal
  3. Use the average position to compute d
The plane equation P · n = d gives you the signed distance from point P to the plane (when n is a unit vector). Positive means P is on the same side as n; negative means the other side. This is used heavily in frustum culling — testing which side of each frustum plane a bounding sphere falls on.

7

Exercises

1. Parametric Ray

Write the parametric ray for a projectile starting at P₀ = (0, 10, 0) and ending at Pend = (5, 0, 0). What is P(0.5)?

d = (5,-10,0). P(t) = (0,10,0) + t·(5,-10,0). P(0.5) = (2.5, 5, 0) — the midpoint of the trajectory.

2. Point Inside AABB

An AABB has Pmin = (0,0,0) and Pmax = (10,5,8). Is point P = (7, 3, 9) inside?

Check each axis: 0≤7≤10 ✓, 0≤3≤5 ✓, 0≤9≤8 ✗. The z-coordinate fails — point is outside.

3. Plane from Three Points

Find the plane equation for the triangle with vertices A=(1,0,0), B=(0,1,0), C=(0,0,1).

e₁ = B-A = (-1,1,0). e₂ = C-A = (-1,0,1). n = e₁×e₂ = (1·1-0·0, 0·(-1)-(-1)·1, (-1)·0-1·(-1)) = (1,1,1). n̂ = (1,1,1)/√3. d = n̂·A = 1/√3. Plane: x + y + z = 1 (before normalizing n).

Interview Question

What's the difference between an AABB and an OBB? When would you use each?

An AABB is always aligned to the world axes — cheap to test (6 comparisons for two AABBs) but can be very loose for rotated objects. An OBB is aligned to the object's local axes — tighter fit but requires the Separating Axis Theorem test (15 potential separation axes in 3D), which is more expensive. Use AABBs for broadphase and OBBs for narrowphase when precision matters.

Interview Question

A game needs to frustum-cull thousands of objects per frame. How would you use the plane equation to do this efficiently?

Represent each of the 6 frustum planes as (n̂, d). For each object, test its bounding sphere center C with radius r against each plane: if (C · n̂ - d) < -r for any plane, the sphere is fully outside and can be culled. If it's positive for all planes, it's inside. This requires just 6 dot products + comparisons per object — very cache-friendly.
← Chapter 8 ↑ Index Chapter 10 →