Matrices are often feared by non math-savvy programmers. Many a game resorted to the old Yaw/Pitch/Roll rotation system and only constructs matrices on-the-fly because the programmer felt more at ease with the conventional yaw-pitch-roll rotation system.
This article tries to uncloud the mystical matrix structure and explains why you needn't fear matrices and should, in fact, dump the good ol' yaw-pitch-roll rotation system for good.
Vectors
Let's start this by examining vectors, which you probably already know. A 3D vector carries an X, Y and Z coordinate. Vectors were originally meant to store directions exclusively, but they are also commonly used by programmers to store positions in space.
If the X axis of your world coordinate system was extending to the right, then a vector of {X:1 Y:0 Z:0} would be pointing to the right and a vector of {X:-1 Y:0 Z:0} would be pointing to the left.
It's really that simple. Just assume you're sitting in the center of your world's coordinate system at {X:0 Y:0 Z:0}. Take any vector and draw a point at the coordinates contained in it. Look at the point. There, you're looking in the direction that's stored in the vector!
Vectors can have different lengths. Going back to the previous example, a vector of {X:2 Y:0 Z:0} would still be pointing to the right, but have a length of 2 units. You'll often hear the term "normalized". A normalized vector is simply a vector with a length of exactly 1, or, in other words, it is a pure direction.
What's in a Matrix?
Your typical 4x4 matrix (that means an array of 4 times 4 numbers) looks like this:
M21 M22 M23 M24
M31 M32 M33 M34
M41 M42 M43 M44
It stores an orientation and a position. The orientation is contained in the upper left 3x3 values:
Up.X Up.Y Up.Z ?
In.X In.Y In.Z ?
? ? ? ?
Right, those are three vectors pointing in different directions.
- The first line contains the X, Y and Z coordinates of a vector pointing towards the right.
- The second line carries the coordinates of a vector that's pointing upwards
- The final line is a vector that's pointing forwards
All three vectors need to be normalized (as explained before, that means they need to have a length of exactly 1.0 units).
That's it, only 3 harmless vectors. When you come accross 3x3 matrices, well, that's all there is to them!
The additional fields in a 4x4 matrix are used to store two different things:
Up.X Up.Y Up.Z Scale.Y
In.X In.Y In.Z Scale.Z
Position.X Position.Y Position.Z <always 1.0>
At the bottom row (in fields M41, M42 and M43), a position is stored. This works just the same as when you would manually store the X, Y and Z coordinates of your object, camera, player or whatever the matrix is used for.
At the rightmost columns, in M14, M24 and M34, the matrix stores the scale of your object. If you would set them to 2.0 each, that would indicate that the object is twice as big as it normally is.
So, in two simple sentences, a 3x3 matrix stores an orientation using 3 vectors; one pointing right, one up and one into the direction the matrix is facing. A 4x4 matrix additionally stores the position and scale of an object.