Game Dev 101
Progress
0/22

Lesson 9 of 22, in world 3: Physics & Collisions

Collision Detection

Time
About 20 min
Builds on
VectorsCoordinates & Transforms

Did the bullet hit the enemy? Is the player standing on the floor? Did Pac-Man eat the dot? Answering “are these two shapes overlapping?” every frame is collision detection. The trick is to use simple shapes — boxes and circles — instead of the actual pixels.

Hitboxes

A character sprite might be a detailed drawing, but for collisions we approximate it with a simple invisible shape: its hitbox (or collider). Hitboxes are often a little smaller than the sprite — it feels fair when bullets narrowly miss you.

The three tests you need

LiveOverlap tests
Shapes

Move the mouse to drag shape A over shape B. Shapes turn pink when the test says they overlap. Dashed lines show the numbers each test compares.

JavaScript
// 1. Axis-Aligned Bounding Boxes (rectangles that don't rotate)
function aabb(a, b) {
  return a.x < b.x + b.w && a.x + a.w > b.x &&   // x ranges overlap
         a.y < b.y + b.h && a.y + a.h > b.y      // y ranges overlap
}

// 2. Circles: closer than the sum of their radii?
function circles(a, b) {
  const dx = a.x - b.x, dy = a.y - b.y
  return dx * dx + dy * dy < (a.r + b.r) ** 2     // compare squared: no sqrt needed
}

// 3. Circle vs rectangle: find the closest point on the rect, then compare to the radius
function circleRect(c, r) {
  const px = clamp(c.x, r.x, r.x + r.w), py = clamp(c.y, r.y, r.y + r.h)
  return (c.x - px) ** 2 + (c.y - py) ** 2 < c.r * c.r
}

Broad phase and narrow phase

With 400 objects, checking every pair is ~80,000 tests per frame. Real games split the work: a cheap broad phase (spatial grid, quadtree) finds pairs that are near each other, then the exact narrow phase test runs only on those.

LiveBroad phase: don't check every pair

Checking every object against every other is n·(n−1)/2 tests. A spatial grid only compares objects in the same or neighbouring cells. Compare the numbers as you add balls.

Detection is only half the story

Knowing two things overlap doesn’t tell you what to do. A coin should disappear (a trigger), but a wall should push you back out (a solid). That’s collision response, the next lesson.

Key takeaways

  • Collide simple hitboxes (rects, circles), not pixels.
  • AABB: x and y ranges both overlap. Circles: distance < sum of radii. Circle-rect: clamp to the closest point.
  • Compare squared distances to avoid sqrt.
  • Use a broad phase (spatial grid) when you have many objects.

Check yourself

Q1. Two circles: centres 50 px apart, radii 20 and 25. Do they overlap?

Q2. Why compare squared distances?

Q3. AABB stands for…

Exercises

  1. Write a pointInRect(px, py, rect) function. Where would a game use it? (Hint: mouse clicks on buttons.)
  2. In the broad-phase demo, how many checks does the grid save at 400 balls? What happens if CELL is much larger than the balls?
  3. Sketch a hitbox for a tall character with a sword. Should the sword be part of the body hitbox?