Lesson 10: Collision Detection and the Pythagorean Theorem


Determining when objects on the screen touch is an important aspect of most games. In this lesson we'll look at how the Pythagorean Theorem and the Distance Formula can be used to measure the distance between two points on the plane, and then decide whether those two points (or game characters) are touching.


Getting Started


View on Code Studio

Anchor Standard

Common Core Math Standards

  • 8.G.7 - Apply the Pythagorean Theorem to determine unknown side lengths in right triangles in real-world and mathematical problems in two and three dimensions.


Students will be able to:

  • Demonstrate that circles will overlap if the distance between their centers is less than the sum of their radii.
  • Show that the distance of two points graphed in 2 dimensions can be represented as the hypotenuse of a right triangle.
  • Understand that the Pythagorean Theorem allows you to calculate the hypotenuse of a right triangle using the length of the two legs.
  • Apply the Pythagorean Theorem to calculate the distance between the centers of two objects.


Heads Up! Please make a copy of any documents you plan to share with students.

For the Students


Report a Bug

Teaching Guide

Getting Started

Are they Touching?

Suppose two objects are moving through space, each one having its own (x,y) coordinates. When do their edges start to overlap? They certainly overlap if their coordinates are identical (x1 = x2, y1 = y2), but what if their coordinates are separated by a small distance? Just how small does that distance need to be before their edges touch?

Visual aids are key here: be sure to diagram this on the board!

In one dimension, it’s easy to calculate when two objects overlap. In this example, the red circle has a radius of 1, and the blue circle has a radius of 1.5 The circles will overlap if the distance between their centers is less than the sum of their radii (1 + 1.5 = 2.5). How is the distance between their centers calculated? In this example, their centers are 3 units apart, because 4 − 1 = 3.

Prompt the class: Would the distance between them change if the circles swapped places? Why or why not?

Work through a number of examples, using a number line on the board and asking students how they calculate the distance between the points. Having students act this out can also work well: draw a number line, have two students stand at different points on the line, using their arms or cutouts to give objects of different sizes. Move students along the number line until they touch, then compute the distance on the number line.

Your game file provides a function called line-length that computes the difference between two points on a number line. Specifically, line-length takes two numbers as input and determines the distance between them

Prompt the students:

What answers would you expect from each of the following two uses of line-length:

  • line-length(2, 5)
  • line-length(5, 2)

Do you expect the same answer regardless of whether the larger or smaller input goes first?

Unfortunately, line-length can only calculate the distance between points in a single dimension (x or y). How would the distance be calculated between objects moving in 2-dimensions (like your game elements)? line-length can calculate the vertical and horizontal lines in the graphic shown here, using the distance between the x-coordinates and the distance between the y-coordinates. Unfortunately, it doesn’t tell us how far apart the two centers are.

Drawing a line from the center of one object to the other creates a right-triangle, with sides A, B and C. A and B are the vertical and horizontal distances, with C being the distance between the two coordinates. line-length can be used to calculate A and B, but how can we calculate C?

In a right triangle, the side opposite the 90-degree angle is called the hypotenuse. Thinking back to our collision detection, we know that the objects will collide if the hypotenuse is less than the sum of their radii. Knowing the length of the hypotenuse will be essential to determine when a collision occurs.


Proving Pythagoras

If your students are new to the Pythagorean Theorem, or are in need of a refresher, this activity is an opportunity to strengthen their understanding in a hands-on fashion.

Organize students into small groups of 2 or 3.

  • Pass out Pythagorean Proof materials ( 1, 2 ) to each group.
  • Have students cut out the four triangles and one square on first sheet.
  • Explain that, for any right triangle, it is possible to draw a picture where the hypotenuse is used for all four sides of a square.
  • Have students lay out their gray triangles onto the white square, as show in this diagram.
  • Point out that the square itself has four identical sides of length C, which are the hypotenuses for the triangles. If the area of a square is expressed by side ∗ side, then the area of the white space is C2.
  • Have students measure the inner square formed by the four hypotenuses (C)

By moving the gray triangles, it is possible to create two rectangles that fit inside the original square. While the space taken up by the triangles has shifted, it hasn’t gotten any bigger or smaller. Likewise, the white space has been broken into two smaller squares, but in total it remains the same size. By using the side-lengths A and B, one can calculate the area of the two squares.

  • You may need to explicitly point out that the side-lengths of the triangles can be used as the side-lengths of the squares.
  • Have students measure the area of the smaller square (A)
  • Have students measure the area of the larger square (B)
  • Ask students to compare the are of square A + square B to the area of square C

The smaller square has an area of A2, and the larger square has an area of B2. Since these squares are just the original square broken up into two pieces, we know that the sum of these areas must be equal to the area of the original square:

A2 + B2 = C2

Collision Detection

In this activity students will:

  • Create right triangles on a graph.
  • Calculate the hypotenuse by direct measurement and by the Pythagorean Theorem.
  • Determine if circles have collided by examining visually.
  • Determine if circles have collided by comparing distance and radii.

Detailed instructions are provided on the Detecting Collisions - Worksheet.

  • Levels
  • 1
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Standards Alignment

View full course alignment

Common Core Math Standards

EE - Expressions And Equations
  • 7.EE.4 - Use variables to represent quantities in a real-world or mathematical problem, and construct simple equations and inequalities to solve problems by reasoning about the quantities.
  • 8.EE.2 - Use square root and cube root symbols to represent solutions to equations of the form x2 = p and x3 = p, where p is a positive rational number. Evaluate square roots of small perfect squares and cube roots of small perfect cubes. Know that √2 is irrationa
F - Functions
  • 8.F.1 - Understand that a function is a rule that assigns to each input exactly one output. The graph of a function is the set of ordered pairs consisting of an input and the corresponding output.1
  • 8.F.2 - Compare properties of two functions each represented in a different way (algebraically, graphically, numerically in tables, or by verbal descriptions). For example, given a linear function represented by a table of values and a linear function represented
G - Geometry
  • 8.G.7 - Apply the Pythagorean Theorem to determine unknown side lengths in right triangles in real-world and mathematical problems in two and three dimensions.
  • 8.G.8 - Apply the Pythagorean Theorem to find the distance between two points in a coordinate system.
MP - Math Practices
  • MP.1 - Make sense of problems and persevere in solving them
  • MP.2 - Reason abstractly and quantitatively
  • MP.3 - Construct viable arguments and critique the reasoning of others
  • MP.4 - Model with mathematics
  • MP.5 - Use appropriate tools strategically
  • MP.6 - Attend to precision
  • MP.7 - Look for and make use of structure
  • MP.8 - Look for and express regularity in repeated reasoning
NS - The Number System
  • 6.NS.8 - Solve real-world and mathematical problems by graphing points in all four quadrants of the coordinate plane. Include use of coordinates and absolute value to find distances between points with the same first coordinate or the same second coordinate.
Q - Quantities
  • N.Q.1 - Use units as a way to understand problems and to guide the solution of multi-step problems; choose and interpret units consistently in formulas; choose and interpret the scale and the origin in graphs and data displays.
  • N.Q.2 - Define appropriate quantities for the purpose of descriptive modeling.