The idea: test a few pairs, not all of them
Every frame a game must find the objects that touch. The exact test for two shapes can be slow, and the number of pairs grows fast: n objects make n(n − 1)/2 pairs. So engines split the work in two. A cheap broad phase throws away pairs that cannot touch and keeps a short list of candidate pairs. Only those go to the exact narrow phase.
A candidate is only a "maybe": two boxes can share a cell and still be apart. A broad phase may keep extra pairs, but it must never drop a pair that really touches. On this page every method finds the same 8 overlaps; they differ only in how many pairs they test. The exact test itself (boxes, circles, the separating axis theorem) is on Collision Detection: AABB, Circles and SAT.
The world is 16 × 12 units with 24 axis-aligned boxes, each 0.8 to 1.8 units wide and tall. The boxes come from a seeded random generator, so every run is the same. Two boxes overlap when their x ranges overlap and their y ranges overlap; touching edges do not count.
Brute force: every pair
Test box 0 against the 23 others, box 1 against 22 more, and so on: 23 + 22 + … + 1 = 24 × 23 / 2 = 276 tests. Only 8 pairs overlap, so 268 tests were wasted (Demo 1). With 48 boxes it would be 1,128 tests: doubling the objects makes about four times the work. Brute force is fine for a dozen objects and hopeless for a thousand.
Uniform grid
Cut the world into equal cells, here 4 × 4 units, which makes 4 × 3 = 12 cells. Each box goes into every cell it touches: one, two or four of them. Then only boxes in the same cell are paired. A cell with k boxes makes k(k − 1)/2 pairs.
- The 24 boxes make 35 cell entries. The busiest cell (x 4–8, y 4–8) holds 7 boxes, so 21 pairs.
- All cells together make 51 pairs. 4 of them are the same two boxes met again in a second cell. Skip those repeats (for example with a set of pair ids), and 47 pairs are tested. 8 of them overlap (Demo 2).
The cell size matters. Cells smaller than the boxes put each box in many cells. Cells much bigger than the boxes put many boxes in each cell. A common choice is a cell a little bigger than the largest object, as here (cells 4, boxes at most 1.8).
Sort and sweep on one axis
Sort and sweep (also called sweep and prune) looks at one axis, here x. Each box covers an interval [min x, max x]. Sort the boxes by their left edge and sweep a line from left to right, keeping an active list of the boxes whose interval has started and not yet ended.
- Take the next box in sorted order. Drop every active box whose right edge is at or before this box's left edge: those intervals are over.
- Pair the new box with every box still in the active list: their x intervals overlap.
- Add the new box to the active list.
Only the pairs that overlap on x come out: 39 of them. Checking y keeps the same 8 overlaps (Demo 3).
Why last frame's order helps
Sorting costs something too. The page keeps the sorted list from one frame to the next and re-sorts it with insertion sort (one of the sorts on Comparison Sorting), which needs one swap for every two boxes that are in the wrong order. The first sort starts from the boxes in id order and needs 118 swaps. Then Next Frame moves every box by at most 0.3 units, so the order barely changes: re-sorting takes 3 swaps (Demo 4). This is temporal coherence: things move little between frames, so last frame's answer is almost right. On that frame boxes 12 and 16 moved apart and boxes 5 and 6 started to overlap, so it is still 8 overlaps, from 44 candidates.
Each frame here is one fixed time step, like the fixed-step loop on Game Loop: Variable dt vs Fixed Timestep. With a fixed step the boxes move the same small amount every frame, which keeps the list nearly sorted.
Sort and sweep has a weak spot: when many boxes line up along the sweep axis (for example a tall stack, all at the same x), every pair overlaps on x and the method falls back to brute force. Engines then pick the axis with the most spread, or sweep on two axes.
Quadtree: small cells only where it is crowded
A quadtree starts with the whole world as one region. A region that holds more than 4 boxes splits into 4 equal quarters, and each quarter does the same, down to a maximum depth (3 here). A box that crosses a split line goes into both sides. Boxes are paired only inside the same leaf. For the spread scene this takes 6 splits and gives 19 leaves and 27 pairs to test, the fewest of the four methods (Demo 5). How a quadtree prunes a search for nearby points is on Quadtree vs Geohash; trees of bounding boxes are on KD-Tree vs R-Tree.
The worst case: a crowded cell
In the second scene, 12 of the 24 boxes are packed into the cell x 4–8, y 4–8. With 3 neighbours crossing in, that cell holds 15 boxes, and 15 × 14 / 2 = 105 pairs come from it alone. The grid tests 113 pairs in all; 20 of them really overlap (Demo 6). Inside one cell, a grid is brute force again.
The quadtree splits the crowded corner down to small leaves. It tests 53 pairs (63 in-leaf pairs minus 10 repeats) and finds the same 20 overlaps (Demo 7). The fullest leaf still holds 7 boxes because depth 3 is the limit.
Which one to use
| method | pairs tested (spread / crowded) | good at | weak at |
|---|---|---|---|
| brute force | 276 / 276 | a few objects; no set-up | grows as n² |
| uniform grid | 47 / 113 | many objects of similar size; cheap to update | crowds in one cell; very mixed sizes |
| sort and sweep (x) | 39 / 45 | objects that move a little each frame (few swaps) | many objects lined up on the sweep axis |
| quadtree | 27 / 53 | uneven crowds and empty space | rebuilding or updating the tree as objects move |
All four found the same overlaps in every case (Demo 8 fills the table for one frame). What changes is the work before the exact test, and the work to keep the structure up to date.
See also Model, View, Projection for how each object's model matrix places it in the world space these boxes live in.
What the page leaves out
Real engines run the broad phase on bounding boxes of any shape, often fattened by a margin so they need updating less often. They keep the overlap pairs between frames and only report pairs that start or stop touching. Physics engines such as Box2D use a dynamic AABB tree (a bounding volume hierarchy) instead of a quadtree. Hashed grids keep only the cells that hold something, which makes an unbounded world possible. The page also skips 3-D (octrees, sweeping on three axes), loose quadtrees, multi-threaded broad phases and the per-pair narrow-phase cost, which depends on the shapes. Turning shapes from model space into world space before any of this is left to other pages.