Professional Documents
Culture Documents
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.
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
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
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˙
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
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.