Game Engine Architecture

(Ben Green) #1

138 4. 3D Math for Games


Figure 4.1. A point rep-
resented in Cartesian
coordinates.


Py

z x


y

Pz Px

P

Ph P

h

r

Pr

θ


Figure 4.2. A
point represent-
ed in cylindrical
coordinates.


between 2D and 3D does not hold all the time. Some operations, like the cross
product, are only defi ned in 3D, and some problems only make sense when all
three dimensions are considered. Nonetheless, it almost never hurts to start by
thinking about a simplifi ed two-dimensional version of the problem at hand.
Once you understand the solution in 2D, you can think about how the prob-
lem extends into three dimensions. In some cases, you’ll happily discover that
your 2D result works in 3D as well. In others, you’ll be able to fi nd a coor-
dinate system in which the problem really is two-dimensional. In this book,
we’ll employ two-dimensional diagrams wherever the distinction between 2D
and 3D is not relevant.

4.2 Points and Vectors


The majority of modern 3D games are made up of three-dimensional objects
in a virtual world. A game engine needs to keep track of the positions, orien-
tations, and scales of all these objects, animate them in the game world, and
transform them into screen space so they can be rendered on screen. In games,
3D objects are almost always made up of triangles, the vertices of which are
represented by points. So before we learn how to represent whole objects in
a game engine, let’s fi rst take a look the point and its closely related cousin,
the vector.

4.2.1. Points and Cartesian Coordinates
Technically speaking, a point is a location in n-dimensional space. (In games,
n is usually equal to 2 or 3.) The Cartesian coordinate system is by far the
most common coordinate system employed by game programmers. It uses
two or three mutually perpendicular axes to specify a position in 2D or 3D
space. So a point P is represented by a pair or triple of real numbers, (Px , Py)
or (Px , Py , Pz).
Of course, the Cartesian coordinate system is not our only choice. Some
other common systems include:
z Cylindrical coordinates. This system employs a vertical “height” axis h, a
radial axis r emanating out from the vertical, and a yaw angle theta (θ).
In cylindrical coordinates, a point P is represented by the triple of num-
bers (Ph , Pr , Pθ). This is illustrated in Figure 4.2.
z Spherical coordinates. This system employs a pitch angle phi (φ), a yaw
angle theta (θ), and a radial measurement r. Points are therefore rep-
resented by the triple of numbers (Pr , Pφ , Pθ). This is illustrated in Fig-
ure 4.3.
Free download pdf