Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Collision detection

After this lesson: I can test whether two boxes or two circles overlap.

60 min lesson1 commandGame development lab: a game loop, gravity and collisions in the browser

A game finds out when two things touch

Collision detection is how a game finds out that two things touch. It decides whether a star is caught, a bullet hits or a player walks into a wall.

Games test simple shapes, boxes and circles, because a screen can hold hundreds of things and every pair may need checking on every frame.

By the end of this lesson

Note

I can test whether two boxes or two circles overlap.

Key words

WordWhat it means
Collisiontwo things touching or overlapping
Bounding boxthe smallest rectangle that holds a shape
Overlapsharing some of the same space
Radiusthe distance from the centre of a circle to its edge
Hitboxthe shape a game uses to test for a collision

The cells students fill in are left blank in this preview.

Boxes overlap when both ranges overlap

A box has a position (x, y), a width w and a height h. Its x range runs from x to x + w. Its y range runs from y to y + h.

Two boxes overlap when their x ranges overlap and their y ranges overlap. Two ranges overlap when each one starts before the other one ends.

Worked example: four pairs of boxes

Box A (x, y, w, h)Box B (x, y, w, h)Overlap?Why
0, 0, 10, 105, 5, 10, 10YesBoth ranges overlap
0, 0, 10, 1020, 0, 10, 10NoA ends at 10, B starts at 20
0, 0, 10, 1010, 0, 10, 10NoThey only share an edge
0, 0, 10, 100, 30, 10, 10NoSame x range, but y is far apart

The cells students fill in are left blank in this preview.

Try it: test two boxes in JavaScript

Code: web
<pre id="out"></pre>

Run the lab's overlap test on six pairs

Command
docker exec jupyter python3 steps/collide.py

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The lab script tests six fixed pairs

The script runs the same test the game uses, written in Python, on six pairs of boxes. It prints a name and True or False for each.

Apart, edges touching and same column far apart print False. Overlapping, one inside the other and one corner inside print True. A box that only touches an edge is not a collision, because the test uses strict signs.

Two circles overlap when the centres are close

A circle has a centre (x, y) and a radius r. Two circles overlap when the distance between the centres is less than the two radii added together.

Finding a distance needs a square root, which is slow. So games compare squares instead: dx * dx + dy * dy is less than (r1 + r2) * (r1 + r2). Here dx and dy are the gaps between the centres.

Worked example: three pairs of circles

Circle A (x, y, r)Circle B (x, y, r)dx, dySquares comparedOverlap?
0, 0, 58, 0, 58, 064 < 100Yes
0, 0, 510, 0, 510, 0100 < 100 is falseNo
0, 0, 23, 4, 33, 425 < 25 is falseNo

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people check only one direction. If two boxes share the same x range, they only collide when their y ranges overlap too. Test both directions, and join the two tests with and. One failed test means no collision.

About this lab and its source: Game development lab: a game loop, gravity and collisions in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.