Game Development Reference
In-Depth Information
13 - Sonar Treasure Hunt
Figure 13-5: The places on the board that [[5, 0], [0, 2], [4, 2]] represents.
We figure out the distance from the sonar device located at 0, 2 with "rings" and the
distances around it:
Figure 13-6: The board marked with distances from the 0, 2 position.
But how do we translate this into code for our game? We need a way to represent
distance as an expression. Notice that the distance from an XY coordinate is always the
larger of two values: the absolute value of the difference of the two X coordinates and the
absolute value of the difference of the two Y coordinates.
That means we should subtract the sonar device's X coordinate and a treasure chest's X
coordinate, and then take the absolute value of this number. We do the same for the sonar
device's Y coordinate and a treasure chest's Y coordinate. The larger of these two values is
the distance. Let's look at our example board with rings above to see if this algorithm is
correct.
Search WWH ::




Custom Search