Game Development Reference

In-Depth Information

// Reset aggregate impact force

p->vImpactForces.x = 0;

p->vImpactForces.y = 0;

// check for collisions with ground plane

if(p->vPosition.y <= (_GROUND_PLANE+p->fRadius)) {

n.x = 0;

n.y = 1;

vr = p->vVelocity;

vrn = vr * n;

// check to see if the particle is moving toward the ground

if(vrn < 0.0) {

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;

}

}

return hasCollision;

}

CheckForCollisions
makes two checks: 1) it checks to see whether or not the particle

is making contact or passing through the ground plane; and 2) it checks to make sure

the particle is actually moving toward the ground plane. Keep in mind a particle could

be in contact with the ground plane right after a collision has been handled with the

particle moving away from the ground. In this case, we don't want to register another

collision.

Let's consider the details of this function, starting with the local variables.
n
is a vector

that represents the unit normal vector pointing from the ground plane to the particle

colliding with it. For ground collisions, in this example, the unit normal vector is always

straight up since the ground plane is flat. This means the unit normal vector will always

have an
x
component of 0 and its
y
component will be 1.

The
Vector vr
is the relative velocity vector between the particle and the ground. Since

the ground isn't moving, the relative velocity is simply the velocity of the particle.
vrn

is a scalar that's used to store the component of the relative velocity in the direction of

the collision unit normal vector. We compute
vrn
by taking the dot product of the

relative velocity with the unit normal vector.
J
is a scalar that stores the impulse resulting

from the collision.
Fi
is a vector that stores the impact force as derived from the impulse