You can now test whether two objects collide — with
Spatial partitioning is the fix: organise objects by where they are so that each one is only ever tested against its near neighbours. It is the broad phase of collision detection — a cheap cull that throws out the overwhelming majority of pairs before the expensive exact test (the narrow phase) runs at all.
Step 1 — count the pairs. Testing every distinct pair among
For
Step 2 — the key observation: distant objects can't collide. Two objects far apart in space will never touch, so testing them is wasted work. If we could ask only “is anything near me?” we'd skip almost every pair. Partitioning answers exactly that — it sorts objects into regions of space, and an object is compared only with others in its own region (and adjacent ones).
Step 3 — a uniform grid. Lay a grid of cells over the world and drop each
object into the cell(s) it overlaps. To find collisions for an object, test only the objects
sharing its cell and the eight (in 2-D) neighbouring cells. With objects spread roughly evenly
across
Step 4 — a quadtree / octree adapts to the clumping. Where a grid uses one
fixed cell size everywhere, a quadtree (2-D) recursively splits a square into
four children only where it's crowded, and an octree does the same
with eight in 3-D. Empty regions stay one big cell; dense regions subdivide deep. The depth of
any object's leaf is
Step 5 — a bounding-volume hierarchy (BVH) for the objects themselves. A
BVH wraps each object in a simple bounding volume (an
Step 6 — collect the win. Each structure replaces the
A 4K frame has roughly eight million pixels, and a path tracer fires several rays per pixel,
each bouncing a few times — tens of millions of rays per frame. A scene has millions of
triangles. Testing every ray against every triangle is the
The BVH is the single data structure that makes it tractable. Each ray descends the hierarchy, rejecting whole branches whose bounding box it misses, and reaches only the handful of triangles it might actually hit — turning millions of triangle tests into a logarithmic walk. This is exactly why modern GPUs ship dedicated ray-tracing cores whose job is BVH traversal and box intersection in hardware: the maths of spatial partitioning, baked into silicon. The same hierarchy that lets a physics engine cull collision pairs in a big open world is what lets the GPU light it in real time.
A faster query is not automatically a faster frame. Every structure above also costs something to build and keep up to date: a quadtree or BVH has to be rebuilt (or repaired) whenever the objects inside it move, and for objects that shuffle every single frame that update cost can swamp whatever you saved on the query side.
So the right structure depends on how static vs dynamic the scene is, not on query speed alone. Mostly-still geometry (level walls, terrain) amortises a one-off BVH build over thousands of frames — nearly free per frame. A cloud of fast-moving particles or bullets rebuilt from scratch every tick can make the "smarter" tree slower in wall-clock time than a plain uniform grid, which is cheap to re-bucket every frame precisely because it has no hierarchy to repair. Pick the structure for the *churn* of the scene, not just its size.