Collision Detection Fundamentals
How games know when things touch — AABB and circle checks, why broad vs. narrow phase matters, and resolving overlaps.
Collision detection answers a question games ask constantly: are these two things touching? It's behind every wall you can't walk through, every bullet that connects, every coin you pick up. The fundamentals are simpler than they look.
The cheapest check: AABB
An Axis-Aligned Bounding Box (AABB) is a rectangle that doesn't rotate. Two AABBs overlap only if they overlap on both the X axis and the Y axis — if there's a gap on either axis, they miss. That's the whole test, and it's extremely fast, which is why AABBs are the default hitbox for most 2D games.
Circles
For round things, a circle vs. circle check is even simpler: they touch if the distance between their centers is less than the sum of their radii. (Compare squared distances to skip the square root and keep it fast.) Circles are great for characters and projectiles where corners would feel wrong.
Broad phase vs. narrow phase
Checking every object against every other is n² — it explodes as your game grows. Real engines split it:
- Broad phase — quickly rule out pairs that can't be touching (using a grid or spatial partition), so you only test nearby candidates.
- Narrow phase — do the precise AABB/circle/pixel check on the handful of pairs that survived.
You don't need this on day one, but knowing the split explains why "just check everything" stops scaling.
Detecting vs. resolving
Detecting a collision is only half the job. Resolution decides what happens next:
- Blocking — push the character back out so they can't pass through a wall (compute the overlap and move them by it).
- Trigger — just fire an event (picked up a coin, entered a zone) without stopping movement.
Deciding whether a collision blocks or merely notifies is a design choice you make per object type.
Tunneling: the classic bug
A fast object can move so far in one frame that it skips over a thin wall entirely — it was in front one frame and behind the next, never overlapping. This is tunneling. Fixes include capping speed, using thicker colliders, or casting a ray/shape along the movement path (see the raycasting guide).
Mental model
Most 2D collision is just rectangles and circles overlapping, filtered so you only test things that are near each other, then resolved by either pushing apart or firing an event.