Game Development Reference
Here's the basic model for using it:
?khheoekj*lh]uan=j`Lh]pbkni$ lh]uan ( lh]pbkni ( ^kqj_aR]hqa ( ±
Let's say you want to set up a collision between the lh]uan object and lh]pbkni,. You want to give
the collision a bounce value of ,*2 and a bit of friction. Friction is calculated in a different way in this
method, so a low friction would be a value of ,*-. The method might look something like this:
To use the ?khheoekj*lh]uan=j`Lh]pbkni method with your games, you need to be aware of the
The ?khheoekj class must be in the same directory as the rest of the code or imported into a
class using the import statement.
The first object in the argument, lh]uan, has to have [rt and [ru properties that are accessible
by getters and setters called rt and ru. Have a look at the Lh]uan[Lh]pbkni class to see how it
is implemented. If the object doesn't have these properties assigned, the method will try to
create them dynamically, although you won't have much control over how the collision behaves.
It's usually better to plan for this and establish the [rt and [ru properties in the object's class,
like you have in Lh]uan[Lh]pbkni.
If you want the lh]uan to bounce when the collision occurs, it needs to have properties called
[^kqj_aT and [^kqj_aU that are accessible by getters and setters called ^kqj_aT and ^kqj_aU.
If your object doesn't have these properties, the blocking feature of the method still works, but
the bounce effect doesn't. This is implemented in the Lh]uan[Lh]pbkni class, so check out the
code if you're unsure about how to do this.
You can assign a subobject to be used as the collision area. If the lh]uan contains a property
called [_khheoekj=na] that's accessible by a getter called _khheoekj=na], that object will be
used as the collision object. If you don't have a [_khheoekj=na] property assigned, the entire
object is used in the collision detection.
The method has been written as an easy-to-use utility that you'll find a use for in countless game
projects. As such, you don't need to know how it works; just how to implement in your games. For the
curious, however, you'll be taking a detailed look at its inner workings at the end of this chapter. Take
a sneak peek if you want to!
But without much further ado, let's put this new collision method to work for you in the game-to-be:
1. Open the project's I]ej[>qc?]p_dan class by double-clicking it in the Project panel. When it
opens, you'll see that the basic framework of the class has already been written for you, so you
can quickly get to the fun part of actually programming the game.
2. You need to check for a collision between the lh]uan object and each of the new lh]pbkni
objects using the new ?khheoekj*lh]uan=j`Lh]pbkni method. This means writing out almost
the same directive for as many times as you have lh]pbkni objects and changing the name of
the lh]pbkni object for each new line. You'll give the collision a mild bounce, for which a value
of ,*. works pretty well. The friction value is additional friction to the friction that you've
already assigned in the Lh]uan[Lh]pbkni class. You don't really need it in this game, so you'll
give it a value of zero. Change the I]ej[>qc?]p_dan*]o file by adding the following code to