Game Development Reference
In-Depth Information
if (body0awake) body[1]->setAwake();
else body[0]->setAwake();
}
}
This method is called whenever we are about to resolve a collision. Collisions
that occur between a sleeping object and an awake object but are not being consid-
ered (because they don't have any velocity or penetration to resolve) don't require the
sleeping object to be awakened. If the contact isn't severe enough to need resolving,
we can assume it isn't severe enough to wake the sleeping object.
If we have a series of collisions in a chain, as shown in figure 16.3, the first colli-
sion will be handled by waking up object B. Then the velocity update algorithm will
determine that the second contact needs resolving, waking up object C, and so on.
Eventually all the objects that need a velocity or position change will be awakened, as
required.
The adjustPositions and adjustVelocities methods of the contact resolver have
the call added just before they perform the resolution on a single contact. Here is the
abbreviated code for penetration resolution:
for (unsigned i = 0; i < positionIterations; i++)
{
// Find worstContact (as before) ...
if (!worstContact) break;
worstContact->matchAwakeState();
worstContact->applyPositionChange();
updatePenetrations();
}
There is a second situation in which we need to wake up a rigid body. That is
when a force is applied to it (excluding forces that are always present, such as gravity).
This can be done manually, adding a setAwake call each time a force is applied. This
is difficult to remember, however, so I have elected to wake the object automatically
whenever a force or torque is applied. Each of the addForce , addForceAtPoint ,and
addTorque functions in the RigidBody class on the CD automatically calls setAwake .
We now have a fully functional sleep system, capable of dramatically improving
the performance of the engine.
Typically, when the game level is loaded, all rigid bodies are placed so that they
are in their rest position. They can then all be set to sleep when the game begins.
This makes the physics simulation code very fast indeed. Objects will require physical
Search Nedrilad ::




Custom Search