Lesson 11: The Big Game - Collision Detection

Overview

To finish up their video games, students will apply what they have learned in the last few stages to write the final missing functions. We'll start by using booleans to check whether keys were pressed in order to move the player sprite, then move on to applying the Pythagorean Theorem to determine when sprites are touching.

Agenda

Getting Started

Activity

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.

Objectives

Students will be able to:

  • Apply the Distance Formula to detect when two points on a coordinate plane are near each other.

Links

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

For the Students

Support

Report a Bug

Teaching Guide

Getting Started

Introduction

Let's get back into that Big Game from stages 7, 12, and 16.

Previous work with the game has created movement for the danger and target characters, using Booleans to check if they have left the screen. The last time students worked on their game they used a conditional to check which key was pressed and make the player move accordingly. At this point the only thing left to do is to decide when the player is touching either the target or danger. Once students have successfully completed the distance and collide? functions, their score will increase when the player touches the target, and decrease when it touches the danger.

The Pythagorean Theorem studied in the last lesson will be used to determine when the characters have made contact. Students are not required to write their own line-length function, but you may ask them to complete the Design Recipe for it anyway.

Students will first complete the distance function so that it measures the distance between two points, (px, py) and (cx, cy). After the students implement the distance formula, they will need to implement the tests in the collide? function.

Once these last functions are put into place, scoring will automatically update based on collisions between target and danger.

Activity

Online Puzzles

Return to your Big Game to use collision detection logic so that you know when your player is touching the target or the danger. Head to Course B Stage 11 in Code Studio to get started programming.

View on Code Studio

Student Instructions

At this point you've programmed a video game with a moving Target and Danger, both of which return to the screen once they've left, and a Player that responds to keyboard input. Click "Run" to see how your game is currently working, and then we'll dig into the final missing piece - collision detection!

View on Code Studio

Student Instructions

Let's fix this distance function so our collision detection works properly.

Use the Distance Formula, so the distance function returns the distance between the player's center coordinates (px, py) and another sprite's center coordinates (cx, cy).

We've provided a working line-length function to help you out.

View on Code Studio

Student Instructions

The distance function returns the distance between two sprites.

Complete the collide? function so that it returns true if the distance between two points is less than 100 pixels apart.

If you're successful, your sprites will instantly collide - why do you think that is?

View on Code Studio

Student Instructions

You made it!

At this point your game should be fully functioning, take some time to enjoy your new game and make any final tweaks to it.

When you're happy with the final product, click Finish and you'll have an opportunity to share a link to your game.

Good work!

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.