380 23.AJitter‐TolerantRigidBodySleepCondition
23.2TheSleepCondition
A physics engine typically puts a rigid body to sleep once some condition has
been satisfied for a certain number of simulation steps. The most obvious condi-
tion to test is that the linear velocity and angular velocity of the rigid body both
stay under particular thresholds. However, using such a condition is not robust
because even if the object isn’t really going anywhere, jitter can cause its step-to-
step velocity to be higher than the threshold value.
The solution is to maintain an axis-aligned world-space bounding box for a
rigid body’s center of mass C. We begin with a bounding box whose minimal
and maximal corners are both C (so that the box’s dimensions are zero), and we
expand the box at each simulation step to include the new center of mass. After a
certain number of steps n have been accumulated, we look at the size of the
bounding box. If the largest dimension is less than a certain threshold value t,
then we can put the object to sleep. The object can jitter around inside the box all
it wants, but as long as it doesn’t move further than t units of distance along any
axis over n steps, then it still goes to sleep.
Of course, this solution isn’t quite enough. The rigid body could be rotating
about its center of mass without any linear translation, in which case the center of
mass stays inside our bounding box, and the physics engine mistakenly decides
to put it to sleep. To prevent this from happening, we can add a second bounding
box away from the center of mass that represents the volume containing some
other point that moves with the rigid body. The easiest point to choose would be
a point one unit away from the center of mass along the object’s world-space
x-axis. If the matrix
xyz transforms the rigid body into world space, then
our new point is given by
x. As we accumulate a bounding box for the point
C, we also accumulate a second bounding box for the point
x calculated at
every step. If the size of both boxes stays under our threshold t for n steps, then
the object can be put to sleep.
We aren’t quite done yet. The rigid body could be rotating about the axis
x
through its center of mass, in which case the points
C and
x would stay inside
their bounding box thresholds, but the object should not be put to sleep. We need
to add one more test point that lies off of the x-axis. The most straightforward
choice would be a point one unit away from the center of mass along the y-axis.
So we maintain bounding boxes for the three points
C,
x, and
y
, as
shown in Figure 23.1. Using three points that are not collinear ensures that there
is no motion that a rigid body can undergo that would be mistaken for a resting
state.