Article Preview

Buy Now

FEATURE

## Basic 3D Math

**Issue:** 1.2 (October/November 2002)**Author:** Joe Strout**Author Bio:** Joe is a software engineer and self-appointed games evangelist at REAL Software.
**Article Description:** No description available.**Article Length (in bytes):** 13,211**Starting Page Number:** 24**Article Number:** 1111**Related Link(s):** None

**Excerpt of article text...**

Whether writing a video game, building a physics simulation, or simply crafting an animated credits window, you are likely to need three-dimensional (3D) math. To some, 3D math seems arcane and complex; to others, the math is simple but the REALbasic tools may be unfamiliar. In this article, we'll cover the basic concepts of 3D math and show how it's done in RB.

VectorsLet's start with the obvious: to identify a point in three-dimensional space requires three numbers. You can think of these as "East," "North," and "Altitude." For example, East = 5 would indicate a point 5 units (where a "unit" might be a meter, a mile, etc.) east of some reference point we'll call the "origin." East = -12 would indicate a point 12 units west of the origin. Of course we haven't fully identified the point until we also specify North and Altitude. By mathematical convention, the three values are actually called X (East), Y (Altitude), and Z (North).

Carrying around three different numbers for each point we have in mind is cumbersome, so mathematicians came up with their equivalent of object-oriented programming: a new data type that wraps up these three related numbers. This is called a vector, and in the current context at least, a vector is nothing more or less than a class containing three numbers (X, Y, and Z).

If we consider those numbers to be relative to the origin, then they specify a location in 3D space. However, we can also think of them as offsets from some other point. In that case, the vector represents a change in position. For example, if I say "vector (3,0,0)" I might mean the point at X=3, Y=0, and Z=0; or I might mean to move some other point +3 units on the X axis, and not at all on Y and Z. I might even mean that one point is 3 units to the right (i.e., in the +X direction) of some other point (see Figure 1.) It all depends on context. The key point is this: a vector can represent an absolute position, or a relative position.

The great thing about wrapping up the three numbers into one object is that we can simplify a lot of notation, so we have less work to do (and less opportunity for error). For example, if we want to add an offset vector B to some position A, and store the result in another position vector C, we could do this by working on each part of the vector individually:

...End of Excerpt. Please purchase the magazine to read the full article.