Game Development Reference
InDepth Information
Figure 12.5
These two players
each have a radius around
them constituting their hit area.
“
In this case, we also need to correct for the perspective
skew of the field. The best way to make this adjustment would be
to have the game engine store their coordinates as though they
werebeingviewedfromthetopdown.Then,theenginecantest
against traditional circles, but render out the view by applying the
perspective correction.
Another nice feature of this type of testing is that it is easy to have
multiple testing radii because the only real criterion is a number in
pixels. Perhaps when two players get a certain distance from each
other, they gain the ability to talk to each other, but only at a closer
distance can they fight, exchange inventory, cuddle, and so on.
One more example in which this type of detection is ideal is that
of a billiards simulation. In a top
touching.
”
down pool game, for instance, you
need to be able to accurately tell when two objects are colliding. The
easiest way to do this type of test is a measurement of the distance
between their edges. This scenario is shown in
Fig. 12.6
.
If you recall back to Chapter 10, the distance formula between
two points is

p
.Asyoucanseein
Fig. 12.6
,the
value of
d
is the distance between the two center points of the
balls. However, this isn
ð
x
2
−
x
1
Þ
+
ð
y
2
−
y
1
Þ
t the value that will tell us when the balls
arecollidingbecausebythetimethedistancebetweenthemis0,
they will be on top of each other. To find the distance between
their edges, we have to calculate
d
minus the two radii. If we use
the value of
r
fortheradius,andassumethetwoballsareofthe
same size (which they would be in billiards), we can
then say that the distance between the two edges is
'
p
ð
r
d
=
x
2
−
x
1
Þ
+
ð
y
2
−
y
1
Þ
2
r
:
When the value of
d
is 0, the two balls are touching.
If it is less than 0, they are overlapping and must have
their positions corrected.
−
Ball 2
(
x
2
,
y
2
)
d
d
= √
(
x
2

x
1
)
+
(
y
2

y
1
)
r
Rect Testing
Another similar method to the basic
hitTestObject
is what
is known as
rect testing
. It involves getting the bounding
box rectangle of any two DisplayObjects (using the
Ball 1
(
x
1
,
y
1
)
Figure 12.6
A distance collision check
applied to two balls on a pool table.