Game Development Reference
In-Depth Information
Now, to compute the impact force, Fi , that will act on the particle during the next time
step, making it bounce off the ground, we set Fi equal to the collision normal vector.
The magnitude of the impact force is equal to the impulse, J , divided by the time step
in seconds. The line Fi *= J/_TIMESTEP; takes care of calculating the final impact force.
To keep the particle from penetrating the ground, we reposition it so that it's just resting
on the ground. The y-position is easy to compute as the ground plane elevation plus the
radius of the particle. We compute the x-position by linearly interpolating between the
particle's previous position and its current position using the newly computed y-
position. This effectively backs up the particle along the line of action of its velocity to
the point where it is just touching the ground plane.
When you run the simulation now, you'll see the particles fall, drifting a bit from left to
right until they hit the ground plane. Once they hit, they'll bounce on the ground,
eventually coming to rest. Their specific behavior in this regard depends on what drag
coefficient you use and what coefficient of restitution you use. If you have wind applied,
when the particles do come to rest, vertically, they should still drift to the right as though
they are sliding on the ground plane.
Particle-to-Obstacle Collisions
To make things really interesting, we'll now add those circular obstacles you saw in
Figure 8-1 through Figure 8-4 . The particles will be able to hit them and bounce off or
even settle down into crevasses made by overlapping obstacles. The obstacles are simply
static particles. We'll define them as particles and initialize them but then skip them
when integrating the equations of motion of the dynamic particles. Here's the declara‐
tion for the Obstacles array:
Particle Obstacles[_NUM_OBSTACLES];
Initializing the obstacles is a matter of assigning them positions and a common radius
and a mass. The few lines of code shown next were added to the main program's Initi
alize function to randomly position obstacles in the lower, middle portion of the win‐
dow above the ground plane. Figure 8-1 through Figure 8-4 illustrate how they are
distributed.
bool Initialize(void)
{
.
.
.
for(i=0; i<_NUM_OBSTACLES; i++)
{
Obstacles[i].vPosition.x = GetRandomNumber(_WINWIDTH/2 −