Game Development Reference
In-Depth Information
All you need to do is update the code with this new position:
1. Add the following code in bold text to the eb statement you just wrote:
eb\$dehh*depPaopLkejp\$lh]uan*t(lh]uan*u 'lh]uan*daecdp+. (pnqa%%
w
lh]uan*t)9rt7
lh]uan*u)9ru7
y
2. Save the I]ej[Lh]ucnkqj`*]o file and test the
project. You'll now be able to move the lh]uan
object up to the very top of the hill, as shown in
Figure 7-33.
This is an illustration of an approach you can use for
shape-based collision detection, but it will almost cer-
tainly require a bit more fine-tuning before you use it
in any of your games. Each game you work on will pres-
ent its own set of problems, but I outlined the starting
points to solving many of them in these simple examples.
(You can find the complete code for this section in the
I]ej[Lh]ucnkqj`[3*]o file.)
Figure 7-33. The player object is now free to
move all the way to the top of the hill.
The one flaw in this technique is that it's not pixel-perfect. You don't have a way to figure out
exactly where the boundary of the hill lies. You only know that it's somewhere within the range of
5 pixels that's defined by the lh]uan object's velocity. If you need greater precision, you'll have to
investigate some more-advanced techniques that are just outside the scope of this topic. For the
adventurous among you, here are some strategies you can try.
You can use a bkn loop and some vector math to test each point along the player's trajectory to find
the edge of the shape and move the player to that spot. This technique is called multisampling .
A simpler way is to use a sdeha loop to push the lh]uan object back by one pixel until it's touching
the hill. The basic code for using a sdeha loop in the context of the present example looks like this:
sdeha\$dehh*depPaopLkejp\$lh]uan*t(lh]uan*u(pnqa%%
w
lh]uan*u''7
y
This will work if the player is moving in one direction: up. You can make it work for all directions if you
combine it with some of the techniques you'll be looking at toward the end of this chapter. (I'll cover
bkn and sdeha loops in detail in the chapters ahead. Come back to this section of the chapter when
you think you are ready for the challenge.)