Game Development Reference
to not try to develop the end-all collision detection system is
that there really isn
t one that works best in every possible situation.
ve used the same technique twice in two games that
t extremely similar. What works well in a driving game might
not make sense in a pinball game, and so on. The following sections
will outline the different types of detection you can achieve in AS3,
with some examples.
HitTestObject—The Most Basic Detection
AS3 provides two methods to developers to detect when
DisplayObjects are colliding. The first, and simplest, is hitTestObject .
You can call it on one DisplayObject and pass it another Display-
Object to test against, regardless of location or parental hierarchy.
Flash will resolve any differences in coordinate systems. If the two
objects are touching, it returns true; otherwise false. Sounds great,
right? Unfortunately, there is one big catch. To keep this calculation
fast, Flash resolves the two DisplayObjects down to their basic
bounding boxes. In other words, even if a shape is very intricate and
has large parts that are transparent or void of any data, Flash will
see it as a single rectangle. This is shown in Fig. 12.1 .
To make matters worse, the bounding box will adjust to what-
ever size it needs to be to encompass all the DisplayObject data. If
that circle from Fig. 12.1 wereabitmapinsteadofashape,it
would actually be a square because of the transparent parts of the
image. If you were to rotate this circle, the bitmap square is now at
an angle. Figure 12.2 shows the larger bounding box that Flash will
now use to fit this rotated shape.
As a result of these limitations, hitTestObject is generally the least
accurate method of determining a collision. That said, it is very fast
and definitely has its uses. When all you need to know is whether two
Sprites are overlapping into each other
s display space, hitTestObject
is very effective. If your game has DisplayObjects that can change
Actual shape object
Actual image object
Figure 12.1 A shape object like a circle is still seen
as a rectangle with its maximum dimensions by
Flash ' s hit detection engine.
Figure 12.2 Once rotated, an image actually takes up
a larger space than its actual dimensions, during