# Like 4Likes Dislike Matrix Math

matrix mat matrices vert math operations concatenate 4x4 multiply

Matrix math is commonly used in 3D engines, unfortunately, I haven't found any tutorials on matrix math itself. I hope this one will help some people out. If you find this tutorial useful, please give me credit. That's all I ask.

Since the most common use of matrix math is 3D math, I will focus on this almost exclusively. What this means is that I will be using 4x4 matrices, and I will be describing typical 3D matrices (rotation, translation, etc).

Okay, with that out of the way, what is a matrix? Simply put, it's an array which holds numbers. In 3D math, we typically use a 4x4 matrix, which is analogous to a 4x4 array.

Well, that's simple enough, right? But, why do we do it? What's the point? I asked the same questions myself. Matrices have some interesting properties. One of which is the ability to concatenate many math operations into one matrix. What this means is that just one matrix can perform the task of literally hundreds of operations.

Matrices are also very easy to use. A matrix multiply routine is really all you need. The algebra is also fairly well hidden from the programmer which, therefore, makes the code easier to read. And, as one professional told me, matrices are very intuitive. By that he meant that you can look at a matrix, and can actually visualize something as complex as a rotation, just by the way the numbers are arranged.

So, how do we use matrices? In computer terms, we simply use a multidimensional array. The only thing that's even the least bit complicated is how to matrix multiply. This is how we concatenate multiple matrices into one. The following is how you would matrix multiply a 4x4 matrix by another 4x4 matrix.

That may look complicated but it really isn't. Just take a look at it and you'll see the simple pattern. Now, you may be thinking, 'Okay, now I can concatenate matrices, but what can I concatenate!' Fair enough. Let's take a look at the matrix form of some 3D algebra.

About the only matrix there that you may not understand is the identity matrix. It is really very simple, actually. It's like multiplying by 1. You can multiply any matrix by the identity matrix, and the result will be the original matrix.

Okay, now you can concatenate many different operations, including 3D algebra operations, into a single matrix. Now what? How do we use this final matrix? Here's where matrices really shine. If you want to perform all the operations in the final matrix to a 3D point, all you must do is multiply the matrix and the point. The result will be a 3D point transformed by the matrix. Here's what that looks like.

As you can see, in matrix math, we treat 3D points as 4x1 matrices (often called vertices). The points/vertices are homogeneous coordinates, hence the need for the 1. Okay, I suppose now you're thinking how all this can be related to code. It's really quite simple actually, but I will provide a couple functions for completeness.

Matrix Matrix_Multiply(Matrix mat1, Matrix mat2) {
int i, j, k;
Matrix mat;

// For speed reasons, use the unroll-loops option of your compiler
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
for(k = 0, mat[i][j] = 0; k < 4; k++)
mat[i][j] += mat1[i][k] * mat2[k][j];

return mat;
}

vertex matrix_vertex_multiply(matrix mat, vertex vert) {
vertex v;

v.x = vert.x * mat.matrix[0][0] +
vert.y * mat.matrix[0][1] +
vert.z * mat.matrix[0][2] +
mat.matrix[0][3];

v.y = vert.x * mat.matrix[1][0] +
vert.y * mat.matrix[1][1] +
vert.z * mat.matrix[1][2] +
mat.matrix[1][3];

v.z = vert.x * mat.matrix[2][0] +
vert.y * mat.matrix[2][1] +
vert.z * mat.matrix[2][2] +
mat.matrix[2][3];

return v;
}
That should be enough to start you off.

©1997 Jeff Weeks and Codex Software

### 2 Comments

May 25 2012 10:16 PM
I don't know how this never got any comments or ratings since it was posted 13 years ago, but this was the first thing I ever read about Matrices. I wrote a Matrix class just based on this article alone. Kudos on a good article, if the author is still even alive
Oct 21 2012 03:07 PM
hard to believe, nevertheless very kool work

Note: GameDev.net moderates article comments.