The Math Behind the Magic

Making It All Move

Making It All Move

We all know what a polygon is, but now how do we get them to move? Well, there are two main functions that you must perform. The first of these is Translation. This is simply moving (sliding) the polygon from one location to another. The next form of movement is rotation. I'm sure you all understand these, but how we deal with these is a bit trickier. Through the use of 3-D Coordinates we can create almost any object.

Just so we all understand each other, 3-D is defined in of course three directions, we'll call them (X,Y,Z). X will be the horizontal plane of your screen, Z will stretch vertically, and Y will extend out of and into your screen. However, this is not the only definition used. The next system is called Sphereical Coordinates it is defined by angles and distance, but for that we'll have to wait for future installments of The Math Behind the Magic. Although these are not the only definitions that are used, these are by FAR the most widely used, hence the only ones we are concerned with. In order to deal with these vectors we need a simple system. To accomplish this task we turn to matrix math. Lets say I can define Xt and Yt with the equations: Xt = aX + bY, & Yt = cX + dY (Where a,b,c,d are coeffiencents)

The matrix notation for this system of equations would be:

(Xt,Yt) = (X,Y)	[ a c]
                [ b d]

And we solve for this with these steps

Xt = (X,Y) [ a .] = aX + bY
           [ b .]

We move across the coordinates left to right and multiply them by the sections in the matrix, top to bottom coeffients.

Yt = (X,Y) [ .c ] = cX + cY
           [ .d ]

For Y, the second number, we use the second column of the matrix.

The matrix format is very important to polygon games, because it is necessary to correctly scale and rotate our vectors. For example, a point (2,2,2) in an object centered at (1,1,1). If we were to scale the X direction by 3,(the X length to the center is 3 times what it was) the point we want would be (4,2,2). Our new X = 3*(OldX-CenterX). Without this system our point would have turned out to be (6,2,2), obviously not the one we wanted. "Okay, I understand," you say, so how can we use this. First we'll cover rotations and transformations. Translation, moving from point A to point B.

We will start with translation from the origin. Most objects are not at (0,0,0,1), so we'll call their center (Tx,Ty,Tz,1).

    [  1    0    0    0  ] = T1
    [  0    1    0    0  ]

This physically moves the object, so it is centered at the origin for our calcuations,

    [  0    0    1    0  ]

eliminating the need for a -Tx for each X, the matrix will factor it

    [  0    0    1    0  ]

in when we multiply it by the others.

    [ -Tx  -Ty  -Tz   1  ]

Now that our object slides, we must have the ability for it to turn or "rotate." Thus, we look at XY Clockwise Rotation.

This will be our first rotation, about the Z-Axis

    [ sinØ  cosØ  0    0  ] = T2
    [-cosØ  sinØ  0    0  ]
    [  0     0    1    0  ]
    [  0     0    0    1  ]

Now for the Left Hand Correction. This will flip the X coordinates. Think about when you look into the mirror, your left hand looks like your right. These rotations do the same thing, so by flipping the X, it will make your X move right when you increase it's value.

    [ -1    0    0    0  ] = T4
    [  0    1    0    0  ]
    [  0    0    1    0  ]
    [  0    0    0    1  ]

Well, that was some nifty math, but where does it lead us?

The Final Viewing Matrix

This is the net transformation matrix for our viewing perspective

V = T1*T2*T3*T4

[ -sinØ  -(cosØ)(cosí)  -(cosØ)(siní)  0   ]
[  cosØ  -(sinØ)(cosí)  -(sinØ)(siní)  0   ]
[   0         siní          -cosí      0   ]
[   0          0              p        1   ]

Lets say our original (X,Y,Z,1) were just that, and the point after the rotation is (Xv,Yv,Zv,1)

(Xv,Yv,Zv,1) = (X,Y,Z,1) * V
Xv = -XsinØ + YcosØ
Yv = -X(cosØ)(cosí) - Y(sinØ)(cosí) + Zsiní
Zv = -X(cosØ)(siní) - Y(sinØ)(siní) - Zcosí + p

Have you had enough?

If your answer is no there are many sources that you can get more information. Most of my information came from the Internet, in particular from the works of Lithium/VLA (A member of a leading PC demo programming group). You can also check your public library for mathematics books, and your school professors for more help. I hope you learned something from this, and if you'd like to see more articles like this feel free to write Game Zero, you're detonation point for gaming information.

-Ferrari Man

[See related article "The Great Polygon Mystery" for more information.]

Originally appeared Vol 2, Iss 5 (11-12/94)

[ New Contents ]
[ Classic Contents - Articles - Reviews - Comics - Codes ]