| We hope you enjoy your visit. You're currently viewing the Ultimate 3D Community as a guest. This means that you can only read posts, but can not create posts or topics by yourself. To be able to post you need to register. Then you can participate in the community active and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free. Join our community! If you are already a member please log in to your account to access all of our features: |
| Understanding transformation matrices; A tutorial to get the principles | |
|---|---|
| Tweet Topic Started: May 7 2009, 12:24 AM (12,875 Views) | |
| Dr. Best | May 7 2009, 12:24 AM Post #1 |
|
Administrator
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
Hi everybody, here comes another tutorial from my side. It will teach you what transformation matrices are, what they are good for and how they work. Once you have read it you should be able to understand, why the most common matrix operations look the way they do and how they work. This will allow you to create functions, which work with matrices by yourself and it will give you the base to understand more abstract texts dealing with matrices. So lets get started. What is a matrix: Matrices are very important in linear algebra and there is a rather abstract definition for them. Since we need matrices only for a very special purpose, we will ignore that abstract definition to focus on the relevant special case. So to us a matrix is a 2-dimensional array of real numbers. If you have integers n and m an n x m matrix is a structure made up of n*m real numbers, ordered into n lines and m columns. So for example a 4x3 matrix is a structure like this (it is not code, but the code block eases the formatting): Four lines, three columns all of them full with numbers, that is a 4x3 matrix. The numbers in a matrix are called entries. More generally matrices can be written with variables. This is usually done like this (the example uses a 4x3 matrix, but it works all the same with other matrices): By itself this definition is quite pointless, but do not worry, it will get more interesting quite soon. We will define mathematical operations like the multiplication for matrices and that will allow us to use them to describe transformations. When we are done multiplying a vector by a matrix will be equivalent to transforming it. So lets get to a more special type of matrices, the transformation matrices. Transformation matrices: Whenever something is done in 3D it is often necessary to describe the orientation and location of objects. Orientation and location can be summarized under the term transformation. One way to describe a transformation is to use a set of three coordinates for the location (together the coordinates form a vector) and three Euler angles for the rotation (known as yaw, pitch and roll or as rotx, roty and rotz in Ultimate 3D for Game Maker). Additionally three real numbers can be used to give the scaling. But there is a problem with this description of transformations. If you want to apply the orientation described by them to a point in 3D space this takes a lot of computing time. To apply the rotation you would have to use the trigonometric functions cosine and sine and using those takes quite much computing time. Transformation matrices solve this problem in a very nice way. 4x4 matrices can contain values, which make it very easy to apply transformations to points in 3D space. All that is needed are multiplications and additions. Both can be performed very fast, so the needed computing time is short. To understand how this works we need to have a look at coordinate systems. The following graphic shows a 3D model in an coordinate system. ![]() The coordinate system is made up of three axes, which are fixed to an origin. The axes are the x-axis in red, the y-axis in green and the z-axis in blue (RGB and XYZ). The origin is the white ball. To describe it we can use three vectors giving directions for the axes and one vector giving a position for the origin. To get the coordinates of a point on the model relative to the coordinate system you have to start at the origin and move parallel to the axes until you get to the point. The coordinates of the point result from how far you have moved along each of the axes. This has been visualized for the point on the left ear of Suzanne (the monkey). The coordinates are (length of the red line ; length of the green line ; length of the blue line). The lengths are measured relative to the lengths of the axes, so if the red line is twice as long as the x-axis the x-coordinate is two. Now have a look at this illustration. ![]() Suzanne has been rotated, moved and scaled. The old coordinate system is still there, but there is also a new one. It has been transformed exactly in the same way as Suzanne. If you determine the coordinates of points on the transformed Suzanne using this transformed coordinate system you get exactly the same result as with the untransformed Suzanne and the untransformed coordinate system. Now this means that we can describe the transformation by describing the new coordinate system in relation to the old one. And as stated above we can describe the coordinate system by giving three direction vectors for the axes and one position vector for the origin. So now we have four 3D vectors to describe a transformation. Those can be put into a 4x3 matrix easily. Lets say we have the direction vectors (x1;x2;x3), (y1;y2;y3), (z1;z2;z3) for the three axes and the position vector (o1;o2;o3) for the origin. Then the 4x3 matrix can look like this: This is the whole trick. This 4x3 matrix is a transformation matrix. Any combination of rotations, translations and scalings can be described through it (and some others like mirroring and shearing). We will see how transformations described this way can be applied to vectors, but first I want to introduce a useful terminology. It is quite unpleasant to write "coordinate systems" all the time. For this reason (and for some other reasons, which have their origin in the linear algebra) they are commonly called "spaces". In our use the terms "coordinate system" and "space" have the same meaning. So through every space you can measure coordinates of points (as described above). And you can convert coordinates from one space to another space using transformation matrices. A transformation matrix, which describes the axes and the origin of a space A using space B is referred to as A to B space transformation matrix. It is called like this, because it can convert vectors from A space to B space. One very important example for this is the following: Lets say you have a mesh (lots of vertices building up triangles). The vertices are given through lots of coordinates. These coordinates do not make sense without an interpretation through a coordinate system (a space), so we say that they are given in mesh space. We want to get them into the "global space" of our scene. This is a space, which is the same for all objects and it is referred to as world space. So what we need to get the coordinates of our mesh into the world in the right way is a mesh to world space transformation matrix. This matrix gives the complete transformation of the mesh. When describing a transformation matrix it is important to give the source space and the destination space. It is a very common bad habit, that people say things like "this is the bone space transformation matrix" or "this is the object transformation". Saying this is just pointless, because it does not say anything about the matrix. If the matrix described in the first phrase is a mesh to bone space transformation matrix it will be completely different than if it is a bone to world space matrix. So please do not use pointless descriptions like this. Always give both spaces. Now it is time to see how matrices in the form described above are used. Transforming vectors with transformation matrices: Above I said that transforming position vectors with Euler angles is simply too slow and that it is significantly faster with transformation matrices. Now it is time to see how that works. It is actually quite easy. Let (x; y; z) be a position vector in A space. We want to get it into B space. So we need an A to B space transformation matrix. Lets say we have that given in the same form as above (x1, x2, x3, y1, etc.). As stated above this matrix describes the axes and the origin of A space using B space. The vector is given in A space, so its coordinates tell how far you need to move along each axis of A space to get to the point. So what we do is the following: We multiply the x-coordinate of the vector by the direction vector for the x-axis of A space from the matrix. The same is done for the y-coordinate with the y-axis and for the z-coordinate with the z-axis. Then we add the results together. Finally we add the origin vector from the matrix. This is the same as starting at the origin and moving along the axis, it is just expressed mathematically. And since all vectors we have used in this process are vectors from the matrix, which are given in B space the resulting vector is the vector in B space, which corresponds to the given vector in A space. We just transformed it. Here is the formula: The vector (x'; y'; z') is the vector in B space then. The formula does exactly what I said above. The direction vectors are multiplied by the corresponding coordinates, the results are added together and the origin is added. This is the whole trick. If you have understood this you have understood the relation between matrices and 3D graphics. It is easy to see that the formula above is very effective. Just a few multiplications and additions. No cosine, no sine. CPUs can do this very fast. This is why matrices are so very important in 3D graphics. If they would not be used 3D graphics would be a lot slower. How to create transformation matrices: You know the meaning of transformation matrices now, but you do not really know how to create them. The nice thing about transformation matrices is that any other description of transformations (e.g. Euler angles) can be converted to matrices. They are very general. With the knowledge you have you could derive it all by yourself and this would be certainly a good practice, but it will be easier to use the existing formulas. I will not give them here, because this tutorial is about the basic principles of transformation matrices, but there are lots of good resources on the web. A very good one is the matrix and quaternion FAQ. The descriptions and formulas, which are given there are precise and understandable. So this is where you should continue reading, once you have finished this tutorial. Other matrix operations: With matrices you can perform more operations than just vector transformations. One important operation is that you can transform a matrix by a matrix. If you have an A to B space transformation and a B to C space transformation you can transform the first using the latter to get an A to C space transformation. This new matrix can get a vector from A to C space immediately, which is faster than transforming it twice. You can also invert matrices, which means converting an A to B space transformation to a B to A space transformation. And there are many more operations, which can be performed with them. Matrices have been used by mathematicians for a long time and for many different purposes. A mathematician (like me) could tell you a lot about them. They have thought of lots of interesting ways to do useful things with matrices. Again the matrix and quaternion FAQ will tell you more about that. Other transformation matrices: It is actually not very common to use 4x3 transformation matrices. To describe transformations, which include translations 4x4 matrices are used very often. For those the last column is (0; 0; 0; 1) most of the time. The vectors are 4D then and the last coordinate is usually 1. The reason for this is that the mathematical definition of matrices says that they should transform a vector, which contains only nulls into a vector, which contains only nulls. The 4x3 transformation matrices I introduced above do not do this, because there is the origin, which is simply added. Besides the additional column and the fourth coordinate can be used for some extra operations. In Direct3D they are used to do the projection transformation. The third coordinate holds, the value, which is written into the z-buffer, the fourth coordinate holds the value, by which the other values need to be divided. I ended up using 4x4 matrices with 3D vectors assuming that the fourth coordinate of the vectors is always one. Other than that 3x3 transformation matrices can be used to describe transformations without translations. Also the principles described above are not limited to 3D. They can be used for 2D just as well and they could be used for 4D, 5D and nD. Conclusion: What you just learned is a very general, efficient and useful concept. To give you an idea of how powerful matrices are: To convert a coordinate from mesh space to a screen coordinate (where the null-point is at the center of the screen) it is enough to transform it using a single transformation matrix and to perform one division and two multiplications on the result. This is why 3D graphics can be so fast. This tutorial just gave you the basic idea of transformation matrices, but it did this detailed. Having understood this principle is very useful and so is a good understanding of the "A to B space" notation. Now to make use of your knowledge you should continue by reading the matrix and quaternion FAQ. On purpose I wrote this tutorial so that it covers mostly the things, which are not explained in detail there. Having read this tutorial you should be able to understand the FAQ. I hope this tutorial is of some help, with friendly regards, Dr. Best |
![]() |
|
| Ormick | May 7 2009, 04:31 AM Post #2 |
|
Advanced Member
![]() ![]() ![]() ![]() ![]()
|
I'll have to read it a few times to get it through my head, but this helped me tremendously! Although I am still confused about the different columns. So basically, from my understanding, each column is one vector for each axis, right? Even if I'm wrong, thanks man!
Edited by Ormick, May 7 2009, 04:49 AM.
|
| Every forum needs an indecisive noob. I'm it. | |
![]() |
|
| Dr. Best | May 7 2009, 11:59 AM Post #3 |
|
Administrator
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
You are mixing up columns and lines. Each of the first three lines is one direction vector for one axis, the fourth line is one position vector for the origin. |
![]() |
|
| Ormick | May 14 2009, 09:48 PM Post #4 |
|
Advanced Member
![]() ![]() ![]() ![]() ![]()
|
sweet, that makes sense. and then the very last column is used to denote the magnitude of the vector or something? |
| Every forum needs an indecisive noob. I'm it. | |
![]() |
|
| Dr. Best | May 15 2009, 02:21 PM Post #5 |
|
Administrator
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
The last column of 4x4 transformation matrices is not really needed most of the time. If a 4D vector is transformed using a 4x4 matrix the result will be another 4D vector. The fourth column of the matrix describes what will be in the w-component of the resulting vector. Usually the fourth column is (0; 0; 0; 1) so that the new w-component equals the old w-component. If you meant to say the very last line, then this one is for the translation resp. the origin of the coordinate system. |
![]() |
|
| hanson | Oct 29 2009, 11:09 PM Post #6 |
|
Advanced Member
![]() ![]() ![]() ![]() ![]()
|
Thanks! I've really been in the dark about matrices - using them some based on example but not understanding them. This helps my understanding a ton! -hanson |
![]() |
|
| Adolph | Dec 27 2011, 05:02 PM Post #7 |
|
Newbie
![]() ![]() ![]()
|
Thanks!
Edited by Adolph, Dec 27 2011, 05:03 PM.
|
![]() |
|
| « Previous Topic · Tutorials · Next Topic » |





![]](http://z1.ifrm.com/static/1/pip_r.png)





9:01 PM Jul 11