top of page
Writer's pictureDilen Shah

Matrices

Updated: Jul 7, 2021

I know we all love Keanu Reeves in the Matrix and could talk for hours about the movie but we are here to talk about the math side of it and why is it so necessary and used in all 3D packages.


Matrices are very important elements in 3D math. They’re mostly used to transform objects within and through different coordinate spaces and in early math classes, we learn that a matrix is a two-dimensional array of numbers, and computer graphics usually only care about square matrices. More specifically, we mostly work with 4 x 4 matrices.


The most common use of a matrix in computer graphics is as a coordinate space transformation. If we apply a matrix to an object, the object is transformed to another location and orientation, applying the inverse of the matrix brings the object back to its original transformation.


Now, when I say we apply a matrix to an object, I really mean we are multiplying in objects. Transformation matrix by this new matrix and object’s location, orientation, and scale is represented by a matrix known as the objects transform or transformation matrix. This matrix describes the object coordinate system. If we look back at the makeup of a transformation matrix, we see a two-dimensional array of numbers with four rows and four columns.


The first three rows of a matrix represent the axis vectors of an object’s object space coordinate system. The fourth row represents the object’s translation relative to the parent coordinate space.


When we multiply Matrix A by Matrix B, it has the effect of transforming the coronets space A by the coroneted space B, which results in a new coordinate space, we can multiply many matrices together to transform an object. Many times the sequence of matrix multiplications can be combined to form a single transformation matrix that has the same effect.


The identity matrix is the matrix with one’s down the diagonal. The identity matrix represents a Coordinate space with no transformation. As you can see from the first three rows of the matrix, the axis vectors form the default or the normal basis.


Sitting at the origin that I mentioned in the vector post, multiplying a matrix by its inverse always results and the identity matrix. Multiplying any matrix by the identity matrix always results in the original matrix, it is similar to multiplying a number by one. Earlier I mentioned that an object’s place in space is represented by a transformation matrix. If we multiply object A by the inverse transformation matrix, the object will go back to the origin and we have a transformation matrix equal to the identity matrix.


Well, what happens when we multiply a different object? B by the inverse transformation matrix of object a multiplying an object B’s transformation matrix by another object. A’s inverse transformation matrix results in a local space transformation matrix of B with respect to A, in other words, A is now the parent coordinate space of B and this is commonly known as parenting in 3D packages.


If we switch the matrices around and multiply Object A’s inverse matrix by Object B’s matrix, the result is a matrix that specifies the transformation difference from A to B


We can also multiply points and vectors to a matrix to transform them into new locations and orientations, all the properties are multiplying by an inverse matrix that we discussed earlier apply to points and vectors too. You can think of vectors and points as a matrix with only one row. Multiplying a point by a matrix transforms the point to a new location, multiplying again by the first matrix brings the point back to its original location.


Multiplying a point by a matrix is often used to change the Coordinate space that a point is in. A vertex of this model is specified as a position relative to the object’s coordinate space. It is often useful to perform a deformation calculation in WorldSpace. To do this, we need to transform this vertex from object space to WorldSpace. We can transform the vertex into WorldSpace by multiplying the vertex point by the WorldSpace transformation matrix of the object. This returns the vertex point position relative to WorldSpace. Once we perform our defamation calculation in WorldSpace, we can put the point back into object space by multiplying it by the object’s inverse world transformation matrix.


We’ve been working with 4 x 4 matrices, why exactly are transformation matrices this size? Looking at the components of a matrix? We’ve already learned that the first three rows represent the axis vectors, that the coordinate system, and that the last row represents the translation relative to the parent coordinate space.


Well, various parts of the Matrix represent different transformation components. The upper 3 x 3 square represents the object’s Orientation. The diagonal represents the object’s Scale along each axis and the fourth row represents the object’s Translation. You may remember from old math classes that in order to multiply two matrices together, their inner dimensions must match. The resulting matrix has dimensions of the outer two dimensions.


Now, remember, I said that you can think of vectors as a matrix with one row. In other words, a 1 x 3 matrix. If the inner dimensions must match, how then do we multiply a one-by-three vector with a 4 x 4 matrix? The answer is we can’t. In order to multiply a vector by a 4 x 4 matrix, we need to add a fourth Coordinate to the vector called a homogeneous coordinate. We usually call this coordinate W and it is almost always won by adding a fourth Kornet to the vector. We can now multiply the vector by a 4 x 4 matrix. You may have noticed that when we were discussing the components of a matrix that the fourth column wasn’t really used for anything.


Well, since vectors are now, 1 x 4 matrices we will multiply a transformation matrix. We want to get a 1 x 4 matrix as a result. Therefore we use the fourth column of the 4 x 4 matrix to preserve the homogeneous coordinate of the vector


In the Vectors post, I mentioned that many systems treat vectors in points differently. I mentioned vectors should be used when working with directional data and points should be used when working with positions, both points and vectors have X, Y, and Z coordinates. So what exactly is the difference between a vector and a point? The difference between a vector and a point in Maya is that a point has a homogeneous W coordinate and a vector does not. What this means is that when you multiply a point by a matrix, the transformation takes into account the fourth row or translational portion of the matrix. When you multiply a vector by a matrix, since there is no fourth homogeneous coordinate in a vector, only the upper left three by three portion of a matrix is used in the transformation.


You may remember that this 3 x 3 portion of the matrix contains the orientation information of the matrix. This difference between points and vectors usually always causes unexpected results when people are learning to program with the Maya API or other systems that make this distinction.


Since a 4 x 4 matrix contains translation, orientation, and scale information in overlapping regions. How does one extract the individual components, for example, from the above image of the matrix, how can I tell how much this object has been rotated about the X-axis? Maya solves this problem by storing each component of a transformation separately, more specifically, a single transformation matrix, and Maya is composed of 11 different components.


By storing these components separately, they can be queried and edited individually and when the final transformation matrix needs to be calculated, all the components are collapsed into a single 4 x 4 transformation matrix.


So in the above image, I have just a little hierarchy here of the moon and the earth. Usually in 3d packages like this, when you parent something, you create a hierarchy, it’s like the moon here. You have these local transformation values and these values over here form the local transformation matrix in the space of its parent object, which is the earth here.


So in order to find the position of this moon, we first have to find the position of the earth. And then we got to find the position of the moon, when I say find the position, it’s just a series of matrix multiplications. So if I go up to the Earth over here, this is my world space here in order to find the position of the moon, after multiplying the Earth’s matrix and then followed by the moon’s matrix.


So you see, whenever you create a hierarchy like this, it’s just a series of matrix multiplications to determine the final position of a point or a transform. Usually, 3D packages do this automatically so you don’t really need to set it up in any manner but it’s always good to know the basics and the understanding of Matrices so you can use that knowledge when you try to write any deformer, tools, or scripts.


Matrices are essential concepts to learn in order to develop complex systems and computer graphics. Take your time to practice the use of matrices and how to extract the values and use them personally.


Thank you to many people who helped me along my way to understand 3D Math myself. Won’t be here if they did not help me learn and share their knowledge. Resources and images were either done by me or found online from Wikipedia and other related websites.

Until next time.

Recent Posts

See All

Applied 3D Math

Math has always been a fun or really bad experience for a lot of people, especially in the Animation and VFX world. Here is my go at...

Comentarios


bottom of page