The Distance Formula
@BootstrapWorld
Distance in 1 Dimension
Sign in to code.pyret.org (CPO) and open your saved Game Starter File.
At this point:
It's almost fully-playable!
Distance in 1 Dimension
Distance in 1 Dimension
Role-Play: Two Characters Collide!
Distance in 1 Dimension
Role-Play: Two Characters Collide!
Distance in 1 Dimension
Our game computes 1-dimensional distance (vertical or horizontal) using a function called line-length. Let's explore how it works!
Distance in 1 Dimension
Distance in 1 Dimension
line-length computes the positive distance between two points on a single number line.
If two coordinates differ by their x-values, this function will tell us how far apart they are.
If two coordinates differ by their y-values, this function will tell us how far apart they are.
But what if the coordinates differ by both x and y...?
Distance in 2 Dimensions
Scroll down to 4. Collisions in your game file and look for the distances-color definition.
Right now this value is defined to be the empty string "". Change this to a color that will show up on your background, and click Run.
This setting will draw lines from your Player to each of the other characters, and then uses those lines as the hypotenuse of right triangles!
The legs of these triangles show the distance in 1 dimension each (on the x- and y-axis).
Distance in 2 Dimensions
In order to compute the diagonal distance between two characters in a video game, we'll need a special formula that considers both the vertical and the horizontal distances between them!
When we turned on distances-color in our game, we saw the diagonal distance between two characters represented as the hypotenuse of a right triangle.
Distance in 2 Dimensions
Connecting Pythagorean Theorem to video games:
On the next slide you'll watch a video created by Tovah Brown of a geometric proof of the Pythagorean theorem, and its application to finding distance between game characters.
Distance in 2 Dimensions
Distance in 2 Dimensions
Distance in 2 Dimensions
For more practice writing code to generate the distance between two fixed points, complete Distance From Game Coordinates
Optional: for more practice, complete Distance From Game Coordinates 2
Distance in 2 Dimensions
All of the practice we've done so far today focused on a screenshot of a moment in time. With the game stopped in that moment, we knew either the exact location of our characters or the exact distances between them.
But, as we play our games, the characters are constantly changing locations!
In order to calculate the distance between two objects whose locations are constantly changing, we need to use variables!
Distance in 2 Dimensions
Turn to Distance (px, py) to (cx, cy) and use the Design Recipe to help you write a function that takes in two coordinate pairs (four numbers) of two characters (px, py) and (cx, cy) and returns the distance between those two points.
HINT: The code you wrote in Circles of Evaluation: Distance between (0, 2) and (4, 5) can be used to give you your first example!
When you're done, fix the broken distance function in your game file, click Run and check that the right triangles in your file now appear with reasonable distances for the hypotenuse.
Distance in 2 Dimensions
Optional:
If we knew the lengths of the hypotenuse and one leg of the triangle, could we use the formula A2 + B2 = C2 to compute the length of the other leg?
Take a look at the two examples on Comparing Code: Finding Missing Distances.
Additional Exercises