Game Development Reference
In-Depth Information
rounding up. We add another for loop, this time counting the
number of checks we need to perform and creating a new rectangle
with a new position to test against. If this seems like a lot of loop-
ing, remember that the number of checks you
re likely to have to
perform is still pretty low unless the rectangle you
'
re checking
against is very small. Even then, AS3 should be able to handle it
just fine. To be even more accurate, it would be wise to add the
same iterative checking for the projectiles as well, but I
'
'
ll leave this
to you as an exercise to complete.
Pixel-Perfect Collision Detection
and Physics
Although for many applications, the methods I
'
ve discussed so far are
very effective (and the most efficient for mobile platforms), there are
some instances where you simply will need pixel-perfect detection of
two arbitrary shapes. There are ways to do this using BitmapData
drawing and comparisons, but I
'
m not going to discuss that here.
Instead, I
m going to recommend the excellent Collision Detection Kit
developed by Corey O
'
ve included the latest version (as of this
writing) with the chapter files that can be downloaded from www.
flashgamebook.com . On the site you will also find direct links to Cor-
ey
'
Neil. I
'
'
s site and his Google Code repository for the source. It
'
sapretty
great library and takes very little time to implement.
Sometimes, even pixel-accurate detection is less important than
the way objects react when they collide. In these cases, you may
need a full rigidbody collision engine with its own physics simula-
tion. For scenarios where you
ll have a lot of objects colliding at
once and needing to react to each other, writing your own physics
engine from scratch can be daunting. Luckily, there
'
sanopen
source library that has been ported to just about every language
under the sun called Box2D. It has been used in a number of
award-winning games and is very full-featured for doing 2D physics
and collisions. However, because it is so full-featured, it can be
daunting to approach. The physics simulation runs independently
of any visuals, so you must bind DisplayObjects to the colliders in
the engine. This increased flexibility also makes it more involved to
implement. To compound this, because it was ported from the
C programming language, there are some conventions in naming
that can seem off-putting at first. Those
'
aside, it is an
excellent piece of software and performs extremely well, even with
many objects in the simulation at once. On www.flashgamebook.
com , you can find links to tutorials and examples that people in
the community have written to get started, as well as links to the
latest source code. The example in Chapter 16 also makes use of
this library and discusses some of the basics.
warnings
Search Nedrilad ::




Custom Search