Game Development Reference
In-Depth Information
Implementing Collisions
Adding external forces made the simulation a little more interesting. However, to really
make it pop, we're going to add collisions. Specifically, we'll handle particle-to-ground
collisions and particle-to-object collisions. If you have not yet read Chapter 5 , which
covers collisions, you should because we'll implement principles covered in that chapter
here in the example. We'll implement enough collision handling in this example to allow
particles to bounce off the ground and circular objects, and we'll come back to collision
handling in more detail in Chapter 10 . The material in this chapter should whet your
Particle-to-Ground Collisions
Essentially what we're aiming to achieve with particle-to-ground collision detection is
to prevent the particles from passing through a ground plane specified at some y coor‐
dinate. Imagine a horizontal impenetrable surface that the particles cannot pass
through. There are several things we must do in order to detect whether a particle is
indeed colliding with the ground plane. If so, then we need to handle the collision,
making the particles respond in a suitable manner.
The left side of Figure 8-5 illustrates a collision scenario. It's easy to determine whether
or not a collision has taken place. Over a given simulation time step, a particle may have
moved from some previous position (its position at the previous time step) to its current
position. If this current position puts the centroid coordinate of the particle within one
particle radius of the ground plane, then a collision might be occurring. We say might
because the other criteria we need to check in order to determine whether or not a
collision is happening is whether or not the particle is moving toward the ground plane.
If the particle is moving toward the ground plane and it's within one radius of the ground
plane, then a collision is occurring. It may also be the case that the particle has passed
completely through the ground plane, in which case we assume a collision has occurred.