Game Development Reference

In-Depth Information

J
. Finally,
hasCollision
is a flag that's set based on whether or not a collision has been

detected.

Now we'll look at the details within
CheckForCollisions
. The first task is to initialize

the impact force vector,
vImpactForces
, to 0. Next, we make the first collision check by

determining if the y-position of the particle is less than the ground plane height plus

the particles radius. If it is, then we know a collision may have occurred.
(_GROUND_PLANE

represents the
y
coordinate of the ground plane, which we have set to 100.) If a collision

may have occurred, then we make the next check—to determine if the particle is moving

toward the ground plane.

To perform this second check, we compute the unit normal vector, relative velocity, and

relative velocity component in the collision normal direct as described earlier. If the

relative velocity in the normal direction is negative (i.e., if
vrn
< 0), then a collision has

occurred. If either of these checks is
false
, then a collision has not occurred and the

function exits, returning
false
.

The interesting stuff happens if the second check passes. This is where we have to de‐

termine the impact force that will cause the particle to bounce off the ground plane.

Here's the specific code that computes the impact force:

J = -(vr*n) * (_RESTITUTION + 1) * p->fMass;

Fi = n;

Fi *= J/_TIMESTEP;

p->vImpactForces += Fi;

p->vPosition.y = _GROUND_PLANE + p->fRadius;

p->vPosition.x = (_GROUND_PLANE + p->fRadius −

p->vPreviousPosition.y) /

(p->vPosition.y - p->vPreviousPosition.y) *

(p->vPosition.x - p->vPreviousPosition.x) +

p->vPreviousPosition.x;

hasCollision = true;

We compute the impulse using the formulas presented in
Chapter 5
.
J
is a scalar equal

to the negative of the relative velocity in the normal direction times the coefficient of

restitution plus 1 times the particle mass. Recall that the coefficient of restitution,
_RES

TITUTION
, governs how elastic or inelastic the collision is, or in other words, how much

energy is transferred back to the particle during the impact. We have this value set to

0.6, but it is tunable depending on what effect you're trying to achieve. A value of 1

makes the particles very bouncy, while a value of, say, 0.1 makes them sort of stick to

the ground upon impact.