If you find this article contains errors or problems rendering it unreadable (missing images or files, mangled code, improper text formatting, etc) please contact the editor so corrections can be made. Thank you for helping us improve this resource
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.