Game Development Reference
In fact, you can even skip this section of the chapter and simply go on using the ?khheoekj*
lh]uan=j`Lh]pbkni method as a utility without ever worrying about how it works. It's presented only
so that you can see the underlying collision system involved. Also, this code is tailored specifically for
this project, so feel free to change it as much as you like for your own games. With time, you might
want to revisit this code when you understand it a bit better and then rewrite it so it's more general
and easier to adapt to a wider range of situations. In fact, I hope you'll eventually do this. General
code tends to be very complex, so the present version is written as a learning tool to be as simple as
possible while still retaining a good degree of flexibility for many different kinds of games.
To use this code in your games, you need the following:
1. The first object involved in the collision, lh]uan, needs publicly accessible rt and ru proper-
ties. The rt and ru getters and setters in the Lh]uan[Lh]pbkni class accommodate for this.
2. The lh]uan object optionally needs publicly accessible ^kqj_aT and ^kqj_aU properties if you
want it to be able to bounce off platforms.
3. If the lh]uan object has a subobject assigned through a publicly accessible _khheoekj=na]
property, that subobject is used as the collision object. In Bug Catcher, the cat's ^k`u subobject
was assigned to the _khheoekj=na] property. It's available to the code through the Lh]uan[
Lh]pbkni class's getter.
4. If you're programming a platform game and need to know whether the lh]uan is on top of any
of the platforms, the code will look for a publicly accessible property called eoKjCnkqj` and
set it to pnqa.
I won't discuss this code in detail, but I'll give you a general map of how it works. Each section of the
code contains comments that explain what it does. I'll leave it up to you to check the specifics on
the code and put all the pieces together. You won't understand all the code immediately, but it's some-
thing you can come back to as your programming experience increases over the weeks and months
ahead. If you feel a bit overwhelmed when you first look at it, don't let it scare you. It's a lot of code, but
you've actually seen all of it in different contexts before. The only really new things are the three formu-
las used to calculate bounce and friction, and you'll be looking at them in the pages ahead.
Here's how the lh]uan=j`Lh]pbkni method works:
It accepts four parameters:
The first object involved in the collision (the lh]uan object)
The second object (the lh]pbkni)
A bounce value
A friction value
If the lh]uan object has a _khheoekj=na] subobject assigned, its se`pd and daecdp are used to
define the lh]uan collision area. Its t and u coordinates are also converted to global coordi-
nates using the hk_]hPkChk^]h method.
The code checks for a collision on each axis; if it finds one, it determines which side of the
lh]pbkni the collision is occurring on. This is exactly the same system used in the ?khheoekj*
^hk_g method. After it finishes, it separates the lh]uan from the collision with the lh]pbkni
and applies bounce and friction forces.