You are on page 1of 10

Attention:

This material is copyright  1995-1997 Chris Hecker. All rights


reserved.

You have permission to read this article for your own education. You
do not have permission to put it on your website (but you may link
to my main page, below), or to put it in a book, or hand it out to your
class or your company, etc. If you have any questions about using
this article, send me email. If you got this article from a web page
that was not mine, please let me know about it.

Thank you, and I hope you enjoy the article,

Chris Hecker
definition six, incorporated
checker@d6.com
http://www.d6.com/users/checker

PS. The email address at the end of the article is incorrect. Please
use checker@d6.com for any correspondence.
by Chris Hecker BEHIND THE SCREEN

Physics, Part 4:
The Third Dimension

I
t pains me to say it, but this is going to be my last column for a while. Writing
these columns takes a lot of time, and right now I need to devote all my waking
hours to my startup game company and to shipping our first game. Still, I’m
going to stay on as Game Developer’s Editor-at-Large, so I will have input on the
magazine — I might even write an arti- matics will tell us how to represent the In order to answer this question and
cle during the hiatus — but this is the 3D object’s configuration and its deriv- keep this article only two times larger 15
last official Behind the Screen for at atives, and the dynamics will tell us than normal, I’m now forced to skip a
least a year. I love writing this column, how to relate these kinematic quanti- ton of math. Rotation in 3D is an
so you can be sure I’ll be back as soon ties to each other and to external forces incredibly rich subject with deep ties to
as possible. In the meantime, remember and torques. For the most part, the almost every field in mathematics. The
that one of the reasons I write these transition from 2D to 3D is intuitive, classical mechanics text by Goldstein in
articles is to encourage information but as you’ll see, the lack of a good the references on my web site (the
sharing among game developers — if parameterization for 3D orientation URL’s at the end of the article) has a 50-
you have an idea for an article you’d mucks up the works a bit. But I’m get- page chapter on 3D orientation, and
like to write, don’t hesitate to propose it ting ahead of myself…. yet there are still plenty of places in the
to the editor. The more information we
share, the faster our industry advances, A swan song if you will, a desperate dash for
and that’s good for everybody.
As my swan song, I’m giving you this closure if you won’t. The physics series
monster of an article to finish up the
physics series. Twice the length! Twice comes to a roaring conclusion by applying all
the number of equations! Twice as late
turning it in to my editor! Off we go! you’ve learned so far to the deep dimension.
chapter where Goldstein has to rein
Prelude himself in to stay on course. Given the
ersonally, I think 2D physics is 3D Kinematics impossibility of covering orientations
P really cool. Still, you’ll never forget irst, the easy part. The equations for even superficially, we need to be con-
the first time you see a physically simu-
lated 3D object careen off a wall —
F 3D linear kinematics (position,
velocity, and acceleration) are exactly
tent to spend only the next paragraph
rationalizing our choice of representa-
especially when you wrote the simula- the same as for their 2D counterparts. tion, and then move on to describe our
tor yourself. Also, for better or for The two-element vectors turn into representation’s properties.
worse, most of the games coming out three-element vectors, and you’re done. There are three angular degrees of
these days are 3D. Unless you’re writ- Unfortunately, it’s not so easy when freedom in 3D (the three linear and
ing the world’s most realistic side- we take 3D orientation into account. three angular degrees of freedom add
scroller, you’re going to need the 3D Consider the wonderfully elegant rep- up to the oft-heard “6DOF”), so we
equivalents of the first three colunms resentation of an orientation in 2D: a need to use at least three scalars to
in this series. This installment is huge scalar. It’s hard to get simpler than this describe an arbitrary orientation. At
because I’m going to cram all three and still represent some useful infor- this point, math deals us a temporary
into a single column. To do this, I’m mation. As we’ve seen, the orientation setback. It’s possible to prove that no
going to have to assume you know the value Ω, its time derivative ω, and its three-scalar parameterization of 3D ori-
material from the first three columns, second derivative α are all scalars that entation exists that doesn’t suck, for
so you might want to go back and read nicely parameterize any orientation some suitably mathematically rigorous
them again before going any farther. and change of orientation in two definition of “suck.” I haven’t done
Like the previous articles, this one is dimensions. However, a single scalar this proof (I think it uses some pretty
divided into a section on kinematics clearly isn’t going to cut it for 3D ori- high-end group theory, which I
and a section on dynamics. The kine- entation. But what representation will? haven’t learned yet), so I can’t tell you

http://www.gdmag.com JUNE 1997 G A M E D E V E L O P E R


BEHIND THE SCREEN

always constrained to be a constant orthogonal 3×3 matrix is a rotation. As


F I G U R E 1 . Axis-angle rotation. value, then the tip of the 3D vector long as we enforce the six constraints of
must always move in a plane. This 3D Eq. 1, we have a valid rotation. As a side
vector minus the single scalar con- note, those of you who have used 3×3
straint leaves us with only two degrees matrices to represent orientations have
of freedom to move in the plane — this probably run into problems when the
is the same as using a 2D vector in the orthogonality constraints were not
y first place. As a rule, the original num- enforced in the face of numerical inac-
o ber of unconstrained degrees of free- curacy. In this case, your “rotation
θ dom minus the number of scalar con- matrix” probably started scaling and
straint equations leaves us with our shearing your objects instead of just
x final number of degrees of freedom. rotating them.
n r' This concept of degrees of freedom and We’re still in mathematical fast-for-
constraint equations becomes incredi- ward mode, so I’ll just point out that a
φ r bly important as you learn more about special orthogonal matrix operates on
dynamics (and about math in general). (or rotates) a vector through plain old
Mull this over for a while until you’re matrix multiplication. This is one rea-
comfortable with the idea. son a 3×3 matrix is a more convenient
Now, as I was saying, we’re going to orientation representation than a set of
16 exactly how it works, but I believe the use 3×3 matrices to represent the orien- Euler angles — Euler angles require eval-
gist of the proof is that no minimal tations of our rigid body. Clearly, an uating trig functions to rotate a vector.
parameterization exists that doesn’t arbitrary 3×3 matrix has nine degrees Also, the matrix product of two special
contain singularities. These singulari- of freedom (one for each scalar in the orthogonal matrices is the cumulative
ties can take different forms — depend- matrix), so we’re going to need some rotation (applying the product BA to a
ing on how you allocate the three constraints to get down to the three column vector is the same as applying A
degrees of freedom — but according to degrees of freedom needed to represent and then B), which means the product
the math, it’s impossible to get rid of a 3D orientation1. We get these con- must be another special orthogonal
them. Anyone who’s played around straints by restricting our matrices to matrix. Finally, matrix multiplication is
with the most common minimal para- be special orthogonal. The “special” part not commutative (BA is different than
meterization of 3D, the set of three means the matrix is not a reflection — AB). This mirrors the noncommutativi-
Euler angles (roll, pitch, and yaw are it can’t turn a right-handed coordinate ty of rotations; it’s easy to construct a
one possible set), has probably run into system into a left-handed one. The sequence of rotations that, when per-
some of these singularities. Luckily, we “orthogonal” part comes from the fol- formed in a different order, result in a
aren’t forced to use only three parame- lowing matrix equation: different final orientation.
ters. We can avoid the singularities by I want to take a step back at this
AA T = 1 (Eq. 1)
using more parameters, as long as we point and explain why I’m being
constrain our multiple parameters In English, A times its transpose, AT, slightly strange in my discussion of
down to three degrees of freedom. This yields the identity matrix, or put anoth- rotation matrices. Don’t I understand
is exactly what we’re going to do by er way, AT = A-1 — transpose is the that everyone knows that a matrix can
chosing 3×3 matrices to represent our inverse. Eq. 1 also implies ATA = 1. The rotate a vector, and that matrix con-
orientations. theory of orthogonal matrices is at least catenation works? Sure, and in fact I’m
Even though I said we’d only use one as large as that of 3D orientations, so counting on you knowing this since I
paragraph to rationalize our orienta- again I’m only going to touch on the don’t have room to explain that stuff
tion parameterization, I’m going to highlights that directly affect us. Eq. 1 in this column. However, I’ve found
cheat a bit and spend another para- gives us our six constraint equations most computer graphics-oriented text-
graph describing what I mean by “con- because it’s a bunch of dot products of books only explain how to construct
strain those parameters down.” As a the rows of A. Three constraints come rotation matrices (“put the sines and
relatively intuitive example, let’s say from the 1s on the diagonal of the iden- cosines in these places”), but they
we want to represent a 2D position. tity matrix, meaning the rows are unit don’t talk about many of the formal
The obvious way to do this is to use a length. The other three constraints are properties of rotation matrices. In
2D vector and be done with it. If we from the 0s, meaning the rows are all at dynamics, after giving our objects their
were feeling particularly nonoptimal — right angles to each other. Those con- initial orientations, we never again
or if there was some problem with straints bring us down to exactly three construct rotation matrices from
using 2D vectors — we could use a 3D degrees of freedom in A. Most people scratch. Our orientations evolve as a
vector to represent 2D position, as long are aware that 3D rotations are orthogo- result of the integration we perform on
as the tip of that vector was con- nal and obey Eq. 1, but it’s also possible the dynamic system — knowing how
strained to move in a plane. We could to prove the converse: that any special to create a rotation around the z-axis
implement this constraint with a single doesn’t help us much. Another impor-
1Lots of people use objects called quaternions to
dot product equation. If the dot prod- tant point is that the 3×3 matrix is the
represent orientations. Quaternions have four
uct of our variable 3D position vector parameters and need one constraint. Usually the orientation. In graphics, we learn to
with another constant vector was quaternion is constrained to be unit length. use matrices to cause rotations, but in

G A M E D E V E L O P E R JUNE 1997 http://www.gdmag.com


this column, the matrix simply is the Neat trick, huh? You could use Eq. 2 to way of saying the vector sum
orientation representation (in addition find a matrix that will rotate an object o + rcosθx + rsinθy or, “start at the ori-
to having the nice property that it around its local x-axis in world space: gin o, go rcosθ units down the x vector,
causes the rotation when multiplied Create a B that’s an x-axis rotation, and then rsinθ units down the y vec-
with a vector).We’re not, for example, then use A, the local-to-world transfor- tor.” So, all we need to do is to form
using Euler angles and converting mation, to similarity-transform B the vectors o, x, and y in the 3D space,
them to matrices in order to operate on (although in this case, it’s probably eas- then apply the 2D rotation formula to
vectors with them; we’re storing the ier just to rotate the object around the them.
matrix as our only representation of local x-axis when it’s in local space First, we define the origin. The origin
our objects’s orientation. So, if some- before applying A, but if you didn’t is the vector parallel to n with its tip
one asks for object A’s orientation, we have the original r you’d need Eq. on the plane of rotation. We can form
hand him or her the whole 3×3 matrix, 2…hey, it’s just an example). this vector by projecting r onto n with
with assurances the matrix is special a dot product, then moving the result-
orthogonal so it really does represent ing distance down n.
an orientation. If we don’t make sure Axis and Angle
o = nT rn (Eq. 3)
it’s special orthogonal, our orientation e’ve decided on our kinematic
representation won’t work properly.
While we gain simplicity over Euler
W representation for orientation,
but we still need to pick representa-
Eq. 3 uses the “matrix notation” for
the dot product. If we transpose the
angles, we give back some of that gain tions for the kinematic derivatives: column vector n, we get a row vector.
because we’re required to enforce the angular velocity and angular accelera- A row vector times a column vector r is 17
constraints on our matrices. I wish I tion. To do that, we need to explore equivalent to a dot product and results
could spend more time going into the our orientation representation in a lit- in a scalar (for matrices, 1×n * n×1 = 1×1).
subtleties of 3D orientation, but I can’t, tle more detail. I’ll give you one more The o vector moves us to the plane of
so you’ll have to discover them for unproven fact, then we’ll slow down rotation. We can trivially define the x
yourself from the references. Anyway, and derive some results ourselves. vector as the difference between the tip
bear with me: Take your current knowl- The fact is that any rotation (and this of r and the o vector.
edge of matrices, add it to anything includes all combinations of rotations)
x = r − nT rn (Eq. 4)
new you learn here, and realize that can be described by a single unit vector
we’re talking about the same matrices and a rotation angle around that vector. Note that we aren’t normalizing x
in the end — now you just see them This means you can concatenate any because its length is exactly what we
from a different side. convoluted sequence of rotations you want: the radius of the rotation circle r.
To warm up for the equation manipu- like, and if you simply tell me the start Finally, we form the y vector using a
lation to come, let’s prove a fundamen- and the end orientations, I can give you cross product of n and r.
tal result for orthogonal matrices. We’ll back a unit vector and a scalar encapsu-
y = n×r (Eq. 5)
use this result later. Start with a rotation lating the change in orientation. The
matrix A that transforms any vector r to scalar tells how far to rotate around the The cross product forms a y that is
r′ by r′ = Ar. Now, say we want to be vector. This rotation will take you from perpendicular to both n and r, and
able to apply a (possibly nonrotation) your start to your end orientation in hence x, since x is a linear combina-
matrix B′ to r′ that will have the same one step. (Note how many degrees of tion of the two. The y vector is also the
effect as a matrix B that’s applied to r freedom we’re talking about here: three perfect length, since the magnitude of
before A rotates it. Symbolically, we for the elements of the vector, plus one the cross product is equal to |r|sinφ (n
want to figure out B′ in B′Ar = ABr. for the angle, minus one for the vector’s is unit length), which conveniently
Thinking about it another way, how do unit-length constraint leaves us with —
we “rotate” the matrix B by A so it will surprise — three.)
work in the primed space? We begin by We can also directly construct a rota- F I G U R E 2 . The circle of rotation.
noting that r = 1r. I can therefore insert tion equation from the unit vector and
the identity matrix into the right-hand the angle. Let’s start with a unit vector y
side of the previous equation, giving us n, an angle θ around that vector, and
AB1r (inserting an identity matrix is a the arbitrary vector to rotate r. Figure 1
common linear algebra trick). The shows the situation, with r′ as the
equality ATA = 1 from Eq. 1 also gives us resultant vector. If we look down -n r'
B′Ar = ABATAr. Comparing the two onto the plane of rotation containing
terms gives the following equation: the tips of r and r′, we see the circle of
rotation in Figure 2. As we know from θ r x
B′ = ABA T (Eq. 2) o
trigonometry, if we consider the tip of
Eq. 2 defines what is called in linear r to be on the x-axis of this diagram,
algebra a “similarity transform.” It then the coordinates of the tip of r′,
shows how to rotate B to get a matrix measured in this planar coordinate sys-
in the primed space that operates on tem, will be (x = rcosθ, y = rsinθ), where
primed vectors in the same way B oper- r is the radius of the circle. This (x,y)
ates on vectors in the unprimed space. coordinate notation is just a shorthand

http://www.gdmag.com JUNE 1997 G A M E D E V E L O P E R


BEHIND THE SCREEN

equals r, the radius of the circle, as you assume r is rotating over time around a tion of the angular velocity at a given
can see in Figure 1. Putting it all fixed n, then we can again reduce the instant, but not before or after that
together, we get problem to the planar Figure 2, and use instant. The instantaneous axis of rota-

( )
similar arguments for the velocity of r tion can and will change under the
r ′ = nT rn + cos θ r − nT rn + sin θ (n × r ) application of forces and torques. This
as we did in my article on 2D angular
(Eq. 6) velocity. The first argument from the means we can use it to calculate veloci-
This is one form of a famous formula 2D article showed the magnitude of ties of points at the instant it’s valid,
on whose name no one seems to agree. the velocity vector as r θ̇, which we’ll but we can’t store it and use it later
I’ve heard it called The Rotation Formula, recognize as |r|sinφ θ̇ from Figure 1. without keeping it up-to-date via inte-
Rodriguez’s Formula, and a bunch of Next, we can see the velocity vector gration. More on that later.
other names. No matter; we’ll call it Eq. must point perpendicularly to r and to As a final derivation, we’ll use Eq. 7
6. Eq. 6 will rotate any r around n by θ. n. This is true because r is only rotating to calculate the derivative of the cur-
We’re not actually going to use Eq. 6 to about n, so the tip of r is always mov- rent orientation matrix using the angu-
rotate vectors, although it would do the ing normal to the plane containing r lar velocity vector. This is a bit tricky,
job just fine. Instead, we’re going to use and n as it rotates. So, what’s a vector so hold on tight. First, we know from
it to prove useful kinematics equations expression that yields a vector of the graphics that the columns of a rotation
for 3D orientation. We could also con- right magnitude pointing in the right matrix are unit vectors in the trans-
struct a rotation matrix from Eq. 6 by direction? How about this: form’s destination coordinate system.
“pulling out” the r vector from the Well, Eq. 7 shows the angular velocity
r˙ = θ˙ n × r = ω × r (Eq. 7)
18 right-hand side, but we’re running out vector “differentiating” a column vec-
of space, so I highly recommend If we define the angular velocity vec- tor, and there’s no reason we can’t use
exploring that yourself. (Hint: Try to tor ω as the current instantaneous axis the angular velocity to differentiate
figure out the 3×3 matrix associated of rotation times the rotation speed each column vector of the orientation
with each term, so that the matrix ( θ̇ n), then we get an expression that is matrix, resulting in the differentiated
times r would equal the terms in Eq. 6. very similar to the one for 2D, only matrix. The only problem is that the
You’ll need the “tilde operator,” which with a cross product replacing the per- cross product of a vector and a matrix
I’ll discuss later.) pendicular operator — I told you the isn’t usually defined. However, we can
two operators were related. Remember, represent a cross product as a matrix
like the 2D version, Eq. 7 is only valid times a column vector, like this:
Angular Velocity if r is a constant vector — it can rotate 0 −ω 3 ω 2 r1
n 2D, we used the time derivative of around, but it can’t change length andx = r − nT rn
I our orientation scalar as our angular
velocity scalar. The angular velocity
keep Eq. 7 valid.
Here’s a totally different way to
r˙ = ω × r = ω
˜ r = ω3
− ω 2
0
ω1
−ω1 r2
0 r3
scalar, when combined with the per- derive the same result: We can consider (Eq. 9)
pendicular operator, was also useful for Eq. 6 as a function that describes the The tilde operator, introduced in the
finding the velocity of any point in a path of the vector r′ as it rotates by θ third term, takes a vector and creates
rotating body. In 3D, our orientation is radians from its initial position r. If θ is the “skew-symmetric” matrix depicted
a 3×3 matrix. Is our 3D angular velocity a function of time, and n is a constant in the final term. If you write out the
required to be the time derivative of axis of rotation, we can differentiate matrix multiply by hand, you’ll see it’s
our orientation matrix? The answer is Eq. 6 with respect to time. equivalent to the cross product. We use
no, the angular velocity representation
doesn’t have to be the time derivative
( )
r˙ ′ = − sin θθ˙ r − n rn + cos θθ˙ (n × r )
T the tilde operator to differentiate each
column with a single matrix multiply.
of the orientation representation. It’s (Eq. 8)
A˙ = ω ˜A (Eq. 10)
only important that we’re able to cal- We consider r in Eq. 6 to be constant
culate the orientation matrix’s deriva- as well, since it’s just the initial posi- The right side of Eq. 10 will differen-
tive so we can integrate it — we don’t tion of the nonconstant vector r′. tiate each column of A, which differen-
have to use the derivative beyond that. Finally, evaluate Eq. 8 at some time t. tiates the whole matrix. We could have
We’ll see how to make the needed cal- We can always define θ(t) to be 0 in defined a vector cross a matrix as the
culation later. Figure 2 by choosing an appropriate column-wise cross product, or we could
It may seem strange that we can use frame of reference. Specifically, we have just looped through the columns
a fundamentally different representa- make the “x-axis” of the figure be the doing cross products individually. But
tion for our angular velocity than we plane containing r and n at any given then you would have missed out on
used for our orientation. Unfortun- instant. Within this frame of reference, the groovy new tilde operator in Eq. 9,
ately, we don’t have the space to go r′ = r, sin0 = 0, cos0 = 1, and we’re left so it was worth it. Plus, we’ll use this
into why this is possible, but it’s cov- with Eq. 7! Remember, just because operator again later.
ered in most of the references on my θ(t) = 0 in our frame of reference, it It’s important to stress a couple
web site. Let’s work through a few doesn’t mean θ̇ (t) = 0. things about Eq. 10. First, the left-
derivations to define and get comfort- This vector ω is the representation hand side is the instantaneous deriva-
able with the angular velocity. we’ll use for our angular velocity. The tive of A, meaning it’s only the deriva-
First, we’ll calculate the linear veloci- vector we’ve defined is “instantaneous” tive at the instant of time when ω is
ty of the vector r in Figure 1. If we in the sense that it’s a valid representa- valid. However, that’s all we need to

G A M E D E V E L O P E R JUNE 1997 http://www.gdmag.com


BEHIND THE SCREEN

F I G U R E 3 . A point on a rigid body. the body’s origin. There are the angular quantities of orientation,
two equivalent ways of dif- angular velocity, and angular accelera-
ferentiating the rotating r tion (I slyly stuck this one into Eq. 13
vector, though. First, we’ll as α, the derivative of ω). We’ve also
use Eq. 7 and differentiate shown how to use ω to differentiate
o the last term in Eq. 11 vectors and matrices, and we calculated
directly. the velocity and the acceleration of any
r = Ar point on a moving body.
b˙ = o˙ + ω × r (Eq. 12)
b The only things left to do before we
Next, for a change of pace, have a full 3D dynamic simulation
we’ll differentiate the middle algorithm are to develop the 3D
term in Eq. 11 explicitly, dynamic quantities and equations,
using the product rule for relate those dynamic quantities to the
derivatives. kinematic quantities, and show how to
integrate them all forward in time.
b˙ = o˙ + Ar
˙ + Ar˙

Since r is a constant vector


in the body, its time deriva- 3D Dynamics
numerically integrate A forward to the tive is 0. We can also substitute Eq. 10 ike 3D linear kinematics, 3D linear
20 next step, as we’ll see. Second, the axis
of the angular velocity vector and the
into this equation and we get L dynamics and 2D linear dynamics
are identical, with the exception that
b˙ = o˙ + ω
˜ Ar = o˙ + ω
˜ r = o˙ + ω × r
axis of the rotation matrix can be dif- the vectors now have a z element. In
ferent, and Eq. 10 still holds. In other In other words, both ways of finding the 2D articles, we derived equations
words, if we have our current orienta- b’s velocity are equivalent — score one for the force and momentum of a sin-
tion A, and our body has some angu- point for math. We differentiate one gle particle, then derived the position
lar velocity, embodied in ω, then Eq. more time to find b’s acceleration. (I’m vector to the center of mass. Since the
10 will calculate how the orientation only going to do it one way this time. derivations are identical in 3D, I’ll just
of A is changing at that instant under You should try the other yourself.) state the results without proof. (Note
the influence of the angular velocity. ˙˙ = o
˙˙ + ω˙ × r + ω × r˙ that I’m switching from the super-
b
This is how our body’s orientation scripted indices that I used in the 2D
changes in the simulator — we relate ˙˙ + α × r + ω × (ω + r )
˙˙ = o
b (Eq. 13) columns to subscripted indices here so
the forces and torques to changes in We should have expected the deriva- I don’t confuse “total” values with the
ω, and use ω with Eq. 10 to integrate tive of the angular velocity vector, the transpose operator. Sorry about that.)
angular acceleration vector α, to show
∑ p˙ = ∑ m v˙ = ∑ m a
our body’s orientation.
up, but what’s the third term doing FT = i i i i i = Ma CM
i i i
there? The math has magically pro-
(Eq. 14)
Kinematic Equations for a Point on a duced the centripetal acceleration of a
Moving Body rotating point! In other words, if you Eq. 14 says the total force FT equals
look at the direction in which the third the sum of all the momentum deriva-
et’s use all the kinematics that term is pointing, you’ll see it’s pointing tives, which is equivalent to the mass of
L we’ve developed so far to write the
equations for a point’s position and its
back towards the origin of the body.
This is the acceleration you feel if
the whole body M times the accelera-
tion of the center of mass aCM. If I know
derivatives. The position vector of the you’re stuck to the wall of one of those all the forces on the body, I take their
point b is given by the position vector spinning carnival rides. You actually vector sum and divide by the total mass
of the origin of the body o plus the feel it as a force pushing you into the to find the acceleration of the center of
vector from o to b in the body, which wall, but that’s only because the wall is mass. I then can integrate the accelera-
we’ll call r. Figure 3 shows this configu- accelerating towards the center to keep tion forward in time to find the new
ration. The vector r rotates with the from being flung across the fair- center of mass velocity and position.
body as shown in the figure. Since the grounds. (Mathematically, this is the The 3D angular dynamic quantities
body is rotating, r is the rotated world- restriction that r is constant in body- are, as you might expect, slightly differ-
space version of a vector we’ll call r space.) I just love dynamics. ent than the 2D angular dynamic quan-
that’s constant in the body space. Now tities. First, we’ll define the angular
we can write the position equation for momentum of point B about point A in
b in world space. Interlude three dimensions. In 2D, the angular
hat you just read was longer momentum was a scalar formed by a
b = o + Ar = o + r (Eq. 11)

If we differentiate Eq. 11, we’ll get


W than any of my other columns,
and we haven’t even covered 3D
perp-dot product. We visualized this
quantity capturing the amount of B’s
the velocity of b. The o vector is easy, dynamics yet. We have come a long linear momentum “rotating around” A.
since it’s just translating around, way, though. We’ve chosen representa- Well, in 3D we need an axis to rotate
keeping track of the origin — its tions for the position, linear velocity, around, so the angular momentum
derivative is just ȯ , or the velocity of and linear acceleration, and also for becomes a vector L. L is calculated with

http://www.gdmag.com JUNE 1997 G A M E D E V E L O P E R


BEHIND THE SCREEN

a cross product, which conveniently The next thing we need to do is devel- change sign. Finally, we use the all-pow-
creates a vector perpendicular to both op the “total” versions of these quanti- erful tilde operator from Eq. 9 to turn
the linear momentum of B, pB, and the ties. That is, what is the angular momen- the equation into a matrix multiply:
vector from A to B, rAB. In other words, tum for the entire body? As in 2D, the Both r cross products are replaced with
the cross product creates a vector that total angular momentum is just the sum r̃ , leaving ω on the right-hand side.
describes the plane of the momentum’s of all the angular momentums of all the
rotation around A. The magnitude of L points in the body measured from a L AT = ∑ −m r˜ r˜ ω = I ω
i
i Ai Ai A (Eq. 17)
is proportional to the sine of the angle point (usually the center of mass).
between the two vectors and measures The inertia finally rears its head in
the amount of momentum that’s per- L AT = ∑r Ai × mi v i = ∑r Ai × mi r˙ Ai 3D, though it’s now a matrix rather
than a scalar! Since ω is constant over
i i
pendicular to r.
L AB = rAB × p B I’ve taken the liberty of rewriting the whole body, as in 2D, we can pull
(Eq. 15)
the momentum of the point being it outside the summation. This leaves
As in two dimensions, the derivative measured as its mass times its velocity us with a matrix called the “inertia ten-
of the angular momentum is the — I even went a step farther in writing sor,” relating the angular velocity of a
torque, denoted by τ. A little bit of work it as the position vector’s time deriva- body to the angular momentum of the
will show the following identities hold: tive. This is the first step in linking body. The inertia tensor obviously is a
the angular dynamic quantities with lot more complicated than the single
L̇ AB = τ AB = rAB × FB (Eq. 16)
the angular kinematic quantities. The scalar moment of inertia from 2D. To
22 The derivative of the angular next step is to substitute Eq. 7 into the make matters worse, the inertia tensor
momentum is the torque, and it can be equation, leaving us with changes as the object rotates because it

∑ m r × (ω × r )
calculated from the cross product of depends on the world-space rs.
L AT = i Ai Ai
the vector from the point of measure- i
If we ignore the change in the inertia
= ∑ − m r × (r × ω )
ment to the point where the force is tensor for a moment, we can actually
i Ai Ai
being applied. The torque measures the i
begin to see how we might implement
amount of “rotating-around” force I flipped the order of the inner cross 3D angular dynamics. We can easily
experienced from a given point. product, which causes the result to find the total torque on the body —
measured about the center of mass — it changes as the object rotates in world to calculate the body-space inertia ten-
by forming the vector sum of all the space because it depends on the world- sor in the first place. For continuous
individual torques produced by force space r vectors. However, it’s possible to bodies, the summation in Eq. 19 turns
applications via Eq. 16. If we integrate do the derivations in body space. You into an integral over the body’s vol-
this torque, we’ll get the total angular end up with an inertia tensor based on ume, and for arbitrarily oddly shaped
momentum about the center of mass. the fixed body-space r vectors. bodies, this integral can get arbitrarily

∑ −m r˜ r˜
Then, assuming we know the world- complicated. It’s fairly easy to analyti-
IA = i Ai Ai (Eq. 19)
space inertia tensor, we can solve the i
cally solve the integral for “easy geom-
inverse of Eq. 17 to find the current The body-space inertia tensor does- etry,” such as boxes, ellipsoids, cylin-
angular velocity for the body. n’t change (since the body is rigid), so ders, and the like, and there are tables
−1 we can compute it once at the begin- for other objects. Also, a paper refer-
ω = ICM LCM (Eq. 18) ning of our simulation and store it. We enced on my web site shows how to
Once we’ve got the angular velocity, use the similarity transform trick we calculate the inertia tensor for an arbi-
we’re home free, kinematically speak- derived oh-so-long-ago in Eq. 2 to gen- trary polyhedron, but the algorithm is
ing, since we already know how to erate the world-space inertia tensor for way too complicated to go into here. I
integrate the orientation using the the current orientation A. More inter- should also note that if you can’t calcu-
angular velocity to get the orientation’s esting, perhaps, is the fact that since late the exact inertia tensor, you can
derivative. The only thing standing in the body-space inertia tensor is con- still use the inertia tensor for a tight-fit-
our way is the inertia tensor. stant, we can precalculate its inverse ting approximation volume and the
before we start. Then we similarity- dynamics will be “mostly right.” 23
transform the inverse inertia tensor,
The Inertia Tensor and avoid the inversion during the
hen we did the derivations lead- simulation when evaluating Eq. 18 to 3D Dynamics Algorithm
W ing to the definition of the iner-
tia tensor in Eq. 17, we were using
find the angular velocity vector.
−1 −1 W e now have the quantities and
equations we need to imple-
I A = AI AA
T
(Eq. 20)
world-space vectors and matrices. This is ment 3D rigid body dynamics, and I’ve
why the inertia tensor is giving us fits — The only piece still missing is a way outlined the simulation algorithm in
BEHIND THE SCREEN

L I S T I N G 1 . The 3D Dynamics Algorithm. velocity from it conveniently. The


angular momentum is also conve-
niently integrated from the torque,
Initialization: while the integration from the angu-
−1
Determine body constants: ICM ,M lar acceleration to the angular veloci-
ty is more complicated. (Try differen-
0
Determine initial conditions: rCM , v 0CM , A 0 , L0CM tiating Eq. 17 to find the angular
acceleration equation. Keep in mind
Compute initial auxiliary quantities: the world-space inertia tensor’s deriv-
−1
−1 T ative is nonzero.) Finally, the angular
I0CM = A 0 ICM A0
−1
momentum vector comes in handy
ω 0 = I0CM L0CM when you want to compute the kinet-
ic energy of the body, which is useful
Simulation: for debugging.
Compute individual forces and application points: Fi , ri Once we’re initialized, we can
begin the simulation. The first step is
Compute total forces and torques: FTn = ∑F ,τ = ∑r × F
i
i
n
T
i
i i to calculate what the external forces
on our body are (from explosions,
Integrate quantities: punches, rockets, or whatever), and
24 n +1
rCM = rCM
n
+ hv nCM where on the body those forces are
applied. Once we have this informa-
+1 FTn
v nCM = v nCM + h tion, we can calculate the total force
M and torque using Eqs. 14 and 16.
A n +1 = A n + hω̃ n A n Now we’re ready to integrate over
+1
LnCM = LnCM + hτ nT the timestep h. When looking at
these equations, it’s important to
Reorthogonalize A n+1 note the right-hand sides of all the
integration steps use the quantities
Compute auxiliary quantities: from step n, and the left-hand sides
+1−1 T all specify the next step, n + 1. The
InCM = A n +1ICM
−1
A n +1
−1
new center of mass position is inte-
ω n +1 = In +1 LnCM
+1
grated from the current position and
velocity. The new velocity is inte-
grated from the current velocity and
acceleration (using the definition of
Listing 1. This listing focuses on the angular momentum, let’s look at the linear acceleration as force over
parts of the overall simulation loop next line in the initialization, Compute mass, à la Eq. 14). Next, we integrate
that changed during the move to three initial auxiliary quantities. The auxil- the orientation. The orientation’s
dimensions, so it doesn’t cover how iary quantities are those we derive derivative is calculated using the cur-
collision detection and resolution fit from the other quantities — we don’t rent angular velocity as we saw in
into the picture. See the algorithm in integrate them directly. We first com- Eq. 10. In the last integration step,
the February/March 1997 “Behind the pute the initial world-space inverse we integrate the new angular
Screen” for the full loop (or look in the inertia tensor by similarity-trans- momentum vector from the torque.
sample code). Let’s step through forming the body-space tensor using Finally, we need to enforce the
Listing 1. the initial orientation matrix (Eq. orthogonality constraints on our ori-
At initialization time, we need to 20). Then we use this world-space entation. If our integration was
determine the mass constants for the inverse inertia tensor and the initial exact, we wouldn’t have to do this
body. These can be calculated on the angular momentum to compute the reorthogonalization, but errors will
fly from the geometry of the object, initial angular velocity (Eq. 18). So, creep into the orientation over time.
or loaded in from a file, or even part of the reason we store the angu- There are many ways to reorthogo-
typed in by the user. We also need lar momentum as a primary quantity is nalize a rotation matrix, but they all
the initial conditions for the object. because we can compute the angular amount to making sure the rows and
I’ve indicated the “step number”
with a superscript, so the initial con- F I G U R E 4 . The 3D collison impulse magnitude.
ditions are all step 0. For the linear
quantities, we store the position vec-
tor of the center of mass, and its −(1 + e )v 1AB ⋅ n
j=
 1 1 
[( ]
velocity. For the angular quantities,
we store the orientation matrix and n ⋅ n
 A
M
+
M B
) ( )
 + I A (rAP × n) × rAP + I B (rBP × n) × rBP ⋅ n
−1 −1

the angular momentum vector.


Before I explain why we store the

G A M E D E V E L O P E R JUNE 1997 http://www.gdmag.com


BEHIND THE SCREEN

columns are perpendicular and unit the bare minimum needed to get
length. See the sample code for one
Postlude our integrator running, but our
technique. hat’s it. With the information in Euler integrator probably won’t do
Now that we’ve got the primary
quantities for step n + 1, we can calcu-
T this series, you should be able to
add much more believable physics to
for a production-quality simulator.
Numerical analysis is the study of
late the auxiliary quantities from them. your games and give the user a more how to implement all of these
This gives us the up-to-date quantities immersive experience. However, you’re equations on the computer.
needed for the next integration step. far from done. Here are just some of As you can see, there is a ton of
And away we go. the features we haven’t covered: physics out there to learn. We’re in the
• Contact. Our objects currently dark ages of physical simulation in
can’t rest on the ground, which is games at this point, and the material in
3D Collision Response pretty vital for a real game engine. these articles is just enough to get you
e’re almost out of space, so I • Multiple simultaneous collision started learning. So go read the refer-
W don’t have room to derive the
3D collision response equation.
points. If you drop a box flat onto
the ground, all four corners should
ences on my web site (http://ourworld.
compuserve.com/homepages/checker),
However, the 3D derivation is very hit at the same time. and get to work! ■
similar to the 2D derivation in the • Modeling friction during contact Chris Hecker’s company, definition six
previous physics column, so you and collision. incorporated, is putting its money where
should be able to work it out yourself • Collision detection. his mouth is by basing its first game on
26 using the formulas in this article, • Joints for articulated figures. some pretty stoked physics. If the e-mail
especially Eq. 12. So that you can • Control for physically based crea- he’s received during this physics series is
check your work, the final 3D equa- tures. Animation loops and simu- any indication, lots of other companies are
tion for the impulse magnitude j is in lation don’t necessarily get along trying to do the same thing, so the next
Figure 4. Just remember, there’s no very well, so how to control crea- generation of games should finally start
such thing as 1⁄I when I is a matrix, so tures in a physically simulated pushing the physics envelope in some
you have to use I-1 and keep track of environment is a huge issue. interesting ways. Let him know how you’re
the order of multiplications. • Numerical analysis. We covered using physics at checker@bix.com.

You might also like