Game Development Reference

In-Depth Information

The last bit of code we need to add to have fully functioning collisions with obstacles

involves adding more collision detection and handling code to the
CheckForColli

sions
function. Before we look at
CheckForCollisions
, let's consider colliding circles

in general to gain a better understanding of what the new code will do.

Figure 8-6
illustrates two circles colliding. We aim to detect whether or not these circles

are colliding by checking the distance between their centers. If the distance between the

two centers is greater than the sum of the radii of the circles, then the particles are not

colliding. The topmost illustration in
Figure 8-6
shows the distance,
d
, between centers

and the distance,
s
, between the edges of the circles;
s
is the gap between the two. Another

way to think about this is that if
s
is positive, then there's no collision. Referring to the

middle illustration in
Figure 8-6
, if
s
is equal to 0, then the circles are in contact. If
s
is

a negative number, as shown in the bottommost illustration, then the circles are pene‐

trating.

Figure 8-6. Collision states

We'll apply these principles for detecting colliding circles to detecting collisions between

our particles and obstacles since they are both circles.
Figure 8-7
illustrates how our

particle-to-obstacle collisions might look.