Collision Detection
Check whether rectangles overlap with the bounding box test, whether circles touch using distance, and decide what happens after a hit.
Welcome back!
Step 1 of 9Learn
When do two boxes touch?
Collecting a coin, getting hit by an enemy, landing on a platform: all of these start with the same question, "Are these two things touching?" Checking every pixel of two shapes would be slow, so games usually wrap each object in an invisible rectangle called a bounding box and test whether the boxes overlap.
The trick is to think about when two rectangles don't overlap. Box A is completely separate from box B if A is entirely to the left of B, entirely to the right, entirely above or entirely below. If none of those is true, they must overlap. Flipped around, that gives the famous AABB test (axis-aligned bounding boxes):
function hits(a, b) {
return a.x < b.x + b.width &&
a.x + a.width > b.x &&
a.y < b.y + b.height &&
a.y + a.height > b.y;
}Line by line: A's left edge is left of B's right edge, A's right edge is right of B's left edge, and the same two checks vertically. All four must be true, which is why they're joined with &&.
This works for any objects drawn with fillRect, since their x and y are the top-left corner. It's fast enough to check hundreds of pairs every frame.
Step 2 of 9Practice · Sort into groups
Box A is at x 0, y 0, 50 wide and 50 tall. Do these boxes B overlap with A? (All B boxes are 20 by 20.)
Tap an item, then tap its group (or drag it there).
Step 3 of 9Learn
Circles and distance
For round things such as balls, bubbles and asteroids, boxes feel unfair: two circles can be "hit" by their box corners while visibly apart. Circle collisions use distance instead.
Two circles touch when the distance between their centers is less than the sum of their radii. The distance comes from the Pythagorean theorem: if the centers are dx apart horizontally and dy apart vertically, the distance is the square root of dx² + dy².
function circlesHit(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
const dist = Math.sqrt(dx * dx + dy * dy);
return dist < a.r + b.r;
}Example: one coin at (100, 100) with radius 10, the player at (124, 110) with radius 20. Then dx = 24, dy = 10, and the distance is the square root of 576 + 100 = 676, which is exactly 26. The radii add up to 30, and 26 is less than 30, so they collide.
A speed tip: square roots are slower than multiplication, so many games skip the square root and compare squared values, dx * dx + dy * dy < (a.r + b.r) * (a.r + b.r). The answer is the same because both sides are positive.
Step 4 of 9Try the calculator
Circle collision checker
Enter two circles' centers and radii to see the distance between centers and whether they overlap (1 means hit, 0 means miss).
- Distance between centers
- 26 px
- Sum of radii
- 30 px
- Hit (1) or miss (0)
- 1
Step 5 of 9Practice · Work it out
A ball of radius 8 is centered at (30, 40). A bumper of radius 12 is centered at (60, 80). What is the distance between their centers in pixels?
Step 6 of 9Learn
What happens after a hit
Detecting a collision is half the job. The other half is the response, and getting it wrong causes some of the funniest game bugs.
Collect and remove. When the player touches a coin, add points and remove the coin, often by marking it collected = true and filtering it out. If you forget to remove it, the player collects it again on the next frame, and the next, earning 600 points per second for standing still.
Damage with invincibility. When an enemy touches the player, the overlap lasts several frames. Subtract a life on every one of those frames and a single brush takes all three lives instantly. The usual fix is a short invincibility timer: after a hit, ignore further hits for, say, 60 frames (one second) and make the player blink so it's visible.
Solid objects. For walls and platforms, you push the player back out of the overlap, then set the velocity on that axis to zero. Landing on a platform means moving the player up so its bottom sits exactly on the platform's top.
A good habit: write the response as a small function, like collectCoin(coin) or hurtPlayer(), and call it from the collision check. That keeps the detection code clean and the rules easy to change.
Step 7 of 9Practice · Diagram
Detecting a hit is half the job
Which response stops one brush with an enemy from taking all three lives?
Tap the shape that answers the question, then press Check.
- Collision detected
- Coin
- Add points
- Remove the coin
Forget this and the player scores every frame while standing still.
- Enemy
- Lose one life
- Start invincibility timer
Ignore hits for a short time, or one touch takes every life.
Step 8 of 9Practice · What would you do?
What would you do?
In Mia's game, touching an enemy once makes the player lose all three lives immediately. Her code checks hits(player, enemy) every frame and subtracts a life whenever it's true. What's the best fix?
Step 9 of 9Practice · Code challenge
Write hits(a, b) that returns true if two rectangles overlap and false otherwise. Each rectangle is an object with x, y, width and height, where x and y are the top-left corner. Rectangles that only touch at an edge do not count as overlapping.
hits({x:0,y:0,width:50,height:50}, {x:40,y:40,width:20,height:20})hits({x:0,y:0,width:50,height:50}, {x:60,y:10,width:20,height:20})hits({x:0,y:0,width:50,height:50}, {x:10,y:55,width:20,height:20})hits({x:100,y:100,width:10,height:10}, {x:0,y:0,width:20,height:20})hits({x:0,y:0,width:50,height:50}, {x:50,y:0,width:20,height:20})
Lesson complete!