Real-Time 3D
Volume Number: 8
Issue Number: 1
Column Tag: C Workshop
Real-Time 3D Animation
Using simple vector calculations to draw objects that move and spin in a
3-D environment
By Lincoln Lydick, Littleton, Colorado
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
Ever felt that real-time 3d animation was meant only for the “computer gods” to
create? That mere mortal programmers are destined only to marvel at the feats of
greatness? That finding example code on how to accomplish some of these tricks is
impossible? Well it turns out not to be difficult at all. This example uses simple vector
calculations to draw 6 objects which move and spin in a 3 dimensional environment.
The viewer is also free to move, look at, and even follow any of the objects. To optimize
the calculations, we’ll do all of this without SANE. This may sound difficult, but stick
with me - it’s very simple
The Plan
In order to draw cubes and pyramids (henceforth called objects), we’ll use a
single pipeline that translates, rotates and projects each in perspective. But first we
need to develop a plan. Our plan will be a Cartesian coordinate system where all objects
(including the viewer) will occupy an x, y, & z position. The objects themselves will be
further defined by vertices and each vertex is also defined by an x, y, & z coordinate.
For instance, cubes will be defined by eight vertices and pyramids by five - with lines
drawn between.
Figure 1: Vertex assignment
Changing any value of a vertex represents movement within space. Therefore we
can move the viewer or an object by simply changing an x, y, or z. If either the viewer
or an object is required to move in the direction of some angle, then we provide a
variable called velocity and apply these simple vector equations:
[EQ.1] Xnew = Xold + sin(angle) * velocity
[EQ.2] Ynew = Yold + cos(angle) * velocity
Translation
Objects will first be translated (moved) relative to the viewer’s position. This
is required because rotation calculations (coming up next) require points to be rotated
around a principal axis. Therefore, since the viewer may not be at the origin (Figure
2), we must move the object the same amount we would need to move the viewer to be at
the origin (Figure 3). Note: I adopt the convention where the x and y axis are parallel
to the plane, and the z axis depicts altitude.
So to perform this “relative” translation, we just subtract the components of
the two points:
[EQ.3] Xnew = Xold - ViewerX
[EQ.4] Ynew = Yold - ViewerY
[EQ.5] Znew = ViewerZ - Zold
Now this is all well and good, but what if the viewer is looking at the object?
Wouldn’t the object then be directly in front of the viewer - and subsequently drawn at
the center of the window? Yes, and this leads us to
Figure 2: Before & Figure 3: After Translation
Rotation
Since we’re providing the viewer with the ability to “look around”, we need to
rotate each object by the viewer’s angle. This rotation will occur around the Z axis and
is accomplished by applying these calculations to each vertex:
[EQ.6] Xnew = Xold * cos(angle) - Yold * sin(angle)
[EQ.7] Ynew = Xold * sin(angle) + Yold * cos(angle)
Figure 4: Before & Figure 5: After Rotation
Figure 4 shows the viewer looking at the object by some angle. Rotating the
object by that angle indeed moves it centered on the y axis (Figure 5) and will be drawn
centered in the window. Of course if the viewer and the object are at different heights,
(it could be above or below us), we might not see it at all - but we’ll deal with that
later.
Now if an object is allowed to rotate itself (i.e., spin), then we use the same
calculations, although the angle will be unique to the object and not the viewers. Note,
this rotation must occur with the object at the origin, and before it is translated
relative to the viewer or rotated by the viewer’s angle. Therefore, we’ll first build the
object around the origin, spin it, move it to its correct location, then translate and
rotate as shown earlier. This may sound costly (and it is a little) but we’ll compute the