ConstrainedDynamics
ConstrainedDynamics
Constrained Dynamics
Andrew Witkin
Pixar Animation Studios
Please note: This document is 2001 by Andrew Witkin. This chapter may be
freely duplicated and distributed so long as no consideration is received in return,
and this copyright notice remains intact.
Constrained Dynamics
Andrew Witkin
Pixar Animation Studios
2 A bead on a wire
We introduce the approach using the simple example of a 2D particle constrained to move on the
unit circle. We can express the constraint by writing a scalar behavior function, as we did in Chapter
C to create energy functions,
1
C(x) = (x · x − 1), (1)
2
F1
C=0 Point-on-circle constraint:
C=0
C= 1
2
( x⋅x - 1 )
so that the legal positions of x are all those that satisfy C(x) = 0. The function C is an implicit
function for the constraint. If x is a legal position, then the legal velocities are all those that satisfy
Ċ = x · ẋ = 0. (2)
C̈ = ẍ · x + ẋ · ẋ = 0. (3)
If we start out with a legal position and velocity, then to maintain the constraint, in principal, we
need only ensure that equation 3 is satisfied at every instant thereafter. See figure 1 The particle’s
acceleration is
f + f̂
ẍ = , (4)
m
where f is the given applied force, and f̂ is the as yet unknown constraint force. Substituting for ẍ in
equation 3 gives
f + f̂
C̈ = · x + ẋ · ẋ = 0, (5)
m
or
f̂ · x = −f · x − m ẋ · ẋ. (6)
f̂ · ẋ = 0, ∀ẋ | x · ẋ = 0.
This condition simply states that f̂ must point in the direction of x, so we can rewrite the constraint
force as
f̂ = λx,
where λ is an unknown scalar. Substituting for f̂ in equation 6, and solving for λ gives
−f · x − m ẋ · ẋ
λ= . (7)
x·x
Having solved for λ, we calculate f̂ = λx, then ẍ = (f̂ + f)/m, and proceed with the simulation in
the usual way.
In its general form, the condition we imposed on f̂ is known as the principal of virtual work.[2]
See figure 2 for an illustration.
2.2 Feedback
If we were solving the differential equation exactly, this procedure would keep the particle exactly
on the unit circle, provided we began with valid initial conditions. In practice, we know that nu-
merical solutions to ODE’s drift. We must add an extra feedback term to prevent this numerical
drift from accumulating, turning the circle into an outward spiral. The feedback term can be just a
damped spring force, pulling the particle back onto a unit circle. The feedback force needs to be
added in after the constraint force calculation, or else the constraint force will dutifully cancel it!
We will discuss feedback in more detail in the next section.
Constraint Forces
Figure 2: In the case of a point-on-circle constraint, the principle of virtual work simply requires
the constraint force to lie in a direction normal to the circle.
When ẋ is nonzero, we unfortunately lose this simple geometric picture, but we can still interpret
the addition of the constraint force as a projection. Rather than a force projection, it is the projection
of the acceleration onto the set of legal accelerations. The velocity-dependent term in equation 7
ensures that the curvature of the particle’s trajectory matches that of the circle. some effort, we could
try to regain the geometric picture by visualizing a projection in phase space, but this is hardly worth
the trouble.
Q̂ · ẋ = 0, ∀ẋ | Jẋ = 0.
All and only vectors Q̂ that satisfy this requirement can be expressed in the form
Q̂ = JT λ,
which is a matrix equation in which all but λ are known. The matrix JWJT is a square matrix with
the dimensions of C. Once λ is obtained, it is multiplied by JT to obtain Q̂, which is added to the
applied force before calculating acceleration.
We already noted the need for a feedback term to prevent the accumulation of numerical drift.
This term can be incorporated directly into the constraint force calculation. Instead of solving for
C̈ = 0, as we did above, we solve for
C̈ = −ks C − kd Ċ,
where ks and kd are spring and damping constants. By adding this term, we make the constraint
force perform the extra function of returning, with damping, to a valid state should drift occur. The
final constraint force equation, with feedback, is
The values assigned to ks and kd are not critical, since this term only plays the relatively undemand-
ing role of absorbing drift. See [1, 3, 5] for further discussion.
2. Calculate forces: loop over all force objects, allowing each to add forces to the particles it
influences.
3. Calculate constraint forces: On completion of the previous step, each particle’s force accu-
mulator contains the total force on that particle. In this step, the global equation 11 is set up
and solved, yielding a constraint force on each particle, which is added into the applied force.
4. Calculate the derivative: Divide force by mass to get acceleration, and gather the derivatives
into a global vector for the solver.
In this section, we are concerned exclusively with the third step in this sequence.
Figure 3: The block-sparse Jacobian matrix. The constraints are shown above, and the particles
along the right side. Each constraint/particle pair corresponds to a block in the matrix. A block is
non-zero only if the constraint depends on the particles. Here, we show a binary constraint, such as
a distance constraint, that connects two particles. The two shaded blocks are that constraint’s only
contributions to the sparse Jacobian.
struct{
int i;
int j;
int ilength;
int jlength;
float *data;
};
To support constrained particle dynamics using block-sparse matrices, as we will soon see,
we must implement only two operations: matrix times vector, and matrix-transpose times vector.
Both are simple operations, looping over the matrix blocks, and performing an ordinary matrix
multiplication for each block, using the block’s i and j as offsets into the destination and source
vectors.
4.5 Summary
To introduce constraints into a particle system simulation, we add the additional step of constraint
force calculation to the derivative evaluation operation. After the ordinary applied forces have been
calculated, but before computing accelerations, we perform the following steps:
• Loop over the constraints, letting each evaluate its own portion of C, Ċ, J and J̇. Each
constraint points to one or more matrix blocks that receive its contributions to the global
matrices.
• Invoke the conjugate gradient solver to obtain the Lagrange multiplier vector, λ.
• Multiply λ by JT to obtain the global constraint force vector, and scatter this force to the
particles.
[2] Herbert Goldstein. Classical Mechanics. Addision Wesley, Reading, MA, 1950.
[3] John Platt and Alan Barr. Constraint methods for flexible models. Computer Graphics, 22:279–
288, 1988.
[4] W.H. Press, B.P. Flannery, S. A. Teukolsky, and W. T. Vetterling. Numerical Recipes in C.
Cambridge University Press, Cambridge, England, 1988.
[5] Andrew Witkin, Michael Gleicher, and William Welch. Interactive dynamics. Computer Graph-
ics, 24, 1990. Proc. 1990 Symposium on 3-D Interactive Graphics.
[6] Andrew Witkin and William Welch. Fast animation and control of non-rigid structures. Com-
puter Graphics, 24(4):243–252, July 1990. Proc. Siggraph ’90.