{"id":41,"date":"2013-11-08T15:46:23","date_gmt":"2013-11-08T15:46:23","guid":{"rendered":"\/\/3dbym.ru\/2013\/11\/building-transformation-matrices\/"},"modified":"2013-11-08T15:46:23","modified_gmt":"2013-11-08T15:46:23","slug":"building-transformation-matrices","status":"publish","type":"post","link":"https:\/\/3dbym.ru\/2013\/11\/building-transformation-matrices\/","title":{"rendered":"Building Transformation Matrices"},"content":{"rendered":"
One of the most important uses for matrices in game programming is for transformations. Transformation matrices are used to position geometry and other objects onscreen. There are five main transformation matrices that you will use when working with 3D models. They are rotation around the X-axis, rotation around the Y-axis, rotation around the Z-axis, transla\u00adtion, and scaling. A rotation matrix, whether around the X, Y or Z axes, will rotate all of the affected geometry around that specific axis. A transla\u00adtion matrix is used to move geometry from one place to another, and a scale matrix is used to change the size of the geometry.<\/p>\n
The best thing about these matrices is the capability to multiply them together to perform several transformations with a single matrix. Keep in mind that the order of multiplication is important here; multiplying transformation matrices in one order will not necessarily give you the same result as multiplying the same matrices in a different order. Let\u2019s look at an example.<\/p>\n
Say you have three rotation matrices. The first is a rotation of 90 degrees around the X-axis, the second -90 degrees around the X-axis, and the third a rotation of 90 degrees around the Y-axis. If you multiply them in<\/p>\n
<\/p>\n
the order of the first times the second, times the third, you will end with a rotation of 90 degrees around the Y-axis because the first two will cancel each other out. However, if you multiply in the order of the first times the third, times the second, your end result will be a positive 90 degree rotation around the Z-axis. Quite a difference.<\/p>\n
So, how do you create these transformation matrices? Each rotation matrix will rotate a certain number of degrees around the X, Y, or Z axis. Figure 1.13 shows the X, Y, and Z rotation matrices.<\/p>\n
\n 1 0 0<\/p>\n<\/td>\n | \n 0<\/p>\n<\/td>\n | \n<\/td>\n | \n cos(0)<\/p>\n<\/td>\n | \n 0 — sin(0)<\/p>\n<\/td>\n | \n 0<\/p>\n<\/td>\n | \n<\/td>\n | \n cos(0)<\/p>\n<\/td>\n | \n -sin(0)<\/p>\n<\/td>\n | \n 0<\/p>\n<\/td>\n<\/tr>\n |
\n 0 cos(0) — sin(0)<\/p>\n<\/td>\n | \n 0<\/p>\n<\/td>\n | \n<\/td>\n | \n 0<\/p>\n<\/td>\n | \n 1 0<\/p>\n<\/td>\n | \n 0<\/p>\n<\/td>\n | \n<\/td>\n | \n sin(0)<\/p>\n<\/td>\n | \n \u041e<\/p>\n \u043e<\/p>\n<\/td>\n | \n 0<\/p>\n<\/td>\n<\/tr>\n |
\n 0 sin(0) cos(0)<\/p>\n<\/td>\n | \n 0<\/p>\n<\/td>\n | \n<\/td>\n | \n sin(0)<\/p>\n<\/td>\n | \n 0 cos(0)<\/p>\n<\/td>\n | \n 0<\/p>\n<\/td>\n | \n<\/td>\n | \n 0<\/p>\n<\/td>\n | \n 0<\/p>\n<\/td>\n | \n 1<\/p>\n<\/td>\n<\/tr>\n |
\n 0 0 0<\/p>\n<\/td>\n | \n 1<\/p>\n<\/td>\n | \n<\/td>\n | \n 0<\/p>\n<\/td>\n | \n 0 0<\/p>\n<\/td>\n | \n 1<\/p>\n<\/td>\n | \n<\/td>\n | \n 0<\/p>\n<\/td>\n | \n 0<\/p>\n<\/td>\n | \n 0<\/p>\n<\/td>\n<\/tr>\n |
\n RotX<\/p>\n<\/td>\n | \n<\/td>\n | \n<\/td>\n | \n RotY<\/p>\n<\/td>\n | \n<\/td>\n | \n<\/td>\n | \n RotZ<\/p>\n<\/td>\n | \n<\/td>\n<\/tr>\n | ||
\n Figure 1.13 The three rotation matrices. The left matrix<\/p>\n<\/td>\n | \n is<\/p>\n<\/td>\n | \n for rotation around the X<\/p>\n<\/td>\n<\/tr>\n<\/table>\n
|