379
23
AJitterTolerantRigidBody
SleepCondition
Eric Lengyel
Terathon Software
23.1Introduction
One of the primary optimizations employed by any physics engine is the ability
to put a rigid body to sleep when it has reached a resting state. A sleeping rigid
body is not processed by the physics engine until some kind of event occurs, such
as a collision or broken contact, necessitating that it is woken up and simulated
once again. The fact that most rigid bodies are in the sleeping state at any one
time is what allows a large number of simulated objects to exist in a game level
without serious performance problems.
A problem faced by all physics engines is how to decide that a rigid body has
actually come to rest. If we put an object to sleep without being sure that it has
come to rest, then we risk accidentally freezing it in mid-simulation, which can
look odd to the player. On the other hand, if we are too conservative and wait for
too strict of a condition to be met before we put an object to sleep, then we can
run into a situation where too many objects are being simulated unnecessarily
and performance suffers.
The sleep decision problem is complicated by the fact that all physics en-
gines exhibit some jitter no matter how good the constraint solver is. If the right
sleep condition isn’t chosen in the design of a physics engine, then jitter can pre-
vent an object from ever going to sleep. Or at the very least, jitter can delay an
object from entering the sleep state, and this generally causes a higher number of
objects to be simulated at any given time. This chapter discusses a simple condi-
tion that can be used to determine when it is the proper time to put a rigid body to
sleep, and it is highly tolerant to jitter.
380 23.AJitterTolerantRigidBodySleepCondition
23.2TheSleepCondition
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
C
x. As we accumulate a bounding box for the point
C, we also accumulate a second bounding box for the point
C
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
C
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,
C
x, and
C
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.
23.2TheSleepCondition 381
Figure 23.1. Bounding boxes are maintained for the center of mass C and the two points
C
x and
C
y
, where x and y are the first two columns of the matrix transforming the
rigid body into world space.
The number of steps n and the bounding box size threshold t are parameters
that can be adjusted in the physics engine. Typically, if the three test points re-
main inside small enough bounding boxes for one second, then it’s safe to put a
rigid body to sleep. The size threshold for the two points away from the center of
mass can be different from that used for the center of mass in order to impose
angular velocity limits.
Whenever the sleep test fails because one of the bounding boxes has grown
too large during a particular simulation step, the whole test should be reset. That
is, the current values of
C,
C
x, and
C
y
should be used to reinitialize the
bounding boxes, and the resting step count should be restarted at one. For a rigid
body that is actively moving in some way, this reset occurs on every simulation
step.
C
Cx
Cy
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset