Game Development Reference
In-Depth Information
object is awakened, it will fall asleep again immediately. Giving it a value of twice
the threshold prevents this and makes sure the object is awake long enough to do
something interesting (presumably the setAwake method is being called so the object
can be awakened to do something interesting, not to fall right back asleep).
Finally, we add functions to check whether an object is asleep and to set and check
the value of canSleep . These are implemented on the CD, and none of them are com-
plex enough to require analysis here.
Putting Objects to Sleep
The algorithm for putting objects to sleep is simple. At each frame we monitor their
motion. When their motion stabilizes over several frames, and their velocity is near
zero, we put them to sleep.
The “near zero” is controlled by a parameter called sleepEpsilon . 2 When the
value of the motion data member drops below this threshold, the body is put to sleep:
if (motion < sleepEpsilon)
{
setAwake(false);
}
In the code on the CD the sleep epsilon value is shared for the whole simulation. It
is a global variable accessed through a pair of functions: setSleepEpsilon and get-
SleepEpsilon . You can fine-tune the value by using body-specific thresholds if you
like.
Setting sleep epsilon is a trial-and-error process. The collision-handling system
introduces motion into objects at each frame. If you set sleep epsilon too low, ob-
jects may never fall asleep. Even if you use a resolution system that doesn't have these
problems, too low a value may take a long time to reach. If you set the value too high,
then objects that are obviously in motion can be sent to sleep, and that can look odd.
I tend to set my sleep threshold as high as possible before strange mid-motion freezes
become apparent.
The algorithm is simple, but it relies on calculating the value of motion . The mo-
tion value needs to encapsulate the linear and angular velocity of the object in a single
scalar. To do this we use the total kinetic energy of the object. In chapter 3 we saw that
the kinetic energy of a particle is given by
1
2 m
2
E k =
p
|
2.
The Greek letter epsilon is used in engineering to mean a very small quantity of any kind.
Search Nedrilad ::




Custom Search