Game Development Reference
In-Depth Information
of momentum. Well, we're still calculating impulses just like in Chapter 5 ; however, in
this example, we're going to compute the impact force based on that impulse and apply
that force to the colliding objects. We'll let the numerical integrator integrate that force
to derive the colliding particle's new velocities. Either method works, and we'll show
you an example of the former method later. We're showing the latter method here just
to illustrate some alternatives. The advantage of this latter method is that it is easy to
compute impact forces due to multiple impacts and let the integrator take care of them
all at once.
Now, with these changes made to Particle , we need to add a line of code to Up
dateSimulation , as shown here:
{
.
.
.
// update computer controlled units:
for(i=0; i<_MAX_NUM_UNITS; i++)
{
Units[i].bCollision = CheckForCollisions(&(Units[i]));
Units[i].UpdateBodyEuler(dt);
.
.
.
} // end i-loop
.
.
.
}
The new line is Units[i].bCollision = CheckForCollisions(&(Units[i])); . Check
ForCollisions is a new function that takes the given unit, whose pointer is passed as
an argument, and checks to see if it's colliding with anything—in this case, the ground.
If a collision is detected, CheckForCollisions also computes the impact force and re‐
turns true to let us know a collision has occurred. CheckForCollisions is as follows:
bool CheckForCollisions(Particle* p)
{
Vector n;
Vector vr;
float vrn;
float J;
Vector Fi;
bool hasCollision = false;