3D Space
The
basis to any three-dimensional world is the space it takes place in.
Look around you. The keyboard and monitor on your desk, the chair on the
floor, all of these items exist in a 3D space. If you had to describe
the location of one of these objects to someone over the phone, how
would you do it? Would you describe your desk as located in front of you
or would you say it was near a certain wall? If the person on the phone
knew absolutely nothing about the room you were in, from that
description, would they understand? Probably not, they’re missing a
point of reference.
A point of reference
is a location that both you and the other person understand. For
instance, if the point of reference was a doorway, you could then
explain that the desk was located about ten feet from the door on the
left hand side. When you’re building a 3D world, a point of reference is
crucial.
You need to be able to place
objects in relation to a point of reference that both you and the
computer understand. When working with 3D graphics, this point of
reference is the coordinate system. A coordinate system
is a series of imaginary lines that run through space and are used to
describe locations within it. The center of this coordinate system is
called the origin; this is the core of your point of reference. Any
location within this space can be described precisely in relation to the
origin.
For example, you can
describe the location of an object by saying it is four units up from
the origin and two units to the left. By using the origin as the point
of reference, any point within the defined space can be described.
If you remember from working
with sprites, any point on the screen can be explained using an X and Y
coordinate. The X and Y coordinates determine the sprite’s position in a
coordinate system consisting of two perpendicular axes, a horizontal
and a vertical. Figure 1 shows an example of a 2D coordinate system.
When working with
three dimensions, a third axis will be needed, called the Z axis. The Z
axis extends away from the viewer, giving the coordinate system a way to
describe depth. So now you have three dimensions, width, height, and
depth as well as three axes. Figure 2 shows a 3D coordinate system.
When dealing with 3D
coordinate systems, you have to be aware that they come in two flavors,
left-handed and right-handed. The handedness of the system determines
the direction the axes face in relation to the viewer.
Left-Handed Coordinate Systems
A
left-handed coordinate system extends the positive X axis to the right
and the positive Y axis upward. The major difference is the Z axis. The Z
axis in a left-handed system is positive in the direction away from the
viewer, with the negative portion extending toward them. Figure 3 shows how a left-handed coordinate system is set up.
Right-Handed Coordinate Systems
The
right-handed coordinate system extends the X and Y axes in the same
direction as the left-handed system, but it reverses the Z axis. The
positive Z values extend toward the viewer, whereas the negative values
continue away. Figure 4 shows a right-handed system.
Points
Now
that you’re familiar with the coordinate system, let’s go over how to
place objects within it. Any position within a coordinate system can be
represented using a point. A point
is simply an infinitely small location in space. When locating a point
in space, it is described using a single value for each axis. This value
is the offset from the origin along each respective axis.
For example, a point
located in 2D space would need two values, an X and a Y value, to
describe its location, such as <1, 3>. Figure 5 shows how this point would look.
For each axis added, it
takes one additional value to represent the point. In 3D space, three
values, X, Y, and Z, are needed to describe a point, such as <1, 2,
4>.
Points can be used in many
ways in the creation of a game, from player position to the location of a
planet. Even though each point is tiny, that one point can be used to
represent the location of any object in your game.
Finding the Distance between Two Points
Occasionally you will need
to determine the distance between two points. These points can be either
the origin and a fixed location or two completely arbitrary points.
For
example, imagine you’re creating a real-time strategy game. Each of the
monsters of the opposing army has the opportunity to move towards a
common goal. During the turn of the AI it can choose to move one of
these monsters toward the goal, but which one? This is where the ability
to figure out distance comes in handy. By calculating the relative
distance between each monster and the common goal, the AI can choose
which one of the creatures is more advantageous to move.
Whether you are determining the distance within a 2D or 3D space, the calculation is essentially the same.
X Distance = x2 - x1
Y Distance = y2 - y1
Final Distance = Square root (X Distance * X Distance + Y Distance * Y Distance)
First,
calculate the distance between the two X values. You can do this by
subtracting the X value from point 1 from the X value of point 2.
Next, figure out the distance for the Y value. The Y value is calculated the same way as the X value.
Add the squares of the X and Y values.
Take the square root of the resulting value.
The final result will be a
single value that represents the distance between the two points. When
determining the distance between two points in 3D space, make sure to
take the Z value into account as well.
X Distance = x2 - x1
Y Distance = y2 - y1
Z Distance = z2 - z1
Final Distance = Square root( (X Distance * X Distance) + (Y Distance * Y Distance)
+ (Z Distance * Z Distance) )