Students act out a collision in their game, and reason about the mathematical behavior of collision detection
Students learn how to compute the distance between objects in one dimension
Students will be able to explain how a Number line is used to calculate distance in one dimension
Students will be able to explain why the line-length function uses a conditional
Computer for each student (or pair), running WeScheme or DrRacket with the bootstrap-teachpack installed
Student workbooks, and something to write with
Class poster (List of rules, language table, course calendar)
Students are logged into WeScheme.org, OR have opened DrRacket.
1D DistanceSuppose 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?
[Video] 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 (). How is the distance between their centers calculated? In this example, their centers are 3 units apart, because 4 - 1 = 3.
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. The first few seconds of our Bootstrap video show this exercise in action.
- 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.
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?
If you have time and want to reinforce how conditionals arise from examples, you can have students fill in blanks in Examples such as (EXAMPLE (line-length 2 5) ___), circle what’s different, and notice that the circle labels are in different orders depending on whether the first or the second input is larger. This in turn suggests that the code for line-length will need a conditional. In this case, one could avoid the conditional by taking the absolute value of the difference (the function abs does this); if you are working with older students who already know about absolute value you could show it. Using cond, however, emphasizes how code structure arises from examples.
- Scroll to the line-length and collide? functions in your game file. Notice that line-length uses a conditional so that it subtracts the smaller number from the bigger one.
Can you explain why line-length needs to use cond? What are the two conditions?
The two conditions are:
A is less than B
B is less than or equal to A
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?
Students’ gamefiles all have a value called *distances-color*, which is set to the empty string "". By changing this to a color such as "yellow" or "red", the game will draw right triangles between each game character, and fill in the lengths for each side. You may want to demonstrate this using your own game file, and have the students follow along. Hint: to make it as easy as possible to see these triangles, set your background to be a simple, black rectangle and slow down the animation functions.
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.