|
Please download and install the Symbol font before reading this because
I use many Greek letters in this tutorial. You can get it here
(68k).
Ok...it is time to clear some confusion that you may have
about 3-D. It is not hard. And no, I am not taking on an arrogant
connotation here. I am saying that 3-D is not hard (or should not be) for
pretty much everyone willing to learn. I have been seeing many "3-D
Engines" around lately and it has been a huge source for boasting so it is
now time to let the cat out of the bag (pardon the cliché). Of course like
every tutorial out there I do have some prerequisites before you start
reading this. However I only require some basic knowledge of Algebra,
Geometry, and Trigonometry. And do not worry if you are a little shady in
those areas because I will still cover it...just not in full
detail.
So what do you need to know about before we get into the
good stuff? Here is a list:
1.) "Matrix Math." Only some basics
though. Mainly multiplication.
2.) The Sum and Difference
Trigonometric Identities: Difference Identity for Cosine
cos (a - b) = cos a cos b + sin a sin b
Sum Identity for Cosine
cos (a + b) = cos a cos b - sin a sin b
Difference Identity for Sine
sin (a - b) = sin a cos b - cos a sin b
Sum Identity for Sine
sin (a + b) = sin a cos b + cos a sin b
I will go through the derivation of those identities in this tutorial
too.
3.) The Law of Cosines. It is a simple law in Trigonometry
that will even be useful outside of this tutorial. I will be mainly using
it to prove the Sum and Difference Identities but it is going to be useful
in other areas too. I will also go over the derivation of this
Law.
4.) The Pythagorean Theorem slash Distance Formula. I am
almost positive everybody is aware of these and how to use them but I am
going to cover it anyway.
5.) Some basic knowledge of the Unit
Circle as well as the sine and cosine Trigonometric functions. If you have
not already read my "Introduction
to Trigonometry" then do it. I kind of wished I had spent more time on
it and made it better but hopefully I can get my point across with it. I
also cover the unit circle breifly in this tutorial.
And I think
that is about it. Before I get started let me say a few things. I am
neither a great programmer or mathematician. If I am able to understand
this you can be confident that you will too...provided you read up on some
of the things I have suggested. But because I am not a great mathematician
I am prone to mistakes...a lot of them. Contact me if you find any errors
in this tutorial or if something just does not makes sense. This is a
huge tutorial packed with tons of information. It is going to be
very hard for me to fully proofread it.
Also just in case you do
not know...maybe I should tell you what 3-D rotations are. When you have a
point on a plane you can rotate it by simply multiplying its "position
matrix" (this is not official terminology I am using...just something I
made up) by a rotation matrix (also known as a transformation matrix).
There are individual matrices for rotation around the x, y, and z axes.
The main reason I am even doing any of this is so that at the end we can
end up knowing how to derive these rotation matrices. It is also important
to know what each rotation looks like. A x-rotation would literally be
around the x-axis. Oddly enough an x-transformation does not affect the
x-position of the point in any way...only the y and z. And hopefully you
can extrapolate on what I just said for the rest of the axes and their
rotations.
Yes it is now that time!
// Start Boring
Stuff Boring stuff first. As many of you probably know a 2-D Cartesian
graph has a x- and a y-axis like this: y
|
|
|
-------------x
|
|
|
And you can define a point on that graph with an ordered pair like
this: (x, y). In three dimensions you have another axis (z-axis) going
from the origin (0, 0) diagonally from the top-right quadrant to the
bottom-left quadrant. Kind of like this: y z
| /
| /
|/
------------ x
/|
/ |
/ |
And now you have an ordered triplet to define a point: (x, y, z). //
End Boring Stuff
// Start Hard Stuff This is the part that is
going to require some thinking. This will actually be the longest part of
this tutorial. Once I get this stuff covered everything else should be
just regurgitation. Also this is where I hope to cover everything on that
list I had above. And according to the list "Matrix Math" is up first. If
you did not already know a matrix is any rectangular array of numbers
written within brackets. They are usually denoted with a capital letter
and classified by its dimensions. Here, matrix A is read as a 3 by 4
matrix: | A11 A12 A13 A14 |
| A21 A22 A23 A24 | = A3x4
| A31 A32 A33 A34 |
I went ahead and added in those elements in the matrix just to show you
how we can refer to those values by only saying "A14" instead of "the
element at the top-right corner". And as you can see the matrix is defined
by the number of rows by the number of columns (3 by 4 in this case). And
for conformity's sake we are going to call the number of rows in a matrix
"i" and the number of columns "j". Ok...that is pretty basic. Knowing how
to multiply matrices is harder.
First of all there are certain
requirements that the two matrices must meet in order for it to be
possible to multiply them. If you have matrix Am x p and
Bp x n then the number of columns in matrix A have to be equal
to the rows in matrix B...you can probably already see that since I gave
the columns in A and the rows in B the same variable name. So let's say
that the two matrices are capable of being multiplied. The product matrix
will have the dimensions of m x n or in other words, the product matrix
will have the same number of rows as A and the same number of columns as
B. Nothing hard yet. It is finding the elements of the product matrix that
can be confusing. A standard text book definition might go something like:
Any element in the ith row and the jth column of the product matrix is
the sum of the products of the corresponding elements of the ith row of
A and the jth column of B.
I do not know if that clears anything up so I am going to give an
example: first with element places like the "A11" kind of stuff and then
with actually numbers. I am also going to be working with multiplying 3 x
3 matrix by 3 x 1 matrices because that will be what we are going to deal
with later on. Here is the element place example: | A11 A12 A13 | | B11 |
| A21 A22 A23 | = A | B21 | = B
| A31 A32 A33 | | B31 |
| [A11 * B11 + A12 * B21 + A13 * B31] |
AB = | [A21 * B11 + A22 * B21 + A23 * B31] |
| [A31 * B11 + A32 * B21 + A33 * B31] |
I would suggest looking at that carefully and examining any patterns
you see. It is very hard for me to explain this but it is absolutely
crucial that you know it. Here is an example with numbers: | 2 3 1 | | 3 |
| 3 2 4 | = A | 4 | = B
| 1 1 2 | | 5 |
| 2(3) + 3(4) + 1(5) | | 6 + 12 + 5 | | 23 |
AB = | 3(3) + 2(4) + 4(5) | = | 9 + 8 + 20 | = | 37 | <<--- This is the product | 1(3) + 1(4) + 2(5) | | 3 + 4 + 10 | | 17 |
That last part is the final answer...and I just checked it on my
graphing calculator and it is correct. Now whether it be because I do not
have a very good grip on the English language or because I am not good at
this I feel that is the best I can explain it. If you are still uncertain
about this then go do a search at the Dr. Math forums for more help: http://www.mathforum.com/dr.math/
.
Ok...well before we move on I would just like to tie back into
or main goal here (3-D just in case you forgot...haha). The above matrices
are basically what we are going to end up with in the end. Matrix A would
be the rotation matrix and matrix B would hold the ordered triplet of the
point we are rotating. I just thought I would show some relevance to what
I am talking about here. But that is still not it for matrices. I also
need to tell you about the identity matrix.
The identity matrix is
the square matrix I of dimensions n x n, whose main diagonal
from, upper left corner to lower right corner contains only 1's. All other
elements are 0. The product of In x n and any other matrix
An x p is the matrix A. An example: I A IA
| 1 0 0 | | 2 3 1 | | 2 3 1 |
| 0 1 0 | | 3 3 1 | = | 3 3 1 |
| 0 0 1 | | 4 5 2 | | 4 5 2 | All right, on with more
hard stuff! I am going to kind of go out of order from the list way above
but oh well. Really this next thing should be nothing new: the Pythagorean
Theorem. Now I am pleading with you just to except it for what its worth.
The proof of it is not hard but it is long and this tutorial is going to
get long and boring enough. So do we have an agreement? Just except it! Oh
yeah...and the theorem is: c2 = a2 + b2
where "a" and "b" are the legs of a right triangle and "c" is the
hypotenuse. Here is an example: |\
| \
a=3 | \ c=5
| \
|________\
b=4
So plug in your numbers into the Pythagorean Theorem: 52 =
32 + 42 which equals 25 = 9 + 16 and 25 = 25 . Easy.
The Distance formula is basically the same thing: D2 = (x1 -
x2)2 + (y1 - y2)2 where (x1, y1) and (x2, y2) are
two ordered pairs on a graph. To visualize that here is a picture: |
| . p1
|
|
|
| . p2
|_______________
connect the points with lines so that it makes a right triangle:
|
| . p1 | |\
| | \
| | \
| |________\. p2
|________________
Now simply take the difference of the x-positions of the two points and
that is your "b" value, and take the difference of the y-positions and
that is your "a" value...your "c" value would be the distance between the
two points (hence the Distance Formula). So once you have taken the
differences between the positions and added their squares you then take
the square root of it all for the final distance. Take a look at this: . p1 (x1,y1)
|\
| \
| \ c
a | \
| \
| \
|____________\. p2 (x2,y2)
b
a = y1 - y2
b = x1 - x2
________
c = \/ a2 + b2We are very close to being able
to prove the Sum and Difference Identities (which 3-D consists of). But we
still need to learn about the Law of Cosines. I suggest that you just all
around "get this". It is very useful. The Laws of Cosines are as follow: For any triangle ABC, where a, b, and c are the lengths of the sides opposite the angles with measures A, B, and C, respectively, a2 = b2 + c2 - 2bc cos A
b2 = a2 + c2 - 2ac cos B
c2 = a2 + b2 - 2ab cos C
Let's look at an oblique triangle and find out why those equations
work: B
/|\
c / | \ a
/ | \
/ h | \
/ |_ \
A /__________________|_|________\ C
b - x D x
Length of AC is b
// Begin Triangle Description This triangle is acute. Its three
angles are A, B, C, and its three sides are a, b, c. A line perpendicular
to AC is drawn through B and intersects AC at D to show the altitude of
the triangle and it is denoted with an h. Distance DC is "x" and AD is b
minus "x" since b is that entire side of the triangle. // End Triangle
Description
So now with only that information let's try to solve
for "c": 1.) Apply the Pythagorean Theorem to triangle BDA c2 = (b-x)2 + h2
2.) Expand the above equation c2 = b2 - 2bx + x2 + h2
3.) Leave the above equation alone for a minute and apply Pythagorean Theorem to triangle BDC a2 = h2 + x2
4.) Now since we know that h2 + x2 equals a2 we can substitute h2 + x2 in the equation
in step 2 with a2: c2 = b2 - 2bx + a2
5.) since cos C = x / a , then x = a cos C
c2 = b2 - 2b (a cos C) + a2
6.) Straighten everything up
c2 = a2 + b2 - 2ab cos C <<-- We just derived the Law of Cosines!
Easy enough right? Even though that is only one of the laws the
derivation of all of them are the same. But that equation probably does
not mean much to you huh? It would be best for me to give an example. Lets
say we had a triangle that looked like this (not drawn to scale)
:

We know the measure of one angle as well as the lengths
of the two adjacent sides. What we want to find out is the measure of "a".
Here is the math work along with comments on each step: // first find out which equation you need to use. You are trying to find "a" so look at all of the law of cosine equations and see which one has an "a" by itself on one side of the equation. For this problem we would use: a2 = b2 + c2 - 2bc cos A. Now just plug those numbers in:
a2 = 62 + 102 - (2 * 6 * 10) cos 50
// simplify a little
a2 = 36 + 100 - 120 cos 50
// get out a calculator and solve for a2
a2 = 58.865
// since you have an a2 on the left side of the equation you can take the square root
of both sides to solve for a
a = 7.672 That is how you would solve for a. Even outside of 3d that
equation will come in handy.
Now this Law of Cosines will help us
prove the Sum and Difference Identities which in turn will help us derive
the rotation matrices. But there is still one more thing that you need to
know about before we get into the identities (its the last one...I
promise). Let's talk about the Unit Circle. The Unit Circle is mainly used
in Trigonometry and it helps you memorize some of the values of the ratios
of easier angles like 0, 30, 45, 60 and 90 degrees. We do not even need to
discuss that though. You do, however, need to know that a unit circle is a
circle with a radius of one centered at the origin (0, 0). The thing that
is so fascinating about the unit circle is that if you were to pick any
point on the circle and imagine a line going from that point to the center
you can find the x-position of that point by taking the cosine of the
angle which is made from the "imaginary" line and the x-axis as well as
the y-position of the point by taking the sine of that angle. If you are
confused look at this picture:
As long as
the radius is one then you can find the positions of point A and B easily:
x y
A: [cos q, sin q]
B: [cos f, sin f]
And it really is that simple. If the radius is not one then you simply
multiply each position of the ordered pair by the radius. Like this: x y
A: [r * cos q, r * sin q]
B: [r * cos f, r * sin f]
And that is about it. I would love to go more into the unit circle but
it is kind of beyond the scope of what we want to do so I will save it for
some other time. Just memorize those first two equations below the unit
circle picture.
Ok now I think we are prepared to go into the sum
and difference identities. First of all here are what the identities
state: Difference Identity for Cosine
cos (a - b) = cos a cos b + sin a sin b
Sum Identity for Cosine
cos (a + b) = cos a cos b - sin a sin b
Difference Identity for Sine
sin (a - b) = sin a cos b - cos a sin b
Sum Identity for Sine
sin (a + b) = sin a cos b + cos a sin b Those
equations may not seem like much right now but they will come in handy in
a minute. There are only two identities up there that we need but we have
a kind of "order of operations" that we need to follow before we can get
to the two we want. First we need to learn the difference identity for
cosine, then the sum identity for cosine, after that the difference
identity for sine, and finally the sum identity for sine. The sum
identities are the ones we want and as you can see they come second and
last in our list so lets get started as soon as possible. First up: the
difference identity for cosine...
Just in case you did not know the
Greek letters a (alpha) and b (beta) are being used to represent angles in those
above identities. Here is a picture to help you visualize
this:
There should be some stuff in there that you already
know about. The ordered pair of point A and B is exactly what I was
talking about when I discussed the unit circle above. The only thing you
should not know about (well you might already) is how to find the sine and
cosine of a minus b
which in turn would be the ordered pair of a point on the circle. Imagine
you rotate from (1, 0) counterclockwise a
degrees, and then you subtract b degree from
a. That point at which an "imaginary" line
intersects the circle with an angle of a minus
b is what this identity finds. So let's prove it
for cosine. First use the law of cosines to find the distance from A to B:
(AB)2 = 12 + 12 - 2(1)(1) cos (a - b)
= 2 - 2 cos (a - b)
|------------------ Therefore, the distance from point A to point B = \| 2 - 2 cos (a - b)
Now use the distance formula to find the distance from point A to point B. Remember that you can use the sine and cosine of an angle to find an ordered pair:
|------------------------------------ AB = \| (cos a - cos b)2 + (sin a - sin b)2
F.O.I.L out the two set of parentheses:
|------------------------------------------------------------ = \| cos2a - 2cos a cos b + cos2b + sin22a - 2sin a sin b + sin2b
Simplify the above equation. First I grouped sin2a and cos2a together to equal one. Why? Well if you read my "Introduction to Trigonometry" you would have seen the Pythagorean Identities. They simply say: sin2q + cos2q = 1. This is hardly the right time to go over why that is. I have also grouped sin2b and cos2b to equal one...you may want to look at this step for awhile:
|------------------------------------ = \| 1 + 1 - 2cos a cos b - 2sin a sin b
Further simplification of the above equation:
|-------------------------------- = \| 2 - 2cos a cos b - 2 sin a sin b
So the above equation is the distance from A to B using the distance formula. Also we have an equation somewhere at the top of this math part where we used the law of cosines to find the distance. Now we just need to set them equal to each other (since they do equal each other) and then simplify:
|----------------- |-------------------------------- \| 2 - 2cos (a - b) = \| 2 - 2cos a cos b - 2sin a sin b
More math. the square roots cancel out:
2 - 2cos (a - b) = 2 - 2cos a cos b - 2sin a cos b
More math. The 2's on each side of the equation cancel out. Also I factored out a -2 on the right side of the equation:
-2cos (a - b) = -2(cos a cos b + sin a sin b)
Last bit of math: divide both sides of the equation by -2:
cos (a - b) = cos a cos b + sin a sin b
And there you go! You just proved the Difference Identity for cosine!
With that out of the way we need to move on to the sum identity for
cosine. I'm going to use the difference identity from above to help us out
with this: 1.) If you know that a number minus a negative number is the same as adding the two numbers then this step should not be anything out of the ordinary. If you did not know that then learn it: a - (-b) = a + b. The negatives cancel out. So our first step is simply:
cos (a + b) = cos [a - (-b)]
2.) Now we use the difference identity for cosine:
cos (a + b) = cos a cos (-b) + sin a sin (-b)
3.) And if you remember anything from my "Introduction to Trigonometry" then hopefully you know the Odd-Even identities for sine and cosine. No time to discuss why but here they are: sin(-q) = -sin(q) and cos(-q) = cos(q). Now substitute those identities into the above equation:
cos (a + b) = cos a cos b + sin a (-sin b)
Therefore:
cos (a + b) = cos a cos b - sin a sin b
And that is it for deriving the sum identity for cosine. To prove the
sum identity for sine, you need to remember the two identities we just
learned along with the cofunction identities: sin q = cos (p/2 - q) and cos q = sin (p/2 - q). Here is the
difference identity for sine proof: 1.) Start with the cofunction identity for sine
sin q = cos (p/2 - q)
2.) Let q = a - b
sin (a - b) = cos [p/2 - (a - b)]
3.) Simplify between the brackets and rearrange so that you have a "term-plus-term" type form
sin (a - b) = cos [(p/2 - a) + b]
4.) Use the sum identity for cosine on the right side of the equation
sin (a - b) = cos (p/2 - a) cos b - sin (p/2 - a) sin b
5.) Use the cofunction identities to substitute for some terms
sin (a - b) = sin a cos b - cos a sin b
And that is the dirivation of the difference identity for sine. This
next part for the sum identity for sine is a lot like the sum identity for
cosine so I am not going to comment it thoroughly. 1.) Remember that a number minus a negative number is the same as the sum of those two numbers
sin (a + b) = sin [a - (-b)]
2.) Use the difference identity for sine on the right side of the above equation
sin (a + b) = sin a cos (-b) - cos a sin (-b)
3.) Use the Odd-Event identities to substitute somethings in the above equation
sin (a + b) = sin a cos b - cos a (-sin b)
4.) Simplify
sin (a + b) = sin a cos b + cos a sin b
Well if you understand all of that then you are really quick! If
you do not get it then you are not alone. It took me forever to understand
this stuff. However I think if you carefully re-read all of this it will
come to you sooner or later.
Ok...that is it for the hard part. We
can now get into the actual 3-D rotations. This next part should be only a
fraction of the length which all this other stuff took. From now on it
will be very easy. First take a look at these pictures which represent
what each axis rotation looks like:
x-axis rotation:

y-axis
rotation:

z-axis rotation:

Do you notice anything
somewhat strange in those pictures. It really is not all that strange but
more interesting. For every axis rotation the coordinates for that axis
are no affected by the transformation. Meaning if you were to rotate a
point on the x-axis only the y- and z-positions of the point would change
and the x would stay the same. Knowing this will help us figure out what
to fill the empty elements in our rotation matrices. Not all of the
elements of a rotation matrix are filled with equations.
With that
said let's ask ourselves what is 3-D? Well? What is it? Circular rotations
around the three axes right? So if you want to put the technical part of
3d into simple English all 3d is is to take the angle that a point makes
with one of the three axes, add an increment angle to rotate the point,
and then find that new point. Instead of being so generalized let me
specify on one thing and let you extrapolate it to the big picture. Let's
take z-axis rotations for instance. The reason I picked the z-axis was
because it is the easiest to understand. A z-rotation only makes the
object move on a circular path around the 2d origin. The z-coordinate is
not changed...only the x and y, therefore its usually the easiest for
people to understand.
For clarification I am going to give the
angles we discussed in the last paragraph some names. We are going to call
the current angle that the point makes with one of the axes q, the angle at which the object is being rotated a, and their sum will be b...I'll get to that part in a minute. Here is a
picture to help you understand better:

You have point A which
makes up an angle, q, with the x-axis which has
the ordered pair (x, y). You then want to rotate it by a degrees which will change the ordered pair to (nx,
ny). Now that is pretty much all 3d is right? Taking a point and rotating
it around a center by some angle increment. And the way to rotate by an
increment would be to take the current angle the point makes up with an
axis, add the increment to that angle, and find the new ordered pair after
the transformation. This is where our sum identities for cosine and sine
come into play, which we derived previously. I'm going to type them again
with the angle symbols we are using now: Sum Identity for Cosine
cos (q + a) = cos q cos a - sin q sin a
Sum Identity for Sine
sin (q + a) = sin q cos a + cos q sin a
So with those identities we are able to find the cosine and sine of the
original angle that a point makes with an axis plus the increment
angle. But what does that matter? I mean so what if we are able to find
that right? Doh! This shows just how important the unit circle is!
Remember that the cosine and sine of an angle is the ordered pair for a
point on the unit circle...right? Well that is what those identities are
basically doing. However we can substitute a few things in those
identities now. If the point is originally (before the transformation) at
an angle of q with the x-axis then we can just
use the point's x- and y-positions for cos q and
sin q since we know the unit circle so good.
Let's do that: cos (q + a) = x * cos a - y * sin a
sin (q + a) = y * cos a + x * sin a
If you add the original angle plus the increment angle you get b. Now to find the point with an angle of b you simply take the cosine and sine of that
angle...once again the unit circle. Now we can substitute b (which is q + a) with the new x- and y-positions: nx = x * cos a - y * sin a
ny = y * cos a + x * sin a
// remember that q + a = b and that cos b equals "x" after rotation and sin b equals "y" after rotation
// And while we are at it I am going to rewrite those two equations
just so that it looks better...it will also help you see the matrices
that they form:
nx = x * cos a - y * sin a
ny = x * sin a + y * cos a Well
believe it or not using those two equations are how you rotate a point
around the z-axis. And if you are confused then read over these last few
paragraphs again.
Now to put that in matrix form. Since a z-axis
rotation only affects x- and y-coordinates I am only going to use those
variables for now just to keep away from possible confusion. Here is what
our very generalized form of the rotation matrix looks like along
with the matrix which holds the "x" and "y" positions of our point: rotation positions
| a c | | x |
| b d | | y |
Its hard for me to explain how to find out what a, b, c, and d need to
be to output the two equations from above when multiplied by that position
matrix...I am not very good with words. But if you understood the matrix
multiplication pretty good then you should see it just by looking at the
matrices and equations for a second. The best I can explain it is to
simply show you what the product of the two above matrices would be: rot-z pos product
| a c | | x | -- | ax + cy |
| b d | | y | -- | bx + dy |
And that product matrix looks a lot like the two transformation
equations we derived above. So now you can just substitute a, b, c, and d
in the rotation matrix for the coeffecients that were paired with "x" and
"y" in the transformation equations: z-axis rotation matrix | cos a -sin a |
| sin a cos a |
And there you go. The final rotation matrix. However that is only
dealing with two dimensions. We need three dimensions right? So the matrix
has to be three by three instead of two by two. The hardest part of this
(probably) is finding where to put those elements in the new three by
three matrix. I'm just going to give you the final matrix and let you
figure out how I did it: | cos a -sin a 0 |
| sin a cos a 0 | = rot-Z
| 0 0 1 | So the elements from the old matrix go in
elements: A11, A21, A12, and A22...do you know how I came up with the
numbers for the elements we did not have like the third row and third
column? Remember the identity matrix? I simply put in numbers that would
not affect anything when multiplied by something.
And thats
it...well at least for the z-axis. We still have the x- and y-axes to
cover. But guess what...they are pretty much the exact same as what
we did above. I will still go over them to make sure there is no confusion
but it is all going to be redundant. Like the z-axis rotation here is a
picture to help you visualize what we are dealing with:

Make sure
to take note that although I am using the same Greek letters for the
increment rotation it really should not be. When you finally make your own
3d file you will want a separate rotation angle for all of the different
axes.
If you take a close look at that picture you will notice that
I did not change very much. Since x-axis rotations take place around the
x-axis (therefore the x-coordinates are not affected from this rotation)
we are going to look at our axes from a side view. Now instead of the
z-axis going from the origin to the back of the screen and out of the
screen the x-axis is. So what used to be a regular x and y axis graph is
now a z and y axis graph. That should not be all that hard to
understand.
Ok...next I am going to cover pretty much every step I
did with the z-rotations except with fewer comments...mainly just the
work: cos (q + a) = cos q cos a - sin q sin a
sin (q + a) = sin q cos a + cos q sin a
// substitute
nz = z * cos a - y * sin a
ny = y * cos a + z * sin a
// rewrite and reorder equations...notice that I also switched around ny and nz..now ny is first and nz is second...just dont want you to get confused
ny = y * cos a + z * sin a
nz = y * -sin a + z * cos a
// matrices setup
rotation pos product | a c | | y | -- | ay + cz | | b d | | z | -- | by + dz |
x-axis rotation | cos a sin a | | -sin a cos a |
full 3 x 3 matrix | 1 0 0 | | 0 cos a sin a | | 0 -sin a cos a |
And a quick picture and run down of y-axis rotations...now we are
viewing the axes from an overhead view:
cos (q + a) = cos q cos a - sin q sin a
sin (q + a) = sin q cos a + cos q sin a
// substitute
nx = x * cos a - z * sin a
nz = z * cos a + x * sin a
// rewrite and reorder equations:
nx = x * cos a - z * sin a
nz = x * sin a + z * cos a
// matrices setup
rotation pos product | a c | | x | -- | ax + cz | | b d | | z | -- | bx + dz |
y-axis rotation | cos a -sin a | << -- pretty much the same as the x-axis rotation matrix | sin a cos a |
full 3 x 3 matrix | cos a 0 -sin a | | 0 1 0 | << -- this is what makes it different from the x-rotation | sin a 0 cos a |
So if we put all of those matrices together for our final list
of transformation matrices you will have something like this: a = angle of rotation around the x-axis b = angle of rotation around the y-axis
f = angle of rotation around the z-axis
| 1 0 0 | | 0 cos a sin a | = rot-X | 0 -sin a cos a |
| cos b 0 -sin b | | 0 1 0 | = rot-Y | sin b 0 cos b |
| cos f -sin f 0 |
| sin f cos f 0 | = rot-Z
| 0 0 1 |
// And on a side note just in case you did not know. You multiply those matrices by your position matrix:
| x | | y | | z |
Now there is something very, very, very important to note here.
These matrices sometimes fluctuate depending where you see them. It kind
of all depends on how you look at the axes when deriving the matrices. The
ones that we have come up with will work but there are other
matrices that will also work. There is not a big difference though...just
a sign change here or there. The one matrix which probably everyone will
agree on is the z-axis one because it is usually the first derived since
it is the easiest.
Personally, kind of on a side note, I dislike
computers. I am a fan of working out problems on paper. So that is
what I did before I even started this tutorial and I decided to scan my
work so you could see it how it should be seen. Pardon my terrible
handwriting though. Here are the links:
Law
of Cosines Difference
Identity for Cosine Sum
Identity for Cosine Difference
Identity for Sine (top) Sum
Identity for Sine (bottom) Z-Axis
Rotation Matrix
I did not show my work for all three axes
because they are pretty much the same. I figured one was good
enough.
Now after all of this I bet you are wondering why I ever
made you put those equations in matrix form since if you were to multiply
everything out it would come out the same as the equation before we
changed it to matrices. And my answer...because it was fun. Yes it
is true that there really was no point in me having you put it in
matrix form but I thought it would be a good learning experience. Also
whenever you see more articles written about 3d they will usually deal
with matrices not regular equations. And on top of that using matrices
will also help you see the other ways of manipulating 3d. Such as scaling
and translating. Also I have not even showed how to add perspective. I
will get to that some other time though. This tutorial has gotten way too
long and I am tired and I still need to proofread this thing.
I'm
not exactly sure what kind of people are going to be seeing this but I was
mainly writing this in response to the Flash community. I hate to see
people boast so I had to write this since way too many people were making
these "3D-Engines" and bragging. I just had to let people know that
there is nothing hard about this. However if you are not a Flash developer
then this is probably where the ride ends. Anything else I write about
having to do with 3D will be about integrating it into Flash.
I
made a sample file a few weeks ago which I was distributing over at Flashmove which you may want to check
out. You can view it here
and download it here.
I have optimized it to the best of my knowledge...I hope you like my style
of programming. It has a few extra things going on but you should be able
to see everything we just went over in this file. I'm planning on writing
another tutorial dealing with 3d except next time I will show you how to
derive a perspective equation along with scaling and translating points. I
also plan on going over my fla with you.
Until next
time...
- Brandon Williams
|