• # Three Dimensional Rotations

Math and Physics

One of the most difficult programming difficulties you will face is that of representing a 3D world on that 2D screen in front of you. It requires some linear alegbra that may be difficult for some, so I will spend some bytes explaining the mathematics computations of using matricies in addition to the equations to get you going. This document is intended to be an aid to anyone programming in any language, as a result it will use mathmatic notation. If you are worthy of using these routines, you ought to be able to get them into your favorite language. All I ask is that you pay a little tribute to your programming buddies in VLA. If you aren't a math person, skip to the end and get the final equations. Just be forewarned, implementing these equations into a coherent 3D world is hard enough when you understand the mathematics behind them... REAL PROGRAMMERS AREN'T AFRAID OF MATH

# 3D Coordinates

Just so we all understand each other, 3D 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. Got it? Hope so, because it gets a bit tricky now. The next system is called Spherical Coordinates it is defined by angles and distance in ([font=Times New Roman]q,f,r[/font]) These Greek letters are Theta, Phi, and Rho. To relate the two systems you can use these equations. If these don't seem right, do a couple of example problems for yourself, it should make since after a bit of trig.

# Matrix Notation

Let's say I can define Xt and Yt with the equations: The matrix notation for this system of equations would be: We can also multiply matrices in this fashion This product is dependent on position, so that means that T1*T2 *DOES NOT* equal T2*T1 In English, the process above went like this, we move left to right in the first matrix, T1, and top to bottom in the second, T2. AE + CF is our first position. The numbers in the first row are multiplied by the numbers in the first column. 1st * 1st + 2nd * 2nd is our first value for the new matrix. Then you repeat the process for the next column of the second matrix. After that, you move down to the next row of the first matrix, and multiply it by the 1st column of the second matrix. You then do the same for the next column of the second matrix. This process is repeated until you've done all of the rows and columns. If this is your introduction to matrices, don't feel bad if you're a bit confused. They are a different mode of thinking about equations. The operations above give the same results as if you were to do the long hand algebra to solve them. It may seem a bit more difficult for these examples, but when you get to systems of equations with many variables, this way is MUCH faster to compute. Trust me, especially when you make your program do it. So, now you have the basic math.... One important point for these matrices below. I will use a homogeneous coordinate system, (X/r, Y/r, Z/r, r) Now I'll use r=1, so nothing will really be different in my calculations, but you need to understand the purpose. This form is very convenient for the translations and rotation equations we will need to do because it allows for scaling of our points with respect to a center point. Consider 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 the added factor of the homogeneous system, calculations assume all objects are centered at the origin, so our point would have turned out to be (6,2,2), NOT the one we wanted. So that's why we are going to do it that way.

# Translation

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).

# XY Clockwise Rotation

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

# YZ Counter-Clockwise Rotation

Now we rotate about the X axis Notice that with two rotations that we can get any position in 3D space.

# 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.

# The Final Viewing Matrix

This is the net transformation matrix for our viewing perspective The math for this one is really messy, and I would need to go over even more matrix stuff to get it reduced, so I will ask you to trust my calculations Let's say our original (X,Y,Z,1) were just that, and the point after the rotation is (Xv,Yv,Zv,1) Some people have had trouble concepts of this implimentation, so I have another way of setting up the equations. This works off of the straight X,Y, and Z coordinates too, but uses another angle. We will define the following variables

# Rotation about the X Axis

For simplification, lets call sin(Yan) = s1, cos(Xan) = c3,

# Final Rotation Matrix

Where Xv,Yv, and Zv are the final rotated points and the little x,y,z are the original points.

# Tips To Speed-Up Your Routines

Pre-Calculate as many values as possible The main limitation you will have is the speed of your math, using precalculated values like Normals, Sin/Cos charts, and distance from the origin are all good candidates. If you can get away with using a math-coprocessor, well... This will greatly increase the speed of your routine. Unfortunately, not everyone has one. Only figure values once If you multiply (Sinq)(Cosq) and will use that same value later, by all means, keep it and use it then instead of doing the multiplication again. Another thing to keep in mind The order of rotations *DOES* make a difference. Try it out and you'll understand. Also, when you start to use these routines, you'll find yourself making arrays of points and plane structures. Counterockwise points Be sure to list your points for the planes in counter-clockwise order. If you don't, not all of your planes will display correctly when you start hiding planes. And as always, be clever Just watch out, because when you have clever ideas you can lose a foot. My brother once had a clever idea to cut his toe nails with an axe and he lost his foot.

Report Article

## User Feedback

There are no comments to display.

## Create an account

Register a new account

• 0
• 23
• 0
• 1
• 0

• 9
• 9
• 11
• 11
• 23
×