Rigid Body Simulation Pixar
Rigid Body Simulation Pixar
David Baraff
Pixar Animation Studios
Please note: This document is 2001 by David Baraff. This chapter may be freely
duplicated and distributed so long as no consideration is received in return, and
this copyright notice remains intact.
Rigid Body Simulation
David Baraff
Pixar Animation Studios
Introduction
This portion of the course notes deals with the problem of rigid body dynamics. To help get you
started simulating rigid body motion, we’ve provided code fragments that implement most of the
concepts discussed in these notes. This segment of the course notes is divided into two parts. The first
part covers the motion of rigid bodies that are completely unconstrained in their allowable motion;
that is, simulations that aren’t concerned about collisions between rigid bodies. Given any external
forces acting on a rigid body, we’ll show how to simulate the motion of the body in response to these
forces. The mathematical derivations in these notes are meant to be fairly informal and intuitive.
The second part of the notes tackles the problem of constrained motion that arises when we
regard bodies as solid, and need to disallow inter-penetration. We enforce these non-penetration
constraints by computing appropriate contact forces between contacting bodies. Given values for
these contact forces, simulation proceeds exactly as in the unconstrained case: we simply apply all
the forces to the bodies and let the simulation unfold as though the motions of bodies are completely
unconstrained. If we have computed the contact forces correctly, the resulting motion of the bodies
will be free from inter-penetration. The computation of these contact forces is the most demanding
component of the entire simulation process.1
1
Collision detection (i.e. determining the points of contact between bodies) runs a close second though!
G1
Part I. Unconstrained Rigid Body Dynamics
1 Simulation Basics
This portion of the course notes is geared towards a full implementation of rigid body motion. In this
section, we’ll show the basic structure for simulating the motion of a rigid body. In section 2, we’ll
define the terms, concepts, and equations we need to implement a rigid body simulator. Following
this, we’ll give some code to actually implement the equations we need. Derivations for some of the
concepts and equations we will be using will be left to appendix A.
The only thing you need to be familiar with at this point are the basic concepts (but not the numer-
ical details) of solving ordinary differential equations. If you’re not familiar with this topic, you’re
in luck: just turn back to the beginning of these course notes, and read the section on “Differential
Equation Basics.” You also might want to read the next section on “Particle Dynamics” as well,
although we’re about to repeat some of that material here anyway.
Simulating the motion of a rigid body is almost the same as simulating the motion of a particle,
so let’s start with particle simulation. The way we simulate a particle is as follows. We let a function
x(t) denote the particle’s location in world space (the space all particles or bodies occupy during
simulation) at time t. The function v(t) = ẋ(t) = dtd x(t) gives the velocity of the particle at time t.
The state of a particle at time t is the particle’s position and velocity. We generalize this concept by
defining a state vector X(t) for a system: for a single particle,
x(t)
X(t) = . (1–1)
v(t)
When we’re talking about an actual implementation, we have to “flatten” out X(t) into an array.
For a single particle, X(t) can be described as an array of six numbers: typically, we’d let the first
three elements of the array represent x(t), and the last three elements represent v(t). Later, when we
talk about state vectors X(t) that contain matrices as well as vectors, the same sort of operation is
done to flatten X(t) into an array. Of course, we’ll also have to reverse this process and turn an array
of numbers back into a state vector X(t). This all comes down to pretty simple bookkeeping though,
so henceforth, we’ll assume that we know how to convert any sort of state vector X(t) to an array
(of the appropriate length) and vice versa. (For a simple example involving particles, look through
the “Particle System Dynamics” section of these notes.)
For a system with n particles, we enlarge X(t) to be
x1 (t)
v1 (t)
X(t) = ... (1–2)
xn (t)
vn (t)
Given any value of X(t), equation (1–3) describes how X(t) is instantaneously changing at time t.
A simulation starts with some initial conditions for X(0), (i.e. values for x(0) and v(0)) and then
uses a numerical equation solver to track the change or “flow” of X over time, for as long as we’re
interested in. If all we want to know is the particle’s location one second from now, we ask the solver
to compute X(1), assuming that time units are in seconds. If we’re going to animate the motion of
the particle, we’d want to compute X( 30 1
), X( 30
2
) and so on.
The numerical method used by the solver is relatively unimportant with respect to our actual
implementation. Let’s look at how we’d actually interact with a numerical solver, in a C++-like
language. Assume we have access to a numerical solver, which we’ll generically write as a function
named ode. Typically, ode has the following specification:
We pass an initial state vector to ode as an array x0. The solver ode knows nothing about the
inherent structure of x0. Since solvers can handle problems of arbitrary dimension, we also have to
pass the length len of x0. (For a system of n particles, we’d obviously have len = 6n.) We also
pass the solver the starting and ending times of the simulation, t0 and t1. The solver’s goal is to
compute the state vector at time t1 and return it in the array xEnd.
We also pass a function Dxdt() to ode. Given an array y that encodes a state vector X(t) and
a time t, Dxdt() must compute and return dtd X(t) in the array xdot. (The reason we must pass
t to Dxdt() is that we may have time-varying forces acting in our system. In that case, Dxdt()
would have to know “what time it is” to determine the value of those forces.) In tracing the flow of
X(t) from t0 to t1, the solver ode is allowed to call Dxdt() as often as it likes. Given that we
have such a routine ode, the only work we need to do is to code up the routine Dxdt() which we’ll
give as a parameter to ode.
Simulating rigid bodies follows exactly the same mold as simulating particles. The only differ-
ence is that the state vector X(t) for a rigid body holds more information, and the derivative dtd X(t) is
a little more complicated. However, we’ll use exactly the same paradigm of tracking the movement
of a rigid body using a solver ode, which we’ll supply with a function Dxdt().
Since the center of mass of the body lies at the origin, the world-space location of the center of
mass is always given directly by x(t). This lets us attach a very physical meaning to x(t) by saying
that x(t) is the location of the center of mass in world space at time t. We can also attach a physical
meaning to R(t). Consider the x axis in body space i.e. the vector (1, 0, 0). At time t, this vector
has direction
1
R(t) 0
0
z x(t) z'
p0 y p(t)
0
x'
x z
Figure 1: The center of mass is transformed to the point x(t) in world space, at time t. The fixed x, y,
and z axes of the body in body space transform to the vectors x0 = R(t)x, y0 = R(t)y and z0 = R(t)z.
The fixed point p0 in body space is transformed to the point p(t) = R(t) p0 + x(t).
then
1 r xx
R(t) 0 = r xy (2–3)
0 r xz
which is the first column of R(t). The physical meaning of R(t) is that R(t)’s first column gives the
direction that the rigid body’s x axis points in, when transformed to world space at time t. Similarly,
the second and third columns of R(t),
r yx rzx
r yy and rzy
r yz rzz
are the directions of the y and z axes of the rigid body in world space at time t (figure 2).
z'
x'
z
Figure 2: Physical interpretation of the orientation matrix R(t). At time t, the columns of R(t) are
the world-space directions that the body-space x, y, and z axes transform to.
v(t)
x(t)
y
Figure 3: Linear velocity v(t) and angular velocity ω(t) of a rigid body.
Since b and ω(t) are perpendicular, their cross product has magnitude
Putting this together, we can write ṙ(t) = ω(t) × (b). However, since r(t) = a + b and a is parallel
to ω(t), we have ω(t) × a = 0 and thus
Let’s put all this together now. At time t, we know that the direction of the x axis of the rigid
body in world space is the first column of R(t), which is
r xx
r xy .
r xz
At time t, the derivative of the first column of R(t) is just the rate of change of this vector: using the
cross product rule we just discovered, this change is
r xx
ω(t) × r xy .
r xz
ω (t) × b
a
r(t) = a + b
Figure 4: The rate of change of a rotating vector. As the tip of r(t) spins about the ω(t) axis, it traces
out a circle of diameter |b|. The speed of the tip of r(t) is |ω(t)||b|.
The same obviously holds for the other two columns of R(t). This means that we can write
r xx r yx rzx
Ṙ = ω(t) × r xy ω(t) × r yy ω(t) × rzy . (2–8)
r xz r yz rzz
This is too cumbersome an expression to tote around though. To simplify things, we’ll use the
following trick. If a and b are 3-vectors, then a × b is the vector
a y bz − b y az
−a x bz + b x az .
ax b y − bx a y
Then2
0 −az a y bx a y bz − b y az
a∗ b = a z 0 −a x b y = −a x bz + b x az = a × b. (2–9)
−a y a x 0 bz ax b y − bx a y
2
This looks a little too “magical” at first. Did someone discover this identity accidentally? Is it a relation that just
happens to work? This construct can be derived by considering what’s known as infinitesimal rotations. The interested
reader might wish to read chapter 4.8 of Goldstein[10] for a more complete derivation of the a∗ matrix.
which is a matrix-matrix multiplication. But since the matrix on the right is R(t) itself, we get simply
that
This, at last, gives us the relation we wanted between Ṙ(t) and ω(t). Note the correspondence
between ṙ(t) = ω(t) × r(t) for a vector, and Ṙ(t) = ω(t)∗ R(t) for the rotation matrix.
X
N
M= mi . (2–14)
i=1
(Henceforth, summations are assumed to be summed from 1 to N with index variable i.)
x(t)
v(t) ri(t)
y
Figure 5: The velocity of the ith point of a rigid body in world space. The velocity of ri (t) can be
decomposed into a linear term v(t) and an angular term ω(t) × (ri (t) − x(t)).
using the definition of ri (t) from equation (2–13). Recall from the definition of the “∗” operator that
ω(t)∗ a = ω(t) × a for any vector a. Using this, we can simply write
Note that this separates the velocity of a point on a rigid body into two components (figure 5): a
linear component v(t), and an angular component ω × (ri (t) − x(t)).
ri(t)
x(t)
y
Fi(t)
Figure 6: The torque τi (t) due to a force Fi (t) acting at ri (t) on a rigid body.
Torque differs from force in that the torque on a particle depends on the location ri (t) of the
particle, relative to the center of mass x(t). We can intuitively think of the direction of τi (t) as
being the axis the body would spin about due to Fi (t), if the center of mass were held firmly in place
(figure 6).
The total external force F(t) acting on the body is the sum of the Fi (t):
X
F(t) = Fi (t) (2–22)
p = mv. (2–24)
The total linear momentum P(t) of a rigid body is the sum of the products of the mass and velocity
of each particle:
X
P(t) = miṙ i (t). (2–25)
From equation (2–17), the velocity ṙ i (t) of the ith particle is ṙ i (t) = v(t) + ω(t) × (ri (t) − x(t)).
Thus, the total linear momentum of the body is
X
P(t) = miṙ i (t)
X
= mi v(t) + mi ω(t) × (ri (t) − x(t)) (2–26)
X X
= mi v(t) + ω(t) × mi (ri (t) − x(t)) .
Because we are using a center of mass coordinate system, we can apply equation (2–20) and ob-
tain
X X
P(t) = mi v(t) = mi v(t) = Mv(t). (2–27)
This gives us the nice result that the total linear momentum of our rigid body is the same as if the
body was simply a particle with mass M and velocity v(t). Because of this, we have a simple trans-
formation between P(t) and v(t): P(t) = Mv(t) and v(t) = P(t)/M. Since M is a constant,
Ṗ(t)
v̇(t) = . (2–28)
M
The concept of linear momentum lets us express the effect of the total force F(t) on a rigid body
quite simply. Appendix A derives the relation
which says that the change in linear momentum is equivalent to the total force acting on a body. Note
that P(t) tells us nothing about the rotational velocity of a body, which is good, because F(t) also
conveys nothing about the change of rotational velocity of a body!
Since the relationship between P(t) and v(t) is simple, we will be using P(t) as a state variable
for our rigid body, instead of v(t). We could of course let v(t) be a state variable, and use the
relation
F(t)
v̇(t) = . (2–30)
M
However, using P(t) instead of v(t) as a state variable will be more consistent with the way we will
be dealing with angular velocity and acceleration.
For an actual implementation, we replace the finite sums with integrals over a body’s volume in
world space. The mass terms mi are replaced by a density function. At first glance, it seems that
we would need to evaluate these integrals to find I(t) whenever the orientation R(t) changes. This
would be prohibitively expensive to do during a simulation unless the body’s shape was so simple
(for example, a sphere or cube) that that the integrals could be evaluated symbolically.
Fortunately, by using body-space coordinates we can cheaply compute the inertia tensor for any
orientation R(t) in terms of a precomputed integral in body-space coordinates. (This integral is typ-
ically computed before the simulation begins and should be regarded as one of the input parameters
and letting 1 denote the 3 × 3 identity matrix, we can express I(t) simply as
X
mi ((ri0 ri0 )1 − ri0 ri0 )
T T
I(t) = (2–35)
X
mi ((ri0 ri0 )1 − ri0 ri0 )
T T
I(t) =
X
= mi ((R(t)r0 i ) T (R(t)r0 i )1 − (R(t)r0 i )(R(t)r0 i ) T )
X (2–36)
= mi (r0 iT R(t) T R(t)r0i 1 − R(t)r0i r0 iT R(t) T )
X
= mi ((r0 iT r0i )1 − R(t)r0i r0 iT R(t) T ).
Since Ibody is specified in body-space, it is constant over the simulation. Thus, by precomputing Ibody
for a body before the simulation begins, we can easily compute I(t) from Ibody and the orientation
matrix R(t). Section 5.1 derives the body-space inertia tensor for a rectangular object in terms of an
integral over the body’s volume in body space.
Thus, the state of a rigid body is its position and orientation (describing spatial information), and its
linear and angular momentum (describing velocity information). The mass M of the body and body-
space inertia tensor Ibody are constants, which we assume we know when the simulation begins. At
any given time, the auxiliary quantities I(t), ω(t) and v(t) are computed by
P(t)
v(t) = , I(t) = R(t)Ibody R(t) T and ω(t) = I(t)−1 L(t). (2–42)
M
d
The derivative dt X(t) is
x(t) v(t)
d d
R(t) ω(t)∗ R(t)
X(t) = = . (2–43)
dt dt P(t) F(t)
L(t) τ(t)
The next section gives an implementation for the function Dxdt() that computes dtd X(t).
One final note: rather than represent the orientation of the body as a matrix R(t) in X(t), it is
better to use quaternions. Section 4 discusses using quaternions in place of rotation matrices. Briefly,
a quaternion is a type of four element vector that can be used to represent a rotation. If we replace
R(t) in X(t) with a quaternion q(t), we can treat R(t) as an auxiliary variable that is computed
directly from q(t), just as ω(t) is computed from L(t). Section 4 derives a formula analogous to
Ṙ(t) = ω(t)∗ R(t), that expresses q̇(t) in terms of q(t) and ω(t).
d
3 Computing dt X(t)
Lets consider an implementation of the function Dxdt() for rigid bodies. The code is written
in C++, and we’ll assume that we have datatypes (classes) called matrix and triple which
implement, respectively, 3 × 3 matrices and points in 3-space. Using these datatypes, we’ll represent
struct RigidBody {
/* Constant quantities */
double mass; /* mass M */
matrix Ibody, /* Ibody */
Ibodyinv; −1
/* Ibody (inverse of Ibody ) */
/* State variables */
triple x; /* x(t) */
matrix R; /* R(t) */
triple P, /* P(t) */
L; /* L(t) */
/* Computed quantities */
triple force, /* F(t) */
torque; /* τ(t) */
};
RigidBody Bodies[NBODIES];
The constant quantities mass, Ibody and Ibodyinv are assumed to have been calculated for
each member of the array Bodies, before simulation begins. Also, the initial conditions for each
rigid body are specified by assigning values to the state variables x, R, P and L of each member of
Bodies. The implementation in this section represents orientation with a rotation matrix; section 4
describes the changes necessary to represent orientation by a quaternion.
We communicate with the differential equation solver ode by passing arrays of real numbers.
Several bookkeeping routines are required:
*y++ = rb->L[0];
*y++ = rb->L[1];
*y++ = rb->L[2];
}
and
rb->P[0] = *y++;
rb->P[1] = *y++;
rb->P[2] = *y++;
rb->L[0] = *y++;
rb->L[1] = *y++;
rb->L[2] = *y++;
/* v(t) = P(t)
M */
rb->v = rb->P / mass;
/* I −1 (t) = R(t)Ibody
−1 R(t) T */
/* ω(t) = I −1 (t)L(t) */
rb->omega = rb->Iinv * rb->L;
}
Note that ArrayToState is responsible for computing values for the auxiliary variables Iinv,
v and omega. We’ll assume that the appropriate arithmetic operations have been defined between
real numbers, triple’s and matrix’s, and that Transpose returns the transpose of a matrix.
#define STATE_SIZE 18
and
computes the force F(t) and torque τ(t) acting on the rigid body *rb at time t, and stores F(t)
and τ(t) in rb->force and rb->torque respectively. ComputeForceAndTorquetakes into
account all forces and torques: gravity, wind, interaction with other bodies etc. Using this routine,
we’ll define Dxdt() as
The numerical solver ode calls calls Dxdt() and is responsible for allocating enough space for the
arrays y, and xdot (STATE_SIZE · NBODIES worth for each). The function which does the real
work of computing dtd X(t) and storing it in the array xdot is ddtStateToArray:
*xdot++ = rb->force[0]; /* d
dt P(t) = F(t) */
*xdot++ = rb->force[1];
*xdot++ = rb->force[2];
*xdot++ = rb->torque[0]; /* d
dt L(t) = τ(t) */
*xdot++ = rb->torque[1];
*xdot++ = rb->torque[2];
}
Given all of the above, actually performing a simulation is simple. Assume that the state vari-
ables of all NBODIES rigid bodies are initialized by a routine InitStates. We’ll have our simu-
1
lation run for 10 seconds, calling a routine DisplayBodies every 24 th of a second to display the
bodies:
void RunSimulation()
{
double x0[STATE_SIZE * NBODIES],
xFinal[STATE_SIZE * NBODIES];
InitStates();
BodiesToArray(xFinal);
dt X(t + 24 )
d 1
/* copy into state variables */
ArrayToBodies(xFinal);
DisplayBodies();
}
}
As we update R(t) using this formula (that is, as we integrate this equation), we will inevitably
encounter drift. Numerical error will build up in the coefficients of R(t) so that R(t) will no longer
be precisely a rotation matrix. Graphically, the effect would be that applying R(t) to a body would
cause a skewing effect.
This problem can be alleviated by representing rotations with unit quaternions. Since quater-
nions have only four parameters, there is only one extra variable being used to describe the three
freedoms of the rotation. In contrast, a rotation matrix uses nine parameters to describe three degrees
of freedom; therefore, the degree of redundancy is noticeably lower for quaternions than rotation
matrices. As a result, quaternions experience far less drift than rotation matrices. If it does become
necessary to account for drift in a quaternion, it is because the quaternion has lost its unit magnitude. 3
This is easily correctable by renormalizing the quaternion to unit length. Because of these two
properties, it is desirable to represent the orientation of a body directly as a unit quaternion q(t).
We will still express angular velocity as a vector ω(t). The orientation matrix R(t), which is needed
to compute I −1 (t), will be computed as an auxiliary variable from q(t).
3
Any quaternion of unit length corresponds to a rotation, so quaternions deviate from representing rotations only if
they lose their unit length. These notes will deal with that problem in a very simplistic way.
[s, v].
[cos(θ/2), sin(θ/2)u].
In using quaternions to represent rotations, if q1 and q2 indicate rotations, then q2 q1 represents the
composite rotation of q1 followed by q2 .4 In a moment, we’ll show how to change the routines of
section 3 to handle the quaternion representation for orientation. Before we can make these changes
though, we’ll need a formula for q̇(t). Appendix B derives the formula
where the multiplication ω(t)q(t) is a shorthand for multiplication between the quaternions [0, ω(t)]
and q(t). Note the similarity between equation (4–2) and
To actually use a quaternion representation, we’ll need to redefine the type RigidBody:
struct RigidBody {
/* Constant quantities */
double mass; /* mass M */
matrix Ibody, /* Ibody */
Ibodyinv; −1
/* Ibody (inverse of Ibody ) */
/* State variables */
triple x; /* x(t) */
quaternion q; /* q(t) */
triple P, /* P(t) */
L; /* L(t) */
/* Computed quantities */
4
This is according to the convention that the rotation of a point p by a quaternion q is qpq−1 . Be warned! This is
opposite the convention for rotation in the original paper Shoemake[16], but it is in correspondence with some more recent
versions of Shoemake’s article. Writing a composite rotation as q2 q1 parallels our matrix notation for composition of
rotations.
/*
* Assume that a quaternion is represented in
* terms of elements ‘r’ for the real part,
* and ‘i’, ‘j’, and ‘k’ for the vector part.
*/
*y++ = rb->q.r;
*y++ = rb->q.i;
*y++ = rb->q.j;
*y++ = rb->q.k;
A similar change is made in ArrayToState. Also, since ArrayToState is responsible for com-
puting the auxiliary variable I −1 (t), which depends on R(t), ArrayToState must also compute
R(t) as an auxiliary variable: in the section
/* v(t) = P(t)
M */
rb->v = rb->P / mass;
/* I −1 (t) = R(t)Ibody
−1 R(t) T */
/* ω(t) = I −1 (t)L(t) */
rb->omega = rb->Iinv * rb->L;
we add the line
rb->R = QuaterionToMatrix(normalize(rb->q));
prior to computing rb->Iinv. The routine normalize returns q divided by its length; this
unit length quaternion returned by normalize is then passed to QuaterionToMatrix which
returns a 3 × 3 rotation matrix. Given a quaternion q = [s, v], QuaterionToMatrix returns the
if(tr >= 0)
{
s = sqrt(tr + 1);
q.r = 0.5 * s;
s = 0.5 / s;
q.i = (m[2,1] - m[1,2]) * s;
q.j = (m[0,2] - m[2,0]) * s;
q.k = (m[1,0] - m[0,1]) * s;
}
else
{
int i = 0;
switch (i)
{
case 0:
s = sqrt((m[0,0] - (m[1,1] + m[2,2])) + 1);
q.i = 0.5 * s;
s = 0.5 / s;
q.j = (m[0,1] + m[1,0]) * s;
q.k = (m[2,0] + m[0,2]) * s;
q.r = (m[2,1] - m[1,2]) * s;
break;
case 1:
s = sqrt((m[1,1] - (m[2,2] + m[0,0])) + 1);
q.j = 0.5 * s;
s = 0.5 / s;
}
return q;
}
The matrix m is structured so that m[0,0], m[0,1] and m[0,2] form the first row (not column)
of m.
The routines ArrayToBodies and BodiesToArray don’t need any changes at all, but note
that the constant STATE_SIZE changes from 18 to 13, since a quaternion requires five less elements
than a rotation matrix. The only other change we need is in ddtStateToArray. Instead of
we’ll use
We’re
. assuming here that the multiplication between the triple rb->omega and the quaternion
rb->q is defined to return the quaternion product
[0, rb->omega]q.
− x0 , − y0 , − z0
2 2 2
Figure 7: A rectangular block of constant unit density, with center of mass at (0,0,0).
5 Examples
5.1 Inertia Tensor of a Block
Let us calculate the inertia tensor Ibody of the rectangular block in figure 7. The block has dimensions
x0 × y0 × z0 . As required, the center of mass of the block is at the origin. Thus, the extent of the
block is from − x20 to x20 along the x axis, and similarly for the y and z axes. To calculate the inertia
tensor, we must treat the sums in equation (2–32) as integrals over the volume of the block. Let us
assume that the block has constant unit density. This means that the density function ρ(x, y, z) is
always one. Since the block has volume x0 y0 z0 , the mass M of the block is M = x0 y0 z0 . Then, in
(and similarly for the others) because the integrals are all symmetric. Thus, the inertia tensor of the
block is
2
y + z20 0 0
M 0 .
Ibody = 0 x20 + z20 0 (5–3)
12
0 0 x0 + y0
2 2
which yields an acceleration of Mg g = g of the center of mass, as expected. What is the torque due
to the gravitational field? The net torque is the sum
X X
(ri (t) − x(t)) × mi g = mi (ri (t) − x(t)) × g = 0 (5–5)
by equation (2–20). We see from this that a uniform gravitational field can have no effect on the
angular momentum of a body. Furthermore, the gravitational field can be treated as a single force
Mg acting on the body at its center of mass.
x(t)
x
(−3,0,−2) (3,0,−2)
F
F
x(t)
x
(−3,0,−2) (3,0,−2)
F
F
Figure 9: A block acted on by two opposite forces F1 and F2 = −F1 , at two different points.
As expected then, the forces acting on the block impart no angular acceleration to the block.
Energy: Energy:
0 1
Mv T v
2
Figure 10: A rectangular block acted on by a force through its center of mass.
−3
((x(t) + 0 ) − x(t)) × F1 +
−2
3 −3 0 3 0
((x(t) + 0 ) − x(t)) × F2 = 0 × 0 + 0 × 0 (5–6)
2 −2 f −2 −f
0 0 0
= 3f + 3f = 6f .
0 0 0
Thus, the net torque is (0, 6 f, 0), which is parallel to the y axis. The final result is that the forces
acting on the block cause it to angularly accelerate about the y axis.
F
F
Energy: Energy:
0 1 1
Mv T v + ω T Iω
2 2
after ten seconds, the body will again have linear velocity v. However, after ten seconds, the body
will have picked up some angular velocity ω, since the force F, acting off center, now exerts a torque
on the body. Since the kinetic energy is (see appendix C)
1
2 M|v|
2
+ 12 ω T Iω
the kinetic energy of the block is higher than when the force acted through the center of mass. But
if identical forces pushed the block in both cases, how can the energy of the block be different?
Hint: Energy, or work, is the integral of force over distance.
F
F
Energy: Energy:
1 1
0 Mv T v + ω T Iω
2 2
Figure 12: The path the force acts over is longer than in figure 10. As a result, the force does more
work, imparting a larger kinetic energy to the block.
Figure 12 shows why the force acting off center results in a higher kinetic energy. The kinetic
energy of the block is equivalent to the work done by the force. The work done by the force is the
integral of the force over the path traveled in applying that force. In figure 11, where the force acts off
the center of mass, consider the path traced out by the point where the force is applied. This path is
clearly longer than the path taken by the center of mass in figure 10. Thus, when the force is applied
off center, more work is done because the point p at which the force is applied traces out a longer
path then when the force is applied at the center of mass.
tc
t0 + ∆t
(inter-penetration detected)
Figure 13: At time t0 + 1t, the particle is found to lie below the floor. Thus, the actual time of
collision tc lies between the time of the last known legal position, t0 , and t0 + 1t.
So far then, we have two problems we’ll need to deal with: computing velocity changes for
colliding contact, and computing the contact forces that prevent inter-penetration. But before we can
tackle these problems we have to deal with the geometric issue of actually detecting contact between
bodies. Let’s go back to dropping the particle to the floor. As we run our simulation, we compute
the position of the particle as it drops towards the floor at specific time values (figure 13). Suppose
we consider the particle at times t0 , t0 + 1t, t0 + 21t etc.5 and suppose the time of collision, tc , at
which the particle actually strikes the floor, lies between t0 and t0 + 1t. Then at time t0 , we find that
the particle lies above the floor, but at the next time step, t0 + 1t, we find the particle is beneath the
floor, which means that inter-penetration has occurred.
If we’re going to stop and restart the simulator at time tc , we’ll need to compute tc . All we know
so far is that tc lies between t0 and t0 + 1t. In general, solving for tc exactly is difficult, so we
solve for tc numerically, to within a certain tolerance. A simple way of determining tc is to use a
numerical method called bisection[14]. If at time t0 + 1t we detect inter-penetration, we inform the
ODE solver that we wish to restart back at time t0 , and simulate forward to time t0 + 1t/2. If the
simulator reaches t0 + 1t/2 without encountering inter-penetration, we know the collision time tc
lies between t0 + 1t/2 and t0 + 1t. Otherwise, tc is less than t0 + 1t/2, and we try to simulate from
t0 to t0 + 1t/4. Eventually, the time of collision tc is computed to within some suitable numerical
tolerance. The accuracy with which tc is found depends on the collision detection routines. The
collision detection routines have some parameter . We decide that our computation of tc is “good
enough” when the particle inter-penetrates the floor by no more than , and is less than above the
floor. At this point we declare that the particle is in contact with the floor (figure 14).
The method of bisection is a little slow, but its easy to implement and quite robust. A faster
method involves actually predicting the time tc of the collision, based on examining X(t0 ) and
X(t0 + 1t). Baraff[1, 2] describes how to make such predictions. How to actually implement all of
5
The ODE solver doesn’t have to proceed with equal size time steps though.
t0 + ∆t
(inter-penetration detected)
Figure 14: When the particle is found to be within some tolerance of contacting the floor, then tc
is considered to have been computed to within sufficient accuracy.
this depends on how you interact with your ODE routines. One might use exception handling code
to signal the ODE of various events (collisions, inter-penetration), or pass some sort of messages to
the ODE solver. We’ll just assume that you have some way of getting your ODE solver to progress
just up to the point tc .
Once you actually reach the time of a collision, or whenever you’re in a state X(t) where no
inter-penetration has occurred, a geometric determination has to be made to find all the points of
contact. (Just because you may be looking for the time of collision between two bodies A and B
doesn’t mean you get to neglect resting contact forces between other bodies C and D. Whenever
you’re trying to move the simulation forward, you’ll need to compute the point of contact between
bodies and the contact forces at those points.) There is a vast amount of literature dealing with the
collision detection problem. For instance, some recent SIGGRAPH papers dealing with the subject
are Von Herzen, Barr and Zatz[17] and Moore and Wilhelms[12]; in robotics, a number of papers of
interest are Canny[4], Gilbert and Hong[6], Meyer[11] and Cundall[5]. Preparata and Shamos[13]
describes many approaches in computational geometry to the problem. In the next section, we’ll
briefly describe a collision detection “philosophy” that leads to very efficient algorithms, for the sorts
of simulation these course notes are concerned with. Actual code for the algorithms is fairly easy to
write, but a little too lengthy to fit in these notes. Following this, we’ll move on to consider colliding
and resting contact.
7 Collision Detection
The collision detection algorithm begins with a preprocessing step, in which a bounding box for each
rigid body is computed (a box with sides parallel to the coordinate axes). Given n such bounding
boxes, we will want to quickly determine all pairs of bounding boxes that overlap. Any pair of rigid
bodies whose bounding boxes do not overlap need not be considered any further. Pairs of rigid
Figure 15: Exhaustive search for a separating plane. Only one face of the two polygons forms a
separating plane.
(a) (b)
defining
face
Figure 16: (a) At this time step, the separating plane is defined by a face of one of the polygons. (b) At
the next time step, the polygons have moved, but the same face still defines a separating plane.
Figure 17: The face that has been defining a separating plane no longer does so, and a new separating
plane must be found.
hedron is inside the other, or an edge of one polyhedron has intersected a face of the other.6 In
this case, the inter-penetrating vertex, or intersecting edge and face are cached as a witness to the
inter-penetration. Since this indicates a collision at some earlier time, the simulator will back up and
attempt to compute dtd X(t) at some earlier time. Until the collision time is determined, the first action
taken by the collision/contact determination step will be to check the cached vertex or edge and face
to see if they indicate inter-penetration. Thus, until the collision time is found, states in which the
inter-penetration still exists are identified as such with a minimum of computational overhead.
I6 I5
I3 I4
I1 I2
b3 b6 b1 e6 e1 b5 b2 e3 b4 e5 e4 e2
Figure 18: The sweep/sort algorithm. (a) When b1 is encountered, the active list contains intervals
3 and 6; interval 1 is reported to overlap with these two intervals. Interval 1 is added to the active
list and the algorithm continues. (b) When e3 is encountered, the active list contains intervals 2, 3
and 5. Interval 3 is removed from the active list.
b3 b6 b1 e6 e1 b5 b2 e3 e5 b4 e4 e2
Figure 19: A coherence-based method of detecting overlaps. The order produced in figure 18 is
nearly correct for this arrangement of intervals. Only b4 and e5 need to be exchanged. When the
exchange occurs, the change in overlap status between interval 4 and 5 is detected.
a sort takes time O(n + c) where c is the number of exchanges necessary. For example, the only
difference between figures 19 and 18 is that interval 4 has moved to the right. Starting from the
ordered list of bi and ei values of figure 18, only a single exchange is necessary to sort the list for
figure 19. The insertion sort is not recommendeded as a sorting procedure in general, since it may
require O(n2 ) exchanges; however, it is a good algorithm for sorting a nearly sorted list, which is
what occurs in our highly coherent environment. To complete the algorithm, note that if two intervals
i and j overlap at the previous time step, but not at the current time step, one or more exchanges
involving either a bi or ei value and a b j or e j value must occur. The converse is true as well when
intervals i and j change from not overlapping at the previous time step to overlapping at the current
time step.
Thus, if we maintain a table of overlapping intervals at each time step, the table can be updated
at each time step with a total cost of O(n + c). Assuming coherence, the number of exchanges c
necessary will be close to the actual number k of changes in overlap status, and the extra O(c − k)
work will be negligible. Thus, for the one-dimensional bounding box problem, the coherence view
yields an efficient algorithm of extreme (if not maximal) simplicity that approaches optimality as
coherence increases.
8 Colliding Contact
For the remainder of these notes, we’re going to be concerned with examining the bodies in our
simulator at a particular instant of time t0 . At this time t0 , we assume that no bodies are inter-
penetrating, and that the simulator has already determined which bodies contact, and at which points.
To simplify matters, we’ll imagine that all bodies are polyhedra, and that every contact point between
bodies has been detected. We’ll consider contacts between polyhedra as either vertex/face contacts
or edge/edge contacts. A vertex/face contact occurs when a vertex on one polyhedra is in contact
with a face on another polyhedra. An edge/edge contact occurs when a pair of edges contact; it is
assumed in this case that the two edges are not collinear. (Vertex/vertex and vertex/edge contacts
are degenerate, and are not considered in these notes.) As examples, a cube resting on a plane would
be described as four vertex/face contacts, one contact at each corner of the cube. A cube resting on
a table, but with its bottom face hanging over the edge of the table would still be described as four
contacts; two vertex/face contacts for the vertices on the table, and two edge/edge contacts, one on
each edge of the cube that crosses over an edge of the table.
Each contact is represented by a structure
struct Contact {
RigidBody *a, /* body containing vertex */
*b; /* body containing face */
triple p, /* world-space vertex location */
n, /* outwards pointing normal of face */
ea, /* edge direction for A */
eb; /* edge direction for B */
bool vf; /* true if vertex/face contact */
};
int Ncontacts;
Contact *Contacts;
If the contact is a vertex/face contact, then the variable a points to the rigid body that the contact
vertex is attached to, while b points to the body the face is attached to. We’ll call these two bodies
A and B respectively. For vertex/face contacts, the variable n is set to the outwards pointing unit
normal of the contact face of body B, and the variables ea and eb are unused.
For edge/edge contacts, ea is a triple of unit length, that points in the direction of the contacting
edge of body A (pointed to by a). Similarly, eb is a unit vector giving the direction that the contact
pa ( t) A
pa ( t0 )
pb ( t) pb ( t0 )
B B
Figure 20: (a) The points pa (t) and pb (t) for a vertex/face contact. (b) At time t0 , the bodies come
into contact at pa (t0 ) = pb (t0 ).
edge on body B points. For edge/edge contacts, n denotes a unit vector in the ea × eb direction.
We’ll adopt the convention that the two contacting bodies are labeled A and B such that the normal
direction ea × eb points outwards from B, towards A, as it does for vertex/face contacts.
For both types of contact, the position of the contact in world space (which is either the contact
vertex, or the point where the two edges intersect) is given by p. The collision detection routines
are responsible for discovering all the contact points, setting Ncontacts to the number of contact
points, and allocating space for and initializing an array of Contact structures.
The first thing we’ll need to do is examine the data in each Contact structure to see if colliding
contact is taking place. For a given contact point, the two bodies A and B contact at the point p. Let
pa (t) denote the particular the point on body A that satisfies pa (t0 ) = p. (For vertex/face contacts,
this point will be the vertex itself. For edge/edge contacts, it is some particular point on the contact
edge of A.) Similarly, let pb (t) denote the particular point on body B that coincides with pa (t0 ) = p
at time t0 (figure 20). Although pa (t) and pb (t) are coincident at time t0 , the velocity of the two points
at time t0 may be quite different. We will examine this velocity to see if the bodies are colliding or
not.
From section 2.5, we can calculate the velocity of the vertex point, ṗa (t0 ) by the formula
ṗa (t0 ) = va (t0 ) + ωa (t0 ) × ( pa (t0 ) − xa (t0 )) (8–1)
where va (t) and ωa (t) are the velocities for body A. Similarly, the velocity of the contact point on
the face of B is
ṗb (t0 ) = vb (t0 ) + ωb (t0 ) × ( pb (t0 ) − xb (t0 )). (8–2)
Let’s examine the quantity
Figure 21: The vector ṗa (t0 ) − ṗb (t0 ) points in the same direction as n̂(t0 ); the bodies are separating.
which is a scalar. In this equation, n̂(t0 ) is the unit surface normal, described by the variable n, for
each contact point. The quantity vrel gives the component of the relative velocity ṗa (t0 ) − ṗb (t0 )
in the n̂(t0 ) direction. Clearly, if vrel is positive, then the relative velocity ṗa (t0 ) − ṗb (t0 ) at the
contact point is in the positive n̂(t0 ) direction. This means that the bodies are moving apart, and that
this contact point will disappear immediately after time t0 (figure 21). We don’t need to worry about
this case. If vrel is zero, then the bodies are neither approaching nor receding at p (figure 22). This
is exactly what we mean by resting contact, and we’ll deal with it in the next section.
In this section, we’re interested in the last possibility, which is vrel < 0. This means that the
relative velocity at p is opposite n̂(t0 ), and we have colliding contact. If the velocities of the bodies
don’t immediately undergo a change, inter-penetration will result (figure 23).
How do we compute the change in velocity? Any force we might imagine acting at p, no matter
how strong, would require at least a small amount of time to completely halt the relative motion
between the bodies. (No matter how strong your car brakes are, you still need to apply them before
you hit the brick wall. If you wait until you’ve contacted the wall, it’s too late...) Since we want
bodies to change their velocity instantly though, we postulate a new quantity J called an impulse.
An impulse is a vector quantity, just like a force, but it has the units of momentum. Applying an
impulse produces an instantaneous change in the velocity of a body. To determine the effects of a
given impulse J, we imagine a large force F that acts for a small time interval 1t. If we let F go to
infinity and 1t go to zero in such a way that
F1t = J (8–4)
then we can derive the effect of J on a body’s velocity by considering how the velocity would change
if we let the force F act on it for 1t time.
For example, if we apply an impulse J to a rigid body with mass M, then the change in linear
contact force
Figure 22: The vector ṗa (t0 ) − ṗb (t0 ) is perpendicular to n̂(t0 ); the bodies are in resting contact. A
contact force may be necessary to prevent bodies from accelerating towards each other.
A
n̂
˙pa ( t 0 ) − p˙ b ( t0 )
Figure 23: Colliding contact. The relative velocity ṗa (t0 ) − ṗb (t0 ) is directed inwards, opposite
n̂(t0 ). Unless the relative velocity is abruptly changed, inter-penetration will occur immediately after
time t0 .
As one would imagine, the impulsive torque τimpulse also gives rise to a change in angular momentum
1L of 1L = τimpulse. The change in angular velocity is simply I −1 (t0 )τimpulse, assuming the impulse
was applied at time t0 .
When two bodies collide, we will apply an impulse between them to change their velocity. For
frictionless bodies, the direction of the impulse will be in the normal direction, n̂(t0 ). Thus, we can
write the impulse J as
J = jn̂(t0 ) (8–7)
where j is an (as yet) undetermined scalar that gives the magnitude of the impulse. We’ll adopt the
convention that the impulse J acts positively on body A, that is, A is subject to an impulse of + jn̂(t0 ),
while body B is subject to an equal but opposite impulse − jn̂(t0 ) (figure 24). We compute j by using
an empirical law for collisions. Let’s let ṗ− a (t0 ) denote the velocity of the contact vertex of A prior
+
to the impulse being applied, and let ṗa (t0 ) denote the velocity after we apply the impulse J. Let
+
ṗ−
b (t0 ) and ṗb (t0 ) be defined similarly. Using this notation, the initial relative velocity in the normal
direction is
v+ −
rel = −vrel . (8–10)
The quantity is called the coefficient of restitution and must satisfy 0 ≤ ≤ 1. If = 1, then
v+ −
rel = −vrel , and the collision is perfectly “bouncy”; in particular, no kinetic energy is lost. At the
other end of the spectrum, = 0 results in v+ rel = 0, and a maximum of kinetic energy is lost. After
this sort of collision, the two bodies will be in resting contact at the contact point p (figure 25).
Calculating the magnitude j of the impulse J = jn̂(t0 ) is fairly simple, although the equations are
a bit tedious to work through. Let’s define the displacements ra and rb as p − xa (t0 ), and p − xb (t0 ).
If we let v− − + +
a (t0 ) and ω a (t0 ) be the pre-impulse velocities of body A, and va (t0 ) and ω a (t0 ) be the
post-impulse velocities, we can write
ṗ+ + +
a (t0 ) = va (t0 ) + ω a (t0 ) × ra (8–11)
A
nˆ( t0 )
B − jˆn( t0 )
Figure 24: The impulse between two bodies at a contact point. An impulse of jn̂(t0 ) acts on A, while
an impulse of − jn̂(t0 ) acts on B.
p p
˙pa−( t0 ) − p˙ b− ( t0 )
(c)
˙pa+( t0 ) − p˙ b+ ( t0 ) (d)
n̂ n̂ ˙pa+( t 0 ) − p˙ b+ ( t0 )
p p
Figure 25: (a) The relative velocity before application of the impulse. (b) The component of the
relative velocity in the n̂(t0 ) direction is reversed for an = 1 collision. The relative velocity
perpendicular to n̂(t0 ) remains the same. (c) A collision with 0 < < 1. The bodies bounce away
in the n̂(t0 ) direction with less speed than they approached. (d) A collision with = 0. The bodies
do not bounce away from each other, but the relative velocity perpendicular to n̂(t0 ) is unaffected by
the collision.
rel , we dot this expression with n̂(t0 ). Since n̂(t0 ) is of unit length, n̂(t0 ) · n̂(t0 ) = 1,
To calculate v+
and we obtain
1 1
= n̂(t0 ) · ( ṗ− −
a (t0 ) − ṗb ) + j + +
Ma Mb
n̂(t0 ) · Ia−1 (t0 ) ra × n̂(t0 ) × ra + n̂(t0 ) · Ib−1 (t0 ) rb × n̂(t0 ) × rb (8–16)
1 1
= v−
rel + j + +
Ma Mb
n̂(t0 ) · Ia−1 (t0 ) ra × n̂(t0 ) × ra + n̂(t0 ) · Ib (t0 ) rb × n̂(t0 ) × rb .
−1
By expressing v+ −
rel in terms of j and vrel , we can compute j according to equation (8–10). If we
substitute equation (8–16) into equation (8–10), we get
1 1
−
vrel + j + + n̂(t0 ) · Ia−1 (t0 ) ra × n̂(t0 ) × ra +
Ma Mb
(8–17)
n̂(t0 ) · Ib (t0 ) rb × n̂(t0 ) × rb = −vrel .
−1 −
−(1 + )v−
j= rel . (8–18)
1
Ma + 1
Mb + n̂(t0 ) · Ia−1 (t0 ) ra × n̂(t0 ) × ra + n̂(t0 ) · Ib−1 (t0 ) rb × n̂(t0 ) × rb
Let’s consider some actual code (written for clarity, not speed). First, we determine if two bodies
are in colliding contact.
/*
* Operators: if ‘x’ and ‘y’ are triples,
* assume that ‘x ˆ y’ is their cross product,
* and ‘x * y’ is their dot product.
*/
/*
* Return true if bodies are in colliding contact. The
* parameter ‘THRESHOLD’ is a small numerical tolerance
* used for deciding if bodies are colliding.
*/
bool colliding(Contact *c)
{
triple padot = pt_velocity(c->a, p), /* ṗ−
a (t0 ) */
pbdot = pt_velocity(c->b, p); −
/* ṗb (t0 ) */
double vrel = c->n * (padot - pbdot); /* v− rel */
Next, we’ll loop through all the contact points until all the collisions are resolved, and actually
compute and apply an impulse.
do {
had_collision = false;
} while(had_collision == true);
}
Note several things. First, = .5 was chosen arbitrarily. In a real implementation, we’d allow the
user to use different values of depending on which two bodies were colliding. Also, every time we
find a collision, we have to rescan the list of contacts, since bodies that were at rest may no longer be
so, and new collisions may develop. If there are initially several collisions to be resolved (such as a
cube dropped flat onto a plane, with all four vertices colliding at once), the order of the contact list
may have an effect on the simulation. There is a way to compute impulses at more than one contact
point at a time, but it more complicated, and is based on the concepts used for resting contact in the
next section. For further information, see Baraff[1].
Incidentally, if you want to have certain bodies that are “fixed”, and cannot be moved (such as
1
floors, or walls), you can use the following trick: for such bodies, let mass be zero; also let the
inverse inertia tensor also be the 3 × 3 zero matrix. You can either special-case the code to check if
a body is supposed to be fixed, or you can recode the definition of RigidBody to have the variable
invmass instead of mass. For ordinary bodies, invmass is the inverse of the mass, while for
fixed bodies, invmass is zero. The same goes for the inertia tensor. (Note that nowhere in any of
the dynamics computations (including the next section) is the mass or inertia tensor ever used; only
their inverses are used, so you won’t have to worry about dividing by zero.) The same trick can be
used in the next section on resting contact to simulate bodies that can support any amount of weight
without moving.
9 Resting Contact
The case of resting contact, when bodies are neither colliding nor separating at a contact point, is the
last (and hardest) dynamics problem we’ll tackle in these notes. To implement what’s in this section,
you’ll have to obtain a fairly sophisticated piece of numerical software, which we’ll describe below.
At this point, let’s assume we have a configuration with n contact points. At each contact point,
bodies are in resting contact, that is, the relative velocity vrel , from section 8, is zero (to within the
numerical tolerance THRESHOLD). We can say that this is so, because colliding contact is eliminated
by the routine FindAllCollisions(), and any contact points with vrel larger than THRESHOLD
can be safely ignored, since the bodies are separating there.
As was the case for colliding contact, at each contact point, we have a contact force that acts
normal to the contact surface. For the case of colliding contact, we had an impulse jn̂(t0 ) where j
was an unknown scalar. For resting contact, at each contact point there is some force f i n̂i (t0 ), where
f i is an unknown scalar, and n̂i (t0 ) is the normal at the ith contact point (figure 26). Our goal is to
determine what each f i is. In computing the f i ’s, they must all be determined at the same time, since
the force at the ith contact point may influence one or both of the bodies of the j contact point. In
section 8, we wrote how the velocity of the contact points pa (t0 ) and pb (t0 ) changed with respect to
j. We’ll do the same thing here, but now we’ll have to describe how the acceleration of pa (t0 ) and
pb (t0 ) depends on each f i .
For colliding contact, we had an empirical law which related the impulse strength j to the relative
velocity and a coefficient of restitution. For resting contact, we compute the f i ’s subject to not one,
but three conditions. First, the contact forces must prevent inter-penetration; that is, the contact
nˆ 3 ( t0 )
p4
B
p3
nˆ 1 (t 0 ) nˆ 2 ( t 0 ) nˆ 4 ( t 0 ) nˆ5 ( t0 ) C
D
p1 p2 p5
Figure 26: Resting contact. This configuration has five contact points; a contact force acts between
pairs of bodies at each contact point.
forces must be strong enough to prevent two bodies in contact from being pushed “towards” one
another. Second, we want our contact forces to be repulsive; contact forces can push bodies apart, but
can never act like “glue” and hold bodies together. Last, we require that the force at a contact point
become zero if the bodies begin to separate. For example, if a block is resting on a table, some force
may act at each of the contact points to prevent the block from accelerating downwards in response
to the pull of gravity. However, if a very strong wind were to blow the brick upwards, the contact
forces on the brick would have to become zero at the instant that the wind accelerated the brick off
the table.
Let’s deal with the first condition: preventing inter-penetration. For each contact point i, we
construct an expression di (t) which describes the separation distance between the two bodies near the
contact point at time t. Positive distance indicates the bodies have broken contact, and have separated
at the ith contact point, while negative distance indicates inter-penetration. Since the bodies are in
contact at the present time t0 , we will have di (t0 ) = 0 (within numerical tolerances) for each contact
point. Our goal is to make sure that the contact forces maintain di (t) ≥ 0 for each contact point at
future times t > t0 .
For vertex/face contacts, we can immediately construct a very simple function for di (t). If pa (t)
and pb (t) are the contact points of the ith contact, between bodies A and B, than the distance between
the vertex and the face at future times t ≥ t0 is given by
At time t, the function d(t) measures the separation between A and B near pa (t). If di (t) is zero,
then the bodies are in contact at the ith contact point. If di (t) > 0, then the bodies have lost contact
at the ith contact point. However, if di (t) < 0, then the bodies have inter-penetrated, which is what
we need to avoid (figure 27). The same function can also be used for edge/edge contacts; since n̂i (t)
A
pa ( t) A
A
nˆ( t)
nˆ( t) pa ( t) nˆ( t) pb ( t)
pb ( t) pb ( t)
pa ( t)
B B B
Figure 27: (a) The displacement pa (t) − pb (t), indicated by an arrow, points in the same direction
as n̂(t). Thus, the distance function d(t) would be positive. (b) The distance function d(t) is zero.
(c) The displacement pa (t) − pb (t) points in the opposite direction as n̂(t). The distance function
d(t) is negative, indicating inter-penetration.
points outwards from B towards A (by convention), n̂i (t) · ( pa (t) − pb (t)) will be positive if the
two contacting edges move so as to separate the bodies.
Since di (t0 ) = 0, we have to keep di (t0 ) from decreasing at time t0 ; that is, we have to have
di (t0 ) ≥ 0. What is d˙i (t0 )? Differentiating,
˙
d˙i (t) = n̂˙ i (t) · ( pa (t) − pb (t)) + n̂i (t) · ( ṗa (t) − ṗb (t)). (9–2)
Since di (t) describes the separation distance, d˙i (t) will describe the separation velocity at time t.
However, at time t0 , pa (t0 ) = pb (t0 ), which means that d˙i (t0 ) = n̂i (t0 ) · ( ṗa (t0 ) − ṗb (t0 )). This
should look familiar: its vrel from the previous section! The function d˙i (t0 ) is a measure of how
the bodies are separating, and for resting contact, we know that d(t ˙ 0 ) is zero, because the bodies are
neither moving towards nor away from each other at a contact point.
At this point then, we have di (t0 ) = d˙i (t0 ) = 0. Now we’ll look at d¨i (t0 ). If we differentiate
equation (9–2), we get
d¨i (t) = n̂¨ i (t) · ( pa (t) − pb (t)) + n̂˙ i (t) · ( ṗa (t) − ṗb (t)) +
n̂˙ i (t) · ( ṗa (t) − ṗb (t)) + n̂i (t) · ( p̈a (t) − p̈b (t)) (9–3)
= n̂¨ i (t) · ( pa (t) − pb (t)) + 2n̂˙ i (t) · ( ṗa (t) − ṗb (t)) + n̂i (t) · ( p̈a (t) − p̈b (t)).
¨ 0 ) = n̂i (t0 ) · ( p̈a (t0 ) − p̈b (t0 )) + 2n̂˙ i (t0 ) · ( ṗa (t0 ) − ṗb (t0 )).
d(t (9–4)
for each contact point. Since the acceleration d¨i (t0 ) depends on the contact forces, this is really a
constraint on the contact forces.
Let’s turn our attention to the second and third constraints. Since contact forces must always
be repulsive, each contact force must act outward. This means that each f i must be positive, since
a force of f i n̂i (t0 ) acts on body A, and n̂i (t0 ) is the outwards pointing normal of B. Thus, we
need
fi ≥ 0 (9–6)
for each contact point. The third constraint is expressed simply in terms of f i and d¨i (t0 ). Since the
contact force f i n̂i (t0 ) must become zero if contact is breaking at the ith contact, this says that f i must
be zero if contact is breaking. We can express this constraint by writing
if contact is breaking, d¨i (t0 ) > 0 and equation (9–7) is satisfied by requiring f i = 0. If contact is not
breaking, then d¨i (t0 ) = 0, and equation (9–7) is satisfied regardless of f i .
In order to actually find f i ’s which satisfy equations (9–5), (9–6), and (9–7), we need to express
each d¨i (t0 ) as a function of the unknown f i ’s. It will turn out that we will be able to write each d¨i (t0 )
in the form
where A is the n × n matrix of the aij coefficients of equation (9–8). Although the code needed to
calculate the aij ’s and the bi ’s is not too complicated, working out the derivations on which the code
is based is somewhat tedious. The derivations are worked out in appendix D, along with code to
compute the matrix of aij ’s and bi ’s.
Appendix D gives an implementation of the routines
where the types bigmatrix and vector represent matrices and vectors of arbitrary size. The first
routine computes the aij ’s, while the second routine computes the bi ’s.
Once we’ve computed all this, we can think about solving equations (9–5), (9–6), and (9–7).
This system of equations forms what is called a quadratic program (QP); that is, f i ’s that satisfy these
three equations are found by an algorithm called quadratic programming. Not all quadratic programs
can be solved efficiently, but because our contact forces are all normal to the contact surfaces (that is,
they do not involve friction), it turns out that our QP can always be solved efficiently. One interesting
thing to note is that QP codes can easily handle the case d¨i (t0 ) = 0 instead of d¨i (t0 ) ≥ 0. We use
d¨i (t0 ) = 0 (and also drop the constraint f i ≥ 0) if we wish to constrain two bodies to never separate
at a contact point. This enables us to implement hinges, and pin-joints, as well as non-penetration
constraints during simulation.
Quadratic programming codes aren’t terribly common though; certainly, they are not nearly as
common as linear equation codes, and are much harder to implement. The quadratic programming
routines used by the author were obtained from the Department of Operations Research at Stanford
University. See Gill et al.[7, 8, 9] for further details. More recently, we have been using code
described by Baraff[3] to solve the quadratic programs. If you are determined to really implement
this, we suggest a thorough study of this paper (excepting for the section on contact with friction).
At any rate, let’s assume that you’ve got a working QP solver at your disposal. We’ll assume
that you pass the matrix A, and the vector of bi ’s to the QP solver, and you get back the vector of
f i ’s. Let’s pretend the interface is
Let’s see how to compute all the resting contact forces. The following routine is presumably called
from ComputeForceAndTorque(), after FindAllCollisions() has been called.
/*
* Allocate n × n matrix ‘amat’ and n-vectors ‘fvec’, and ‘bvec’.
*/
/* Solve for f j ’s */
qp_solve(amat, bmat, fvec);
A->force += f * n;
A->torque += (contacts[i].p - A->x) * (f*n);
/* and negatively to B */
B->force -= f * n;
B->torque -= (contacts[i].p - B->x) * (f*n);
}
}
That’s pretty much it! Now that the resting forces have been computed and combined with the
external forces, we return control to the ODE solver, and each body goes merrily along its way, in a
physically correct manner, without inter-penetration.
where ṙ i (t) is the velocity of the ith particle. The net work over all the particles is the sum
X Z t1 Z t1 X
Fci (t) · ṙ i (t) dt = Fci (t) · ṙ i (t) dt,
i t0 t0 i
which must be zero for any interval t0 to t1 . This means that the integrand
X
Fci (t) · ṙ i (t) (A–1)
i
P
is itself always zero for any time t. (Henceforth we’ll just write these expressions as Fci · ṙ i = 0.)
We can use this fact to eliminate any mention of the constraint forces Fci from our derivations.
First, some quick notes about the “∗” operator defined in section 2.3: since a∗ b = a × b, and
a × b = −b × a, we get
−a∗ b = b × a = b∗ a. (A–2)
d
(ȧ)∗ = (a˙∗ ) = (a∗ ) (A–4)
dt
and for a set of vectors ai
X X ∗
a∗i = ai . (A–5)
Note that this equation must hold for arbitraryP values of v and ω. Since v and ω are completely
independent, if we choose P ω to be zero, then Fci · v = 0 for any choice of v, from which we
conclude that in fact Fci = 0 is always true. This means
P that the constraint forces produce no
net force. Similarly, choosing v to be zero we see that −Fci · (ri0 ∗ ω) = 0 for any ω. Rewriting
Fci · (ri0 ∗ ω) as Fci T (ri0 ∗ ω) we get that
X X
∗ ∗
−Fci T ri0 ω = −Fci T ri0 ω = 0 (A–8)
P
for any ω, so −Fci T ri0 ∗ = 0T . Transposing, we have
X ∗
X X
−(ri0 ) T Fci = (ri0 )∗ Fci = ri0 × Fci = 0 (A–9)
∗ ∗ ∗ ∗ ∗ ∗
ri0 mi (v̇ − ṙ 0i ω − ri0 ω̇) − ri0 Fi − ri0 Fci = ri0 0 = 0. (A–15)
We’re almost done now: if we refer back to the matrix defined by the “∗” notation, one can easily
verify the relation that the matrix −a∗ a∗ is equivalent to the matrix (a T a)1 − aa T where 1 is the
3 × 3 identity matrix. (This relation is equivalent to the vector rule a × (b × c) = ba T c − ca T b.)
Thus
X ∗ ∗
X
−mi ri0 ri0 = mi ((ri0 ri0 )1 − ri0 ri0 ) = I(t).
T T
(A–20)
The above expression is almost acceptable,Pas it gives an expression for ω̇ in terms of τ, ex-
cept that it requires us to evaluate the matrix miri0 ∗ṙ ∗i , which is as expensive as computing the
inertia tensor from scratch. We’ll use one last trick here to clean things up. Since ṙi0 = ω × ri0 and
ri0 ∗ ω = −ω × ri0 , we can write
X ∗ ∗
X X
mi ṙ0i ri0 ω = mi (ω × ri0 )∗ (−ω × ri0 ) = −mi (ω × ri0 ) × (ω × ri0 ) = 0. (A–22)
Finally, since
d X ∗ ∗
X ∗ ∗ ∗ ∗
İ(t) = −miri0 ri0 = −miri0 ṙ0i − miṙ 0i ri0 (A–24)
dt
we have
d
İ(t)ω + I(t)ω̇ = (I(t)ω) = τ. (A–25)
dt
Since L(t) = I(t)ω(t), this leaves us with the final result that
L̇(t) = τ. (A–26)
The product [0, ω(t0 )] q(t0 ) is abbreviated to the form ω(t0 )q(t0 ); thus, the general expression for
q̇(t) is
X X X (C–2)
∗ ∗ ∗
= 1
2 mi v v + T
v T
miri0 ω + 12 mi (ri0 ω) T (ri0 ω)
X X ∗ X
∗ T ∗
= 12 v T mi v + v T miri0 ω + 12 ω T mi (ri0 ) ri0 ω.
P
mi ri0 = 0 and (ri0 ∗ ) = −ri0 ∗ , we have
T
Using
X
∗ ∗
T = 12 v T Mv + 12 ω T −miri0 ri0 ω = 12 (v T Mv + ω T Iω) (C–3)
P
since I = −miri0 ∗ ri0 ∗ from appendix A. Thus, the kinetic energy can be decomposed into two
terms: a linear term 12 v T Mv, and an angular term 12 ω T Iω.
Since we know that L̇(t) = τ(t), let us consider İ −1 (t). From equation (2–40),
I −1 (t) = R(t)Ibody
−1
R(t) T ,
so
İ −1 (t) = Ṙ(t)Ibody
−1 −1
R(t) T + R(t)Ibody Ṙ(t) T . (C–5)
This yields
İ −1 (t) = Ṙ(t)Ibody
−1 −1
R(t) T + R(t)Ibody (−R(t) T ω(t)∗ )
= ω(t)∗ R(t)Ibody
−1
R(t) T − I −1 (t)ω(t)∗ (C–8)
Then
But since I −1 (t)L(t) = ω(t), the first term, ω(t)∗ I −1 (t)L(t) is equivalent to ω(t)∗ ω(t), or ω(t) × ω(t),
which is zero. This leaves the final result of
Then
We can interpret this as follows. The first term, ω̇(t) × r(t) is the tangential acceleration of the point;
that is, ω̇(t) × r(t) is the acceleration perpendicular to the displacement r(t) as a result of the body
being angularly accelerated. The second term, ω(t) × (ω(t) × r(t)) is the centripetal acceleration of
the point; this centripetal acceleration arises because the body is rigid, and points on the body must
rotate in a circular orbit about the center of mass. The last term, v̇(t) is the linear acceleration of the
point due to the linear acceleration of the center of mass of the body.
D.1 Derivations
We need to express d¨i (t0 ) in terms of all the unknown f i ’s. It will turn out that we’ll be able to write
each d¨i (t0 ) in the form
¨ 0 ) = n̂i (t0 ) · ( p̈a (t0 ) − p̈b (t0 )) + 2n̂˙ i (t0 ) · ( ṗa (t0 ) − ṗb (t0 ))
d(t (D–2)
where pa (t0 ) = pi = pb (t0 ) is the contact point for the ith contact at time t0 . The right-most
term 2n̂˙ i (t0 ) · ( ṗa (t0 ) − ṗb (t0 )) is a velocity dependent term (i.e. you can immediately calculate
it without knowing the forces involved), and is part of bi , so we’ll ignore this for now.
So we only need to know how p̈a (t0 ) and p̈b (t0 ) depend on f j , the magnitude of the jth contact
force. Consider the jth contact. If body A is not one of the bodies involved in the jth contact, then
p̈a (t0 ) is independent of f j , because the jth contact force does not act on body A. Similarly, if B is
also not one of the two bodies involved in the jth contact, then p̈b (t0 ) is also independent of f j . (For
example, in figure 26, the acceleration of the contact points at the first contact is completely unaf-
fected by the contact force acting at the fifth contact. Thus, d¨1 (t0 ) would be completely independent
of f5 . Conversely, d¨5 (t0 ) is completely independent of f1 .)
Suppose though that in the jth contact, body A is involved. For definiteness, suppose that in the
jth contact, a force of jn̂ j (t0 ) acts on body A, as opposed to − jn̂ j (t0 ). Let’s derive how p̈a (t0 ) is
affected by the force jn̂ j (t0 ) acting on A.
From equation (C–12), we can write
p̈a (t) = v̇a (t) + ω̇a (t) × ra (t) + ωa (t) × (ωa (t) × ra (t)) (D–3)
where ra (t) = pa (t) − xa (t), and xa (t), va (t), and ωa (t) are all the variables associated with body
A. We know that v̇a (t) is the linear acceleration of body A, and is equal to the total force acting on
A divided by the mass. Thus, a force of jn̂ j (t0 ) contributes
f j n̂ j (t0 ) n̂ j (t0 )
= fj (D–4)
ma ma
to v̇a (t) and thus p̈a (t). Similarly, consider ω̇a (t), from equation (C–10):
where τa (t) is the total torque acting on body A. If the jth contact occurs at the point p j , then the
force jn̂ j (t0 ) exerts a torque of
( p j − xa (t0 )) × f j n̂ j (t0 ).
But we also have to take into account the contributions to p̈a (t0 ) and p̈b (t0 ) due to known ex-
ternal forces such as gravity, as well as the force-independent terms ωa (t0 ) × (ωa (t0 ) × ra ) and
(Ia−1 (t0 )(Ła (t0 ) × ωa (t0 ))) × ra . If we let the net external force acting on A be Fa (t0 ), and the net
external torque be τa (t0 ), then from equations (D–4) and (D–5), we get that Fa (t0 ) contributes
Fa (t0 )
ma
and that τa (t0 ) contributes
Ia−1 (t0 )τa (t0 ) × ra .
Thus, the part of p̈a (t0 ) that is independent from all the f j ’s is
Fa (t0 )
+ Ia−1 (t0 )τa (t0 ) × ra + ωa (t0 ) × (ωa (t0 ) × ra ) + Ia−1 (t0 )(Ła (t0 ) × ωa (t0 )) × ra
ma
and similarly for p̈b (t0 ). To compute bi , we combine the constant parts of p̈a (t0 ), p̈b (t0 ), dot with
n̂i (t0 ), and add the term 2n̂˙ i (t0 ) · ( ṗa (t0 ) − ṗb (t0 )).
n1 = n1 / length; /* normalize */
b[i] = k1 + k2;
}
}
Computing the aij terms is a little more tricky, because we have to keep track of how the jth
contact force affects the ith contact point. The following routine is not the most efficient way to do
things, because with a good data structure, you can tell in advance which of the aij ’s are going to
be zero. Still unless you’re working with really huge numbers of contacts, not too much extra work
will be done.
Body *A = ci.a,
*B = ci.b;
triple ni = ci.n, /* n̂i (t0 ) */
nj = cj.n, /* n̂ j (t0 ) */
pi = ci.p, /* ith contact point location */
pj = cj.p, /* jth contact point location */
ra = pi - A->x,
rb = pi - B->x;
if(cj.a == ci.a)
{
/* force direction of jth contact force on A */
force_on_a = nj;
/* torque direction */
torque_on_a = (pj - A->x) ˆ nj;
}
else if(cj.b == ci.a)
{
force_on_a = - nj;
torque_on_a = (pj - A->x) ˆ nj;
}
if(cj.a == ci.b)
{
/* force direction of jth contact force on B */
force_on_b = nj;
/* torque direction */
torque_on_b = (pj - B->x) ˆ nj;
}
else if(cj.b == ci.b)
{
force_on_b = - nj;
torque_on_b = (pj - B->x) ˆ nj;
}
/* Now compute how the jth contact force affects the linear
and angular acceleration of the contact point on body A */
/* Same for B */