Part One: The Basics of Rotations in Three Dimensions


By confuted
The purpose of this tutorial series is to explain the math involved behind rotating points in three dimensions. It will start out by describing how to use a rotation matrix to rotate a point over the Z-axis, simply because this is the easiest rotation to visualize and implement. It's just like a clock hand going around. Then, the tutorials will move on to give you the matrices for rotation over the x and y axes, tell you how to use them, and then give you a matrix which will allow rotations around an arbitrary axis. Translation matrices will also be covered. There will be talk of cameras and simply rotating objects, and then will move on to quaternions, which are the solution to the problem of gimbal lock, which is encountered when using Euler angles. I may mention some other things, or I may not. The current plan is to type these until I get sick of it.



This first tutorial will be short - mainly because there are so many sources available online and in print which teach about vectors, three-dimensional axes, matrices, vertices, trig and the like. I'll be glad to let you consult those for the basic information that I do not provide here. Please understand the concept of vectors and understand that a triangle has three vertices and any more complex polygon can be made from several triangles before proceeding with these tutorials. An understanding of trigonometry will also be essential for much of the discussion.

Rotation About The Z Axis:
Rotation about the Z-axis really isn't difficult. If you are strong with trig, you can probably work out the equations necessary to rotate a point around the Z-axis. After all, it's taught in math class when you learn parametric equations. Suppose you're rotating the point (1,0) 90 degrees over the Z-axis. When you're rotating over the Z-axis, the Z component won't change, so you can obviously ignore it in your calculations for this axis. It will be a good way to ease yourself in. Anyway, you're rotating the point (1,0) about the Z-axis 90 degrees. A bit of visualization will tell you that you're going to end up with the point (0,1), but you can't just do that in a program, and you can't even do it in your head for stranger angles, like 37 degrees. Anyway, by looking at a triangle in a unit circle, we can determine the following formulas for the rotation.

Xrotated = hypotenuse * cos(theta)

Yrotated = hypotenuse * sin(theta)

Where Xrotated is the X coordinate of the point after the rotation, Yrotated is the Y coordinate of the point after the rotation, theta is the number of degrees/radians we are rotating the point about the Z axis, and hypotenuse is the distance between the point and the origin. The hypotenuse can be easily calculated using the Pythagorean theorem, also known as the Distance Formula. Extending this theorem to three dimensions isn't difficult, but I'll get to that later. Here's how to calculate the hypotenuse in two dimensions:

hypotenuse= sqrt( x2 + y2 )

So, that's pretty easy, and you could use it to make a program which displayed an analog clock, for example. You can also use that to draw a circle. All you have to do for the circle is rotate the point in one-degree increments (other numbers of degrees will work) and display a point at every location, and draw lines between points. I'll leave that to the reader, because it's boring. You want 3d math! You want matrices! You want to make Quake! Well, that's coming. Hang in there. More will be revealed in Part Two.

Oh, and in case you want my contact information, here it is:
I'm Matthew Hansen, known on Cprogramming.com as Confuted. I'm currently residing in Traverse City, Michigan.




Next: 3D Rotation Matrices
Back to Graphics tutorials index