matrix
Last updated
Last updated
3D computer graphics rely heavily on the use of matrices. Unfortunately in pure mathematics, the conceptual understanding of what a matrix "does" or "means" isn't explicitly taught, and this understanding usually doesn't come about until a student has a good understanding of linear algebra.
Although an understanding of matrices is not necessary to make many FRB games, knowing what a matrix does and how to use it in code can help improve your efficiency as a programmer.
This section will discuss matrices at a conceptual level. Don't worry, you don't need to know a lot of math to understand what I am going to explain here.
Although the world is round, and when you move north, the actual direction you are moving depends on your position, I will ignore this fact to keep things simple. That is, we can consider that moving north is always in the same direction and moving east is always in the same direction - much like people thought when they considered the world to be flat.
To begin our example, imagine standing on a large, flat area of land. There are no landmarks. In fact, there is nothing that you can use to determine whether you are facing north, south, east, or west. However, just because you don't know what is north doesn't necessarily mean there isn't a north (no, I'm not trying to get philosophical). In other words, there is a "true" north, only you don't know what it is.
Let's say you are given the command to walk a certain distance in a certain direction. That is, you are given the command, "Walk west 1 unit." In this situation, you have two problems. First, which way is west? Second, even if you knew which way west was, how long is one unit? Is it one foot? One meter? One mile?
To actually walk west 1 units, you will need two things. First, you'll need a compass to tell you which way you are moving. Second, you'll need some kind of measuring device to tell you how far one "unit" is.
Continuing the previous example, let's say that you now have a compass and a measuring stick. The compass tells you which direction you are facing, and the stick can be placed on the ground to indicate the length of one "unit". Now that you have a measure of direction and distance, you can accurately follow directions.
With this new equipment in hand, you receive the following orders:
"Move west 1 unit. Move north 1 unit. Leave a mark in the ground. Move east 2 units. Leave a mark in the ground. Move south 2 units. Leave a mark in the ground. Move west 2 units. Leave a mark in the ground."
In this situation, you should have moved as shown in the following image.
This of course assumes that north is up, east is right, and so on.
So far, we assume that the compass and the measuring stick are accurate. Or perhaps another way to look at it is that the person who gave you the two devices was honest. However, consider what would happen if the person who gave you the compass decided to play a joke. He modified the compass so that it is slightly inaccurate. That is, when the compass indicates that you are facing north, you are actually facing north-east. When the compass indicates east, you are actually facing south-east. South becomes south-west, and west becomes north-west.
An important point to make is that the square still appears to be a regular "square" to you, but to someone who knows which way true north is, it appears rotated by 45 degrees. Another interesting point is that the directions that you received didn't change at all - the only thing that changed is the compass. In fact, by changing the way that the compass points, you can actually draw a square of any orientation. To you, you are simply moving north, south, east, and west according to the compass.
Although this would probably be something you (as a holder of the measuring stick) would realize, in terms of math or instructions it is also possible to change the length of the stick. A measuring stick twice as long would produce a square that is twice as large.
What is important to realize is that the "directions" are separate from orientation and size. Conceptually, it's important to understand that the directions can change while compass or stick stay the same, or vice versa.
This brings us to two terms: "world coordinates" and "object coordinates". These are also referred to as "world space" and "object space". World space is the "authority" on what north is. That is, in our previous example, in world space, north is straight up and east is to the right. Object space is subject to a "compass" and "measuring stick" which may or may not be aligned with world space. In the situation where your compass worked correctly and the measuring stick actually measured one unit, then you were travelling in world space and object space. Object space and world space were aligned perfectly. Once the compass changes, the world space and your object space are no longer aligned. Travelling north in object space may result in you actually travelling a different direction depending on the compass. Also, you may be travelling a further distance than you really think you are if the measuring stick is larger than one unit in world space.
So far, we've treated direction and length as two separate things. That is, in the previous example, you are holding both a compass and a measuring stick. In this section, I will discuss how these two can be combined and represented mathematically.
Rather than using north, south, east, and west, I will use X and Y. Moving in the positive X direction is to the right, and positive Y direction is up.
You may recall first learning about the Cartesian plane in school. One of the first exercises students are given is to graph a series of points. When presented with a point such as (3,2), the students are told to move to the "right" 3 times, and "up" 2 times.
Another way to think about how to graph (3,2) is to use two "lines". That is, consider having one horizontal line of one unit in length and one vertical line also of one unit in length. To move to (3,2), you could use the horizontal line and place it end to end 3 times, then use the vertical line, start at the end of the 3rd horizontal line and move in the vertical line's direction the length of two lines.
The idea is similar to the example from before. To find the point at (3,2), simply place 3 horizontal lines end to end, then 2 vertical lines. Notice that (3,2) no longer means "Move 3 units to the right and 2 units up", but rather "Move 3 units along the blue line and 2 units along the red line". The lines are now our compass and measuring stick and (3,2) are the instructions.
Just as before, we can rotate the lines so that they point to any direction. Also, the lines can be made longer or shorter to stretch or shrink how far away from the origin points appear. Using two lines rather than a compass and measuring stick gives us a few extra "abilities". One is that the lines can rotate independently. They don't have to form 90 degree angles, but can point in similar or even opposite directions. Another ability is that we can stretch or shrink the lines independently.
The next thing that we need to understand is how to define these "lines". So far I've used the word lines to describe the way to move when receiving an "instruction" such as (3,2). A more proper term is vector. Consider that any of the previous vectors that we used to define which way to move when receiving instructions can actually be represented by a coordinate system as well. That is, the horizontal blue line pointed to the right 1 unit and the vertical red line pointed up 1 unit. In terms of vectors, we can say that the horizontal line is a vector represented by (1,0) in world space. That is, in an "unmodified" coordinate system, moving along the blue line from the origin will take you to (1,0). Similarly, the horizontal red line can be represented as the vector (0,1).
This can bring us to the first understanding of what a matrix represents. In terms of how it affects coordinate systems, a matrix is nothing more than a collection of these vectors to define which way is "up" and which way is "right". Therefore, a matrix that represents the red and blue lines may be written as:
Where the first row ( 1 0 ) is the horizontal line and the second row ( 0 1 ) is the vertical line. Therefore when we are told to move 3 untis along the horizontal line and 2 units along the vertical, we could represent this movement as:
which becomes:
which of course becomes:
This matrix happens to "align" with the unmodified world coordinate system. That is, if we follow this matrix to find a point such as (3,2), the point will be in the same position as if we just moved along the x and y axis, as we learned in elementary. In fact, this will be the case for any point we can think of. Whenever a matrix is aligned with the world coordinate system, it is called the "identity matrix". Another way to quickly determine if a matrix is an identity matrix is to note that it has 1's diagonally starting at the top right, and 0's everywhere else. Here is a 4X4 identity matrix: