3D with matrices
Matrices provide an alternate (better) way to rotate and move your 3D world. They are far more complicated than the last tutorial and use more difficult maths. You do not really need to completely grasp the maths to be able to use them but it is useful to have a basic understanding.Previous:3D rotation
next:3D shading
Matrix World Manipulation
Firstly you must create a world matrix or camera matrix which defines the general translation and rotation of the world. This will be applied to the objects later.[R R R T] Or [R R R] [T]
[R R R T] [R R R] [T]
[R R R T] [R R R] [T]
[0 0 0 1]
I personally prefer the second method with the separate translation vector and 3 by 3 rotation matrix but in this tutorial I will uses the former because it seems to be more conventional.
Matrix Multiplication
Multiplying matrices is quite simple once you know how. What you have to do is multiply a row by a column. You work out the row and the column by which row and column the space you are multiplying is in i.e. top left space is top row from the first matrix multiplied by the left column from the second matrix. Always remember that the row comes from the first matrix and the column form the second. The matrices you are multiplying must be the same size.To multiply a row by a column all you have to do is multiply the corresponding pairs and then add all of the result together i.e. R1*C1 + R2*C2 + R3*C3 etc.
Multiplying a 2 by 2 matrix
a b * 1 2 = a1+b3 a2+b4
c d 3 4 c1+d3 c2+d4
One with numbers;
1 2 * 6 4 = 10 10
3 5 2 3 28 27
As you can see the resulting matrix is quite a lot larger than the starting two. This is even more pronounced in larger matrices.
A 4 by 4 just for fun! (I have written a nice little program that does these for me. Look on the source code page.)
2 3 4 4 1 5 8 4 117 98 69 296
1 4 7 3 * 9 4 7 76 = 171 134 88 381
3 8 8 6 17 14 7 7 241 189 142 784
5 9 2 6 5 5 1 8 150 119 123 766
Just as a note of interest which doesn't have any relevance, an identity matrix will always keep the matrix that it is multiplying the same. They always look like this;
1 0 0
0 1 0
0 0 1
Rotation
We combine these three rotation matrices together to make a single rotation matrix.1 0 0
Rx = 0 cosa sina
0 -sina cosa
cosb 0 sinb
Ry = 0 1 0
-sinb 0 cosb
cosc sinc 0
Rz = -sinc cosc 0
0 0 1
We combine them by multiplying them together and get this resulting rotation matrix:
Yang, Xang, Zang - angle for rotation round the Y, X, Y axes.
m.m11 = Cos(Yang) * Cos(Zang)
m.m12 = -Cos(Yang) * Sin(Zang) * Cos(Xang) + Sin(Yang) * Sin(Xang)
m.m13 = Cos(Yang) * Sin(Zang) * Sin(Xang) + Sin(Yang) * Cos(Xang)
m.m21 = Sin(Zang)
m.m22 = Cos(Zang) * Cos(Xang)
m.m23 = -Cos(Zang) * Sin(Xang)
m.m31 = -Sin(Yang) * Cos(Zang)
m.m32 = Sin(Yang) * Sin(Zang) * Cos(Xang) + Cos(Yang) * Sin(Xang)
m.m33 = -Sin(Yang) * Sin(Zang) * Sin(Xang) + Cos(Yang) * Cos(Xang)
Translation
To get the translation we just take our translation vector (how far we have moved in each direction) and multiply it by our world matrix.Tx = R1 R2 R3 . Tz = R1*Tx + R2*Ty + R3*Tz
Ty = R4 R5 R6 . Ty = R4*Tx + R5*Ty + R6*Tz
Tz = R7 R8 R9 . Tx = R7*Tx + R8*Ty + R9*Tz
Putting it all together
Now we have the rotation and translation part of our matrix. All the remains is applying it to the dots. This is the same process as you applied to the translation except you must remember to add the translation part of the vector.
Download the source code for this tutorial
Previous:3D rotation
next:3D shading
© Jonathan Waller 2005; QuantumState Visual Basic




