hr-studioPhysics · 03
A computational design primer · Collision & Contact

Before anything can bounce, something has to notice.

Every simulation of solid things rests on a quiet question asked millions of times a second: are these two overlapping, and if so, which way do I push them apart? This is collision detection — the distance test, the contact normal, the penetration depth, and the trick that lets you ask it for thousands of objects without checking every pair. Drag anything.

Scroll to begin
i.

The distance test

Give each object a collision radius and the test is almost embarrassingly simple: two circles touch exactly when the distance between their centres is less than the sum of their radii. Grow one circle's radius by the other's and the question becomes "is that centre inside this ring?" — a single distance, one comparison.

Drag the circles together and apart.

d < r₁+r₂ means contact
ii.

Normal and penetration

Knowing they overlap isn't enough; a solver needs to know which way and how much. Both fall out of the same line of centres: the contact normal is the unit vector along it, and the penetration depth is how much the radii overlap, r₁+r₂ − d. That's the whole contact — a direction to separate and a distance to travel.

Overlap the circles and watch the normal and depth.

Normal = push direction · depth = overlap
iii.

Pushing them apart

Now do something about it. Move each overlapping pair back along the normal by the penetration — split between them so the lighter one moves more — and the overlap vanishes. Apply it to every pair, a few times over, and a jammed heap relaxes until nothing intersects. This positional fix is the backbone of packing, granular piles, and stable stacks.

Drag one into the others, or scatter them and watch them separate.

Every frame: separate the overlaps
iv.

Not checking everyone

Testing every pair is O(n²) — a hundred objects is five thousand tests, a thousand is half a million. The fix is a broad phase: drop objects into a grid of cells sized to the largest one, and only test neighbours sharing a cell or an adjacent one. Distant objects are never compared at all, so the real work collapses to a tiny fraction — and it never misses a genuine contact.

Add objects and watch the two counts diverge.

Grid buckets · only neighbours tested
v.

The collision playground

Everything together: a box of bodies falling, colliding, transferring momentum and separating — all detected through the grid, so the check counter stays low even as the crowd grows. Drop an obstacle in the middle, tune the count, size and bounce, and turn on the grid to see the broad phase at work. This is the engine under packing, granular flow, and any crowd of solid things.

Tap to drop · drag to fling · drag the obstacle ◉
A radius, a distance, a normal, a nudge apart — repeated smartly so you never compare things that could never touch. Detection is the unglamorous half of physics, and everything visible depends on it.