3D Math Primer for Game Programmers (Matrices)


The Matrix

The Matrix

In this article, I will discuss matrices and operations on matrices. It is assumed that the reader has some experience with Linear Algebra, vectors, operations on vectors, and a basic understanding of matrices.

Conventions

Throughout this article, I will use a convention when referring to vectors, scalars, and matrices.

  1. Scalars are represented by lower-case italic characters ().
  2. Vectors are represented by lower-case bold characters ()
  3. Matrices are represented by upper-case bold characters ()

Matrices are considered to be column-major matrices and rotations are expressed using the right-handed coordinate system.

Linear Transformation

A linear transformation is defined as a transformation between two vector spaces and denoted and must preserve the operations of vector addition and scalar multiplication.  That is,

  1. for any vectors and
  2. for any scalar

This property also implies that any linear transformation will transform the zero vector into the zero vector.  Since a non-zero translation will transform the zero vector into a non-zero vector then any transformation that translates a vector is not a linear transformation. A few examples of linear transformations of three-dimensional space are:

Rotation

As stated in the article about coordinate systems (Coordinate Systems) considering a right-handed coordinate system, a positive rotation rotates a point in a counter-clockwise direction when looking at the positive axis of rotation.

Rotation about the X Axis

A linear transformation that rotates a vector space about the axis:

For example rotating the point 90 degrees about the axis would result in the point . Let’s confirm if this is true using this rotation.

And rotating the point 90 degrees about the axis would result in the point .

Rotation about the Y Axis

A linear transformation that rotates a vector space about the axis:

For example rotating the point 90 degrees about the axis would result in the point .

And rotating the point 90 degrees about the axis would result in the point .

Rotation about the Z Axis

A linear transformation that rotates a vector space about the axis:

For example rotating the point 90 degrees about the axis would result in the point .

And rotating the point 90 degrees about the axis would result in the point .

Rotation about an arbitrary axis

A 3D rotation about an arbitrary axis by an angle :

Scale

A linear transformation that scales a vector space:

A linear transformation that scales by a factor in an arbitrary direction :

Affine Transformation

An affine transformation is a linear transformation followed by a translation. Any linear transformation is an affine transformation with a translation of , but not all affine transformations are linear transformations. The set of affine transformations is a superset of linear transformations. Types of affine transformations are scale, shear, rotation, reflection, and translation. Any combination of affine transformation results in an affine transformation. Any transformation in the form is an affine transformation where is the transformed vector, is the original vector, is a linear transform matrix, and is a translation vector.

Determinant of a Matrix

For every square matrix, you can calculate a special scalar value called the “determinant” of the matrix.  If the determinant is not , then the matrix is invertible and we can use the determinant to calculate the inverse of that matrix.

The determinant of a matrix is denoted or .

Before we discuss how to calculate the determinant of a larger matrix, let’s first discuss the determinant of a matrix:

You can use the following diagram to help you remember the order in which the terms should be placed:

Matrix Determinant Diagonals

In this diagram, we see the arrows passing through the diagonal terms. We simply multiply the operands on the diagonal terms and we subtract the result of the back diagonal term from the result of the front diagonal term.

The determinant of a matrix is shown below:

We can use a similar diagram to memorize this equation. For this diagram, we duplicate the matrix and place it next to itself, then we multiply the forward-diagonal operands, and the backward-diagonal operands and we add the results of the forward-diagonals and subtract the results of the backward-diagonals:

Determinant of a 3x3 matrix

If we imagine the rows of the matrix as vectors, then the determinant of the matrix is equivalent to the triple product that was introduced in the article on vector operations. If you recall, this is the result of taking the dot product of a cross product. This is shown below:

We can calculate the determinant of a matrix by choosing an arbitrary row and applying the general formula:

Note that we can also arbitrarily choose a column that can be used to calculate the determinant, but for our general formula, we see the determinant applied to the row.

Where is the matrix obtained by removing the row and the column from matrix . This is called the minor of the matrix at the row and the column:

The cofactor of a matrix at the row and the column is denoted and is the signed determinant of the minor of the matrix at the row and the column:

Note that the term has the effect of negating every odd-sum cofactor:

Let’s take an example of using the general form of the equation to calculate the determinant of a matrix:

And expanding the determinants for the minors of the matrix:

As you can see, the equations for calculating the determinants of matrices grows exponentially as the dimension of our matrix increases.

Inverse of a Matrix

We can calculate the inverse of any square matrix as long as it has the property that it is invertible. We can say that a matrix is invertible if it’s determinant is not zero. A matrix that is invertible is also called “non-singular” and a matrix that does not have an inverse is also called “singular”:

The general equation for the inverse of a matrix is:

Where is the classical adjoint, also known as the adjugate of the matrix and is the determinant of the matrix defined in the previous section. The adjugate of a matrix is the transpose of the matrix of cofactors. The fact that the denominator of the ratio is the determinant of the matrix explains why only matrices whose determinant is not have an inverse.

For a matrix :

the adjugate is:

The adjugate of a matrix :


is:

As an example, let us take the matrix :

Then is defined as:

Once we have the adjugate of the matrix, we can compute the inverse of the matrix by dividing the adjugate by the determinant:

An interesting property of the inverse of a matrix is that a matrix multiplied by its inverse is the identity matrix:

Orthogonal Matrices

A square matrix is orthogonal if and only if the product of the matrix and its transpose is the identity matrix:

And since we know from the previous section that an invertible matrix multiplied by it’s inverse is the identity matrix , we can conclude that the transpose of an orthogonal matrix is equal to its inverse:

This is a very powerful property of orthogonal matrices because of the computational benefit that we can achieve if we know in advance that our matrix is orthogonal. Translation, rotation, and reflection are the only orthogonal transformations. A matrix that has a scale applied to it is no longer orthogonal. If we can guarantee the matrix is orthogonal, then we can take advantage of the fact that the transpose of the matrix is its inverse and save on the computational power needed to calculate the inverse the hard way.

How can we tell if an arbitrary matrix is orthogonal? Let us assume is a matrix, then by definition is orthogonal if and only if .

If we rewrite the matrix as a set of column vectors:

Then we can write our matrix multiply as a series of dot products:

From this, we can make some interpretations:

  • The dot product of a vector with itself is one if and only if the vector has a length of one, that is it is a unit vector.
  • The dot product of two vectors is zero if and only if they are perpendicular.

So for a matrix to be orthogonal, the following must be true:

  • Each column of the matrix must be a unit vector. This fact implies that there can be no scale applied to the matrix.
  • The columns of the matrix must be mutually perpendicular.

References

3D Math Primer for Graphics and Game Development

3D Math Primer for Graphics and Game Development

Fletcher Dunn and Ian Parberry (2002). 3D Math Primer for Graphics and Game Development. Wordware Publishing.

7 thoughts on “3D Math Primer for Game Programmers (Matrices)

  1. Nice article. A good detailed intro to 3D matrix math relevant to 3D graphics/games.
    However, can I suggest a correction: You’ve referred to “determinate” of a matrix where it should read “determinant”. I.e. det(M) refers to the “determinant” not “determinate” of a matrix.

        • “For example rotating the point 90 degrees about the axis would result in the point . Let’s confirm if this is true using this rotation.”

          Then after, you come up with the result 0,0,1 without further explanation on how..

          If I understood what you were talking about I wouldnt have to read this article..and when I dont understand it I cant understand it..see my point ?

          • mIKE the derivation of the statement is shown in the following part that states “Let’s confirm that this is true using this rotation…”

            This article was written for people with a basic understanding of trigonometry and linear algebra. If you don’t know how I transformed the vector to then you probably don’t understand the trigonometry (sine, cosine functions) and matrix multiplication which is shown there. You should study linear algebra before you read this article as linear algebra is the basis for linear and affine transformations in 3D.

  2. Nice article, but I’m a little confused by the formatting.

    You start by offering the following convention: “Vectors are represented by lower-case bold characters”

    Then you start to explain linear transformation by referring to vectors (v1 and v2) using non-bold characters.

    This makes an otherwise excellent article a little difficult for me to read. I easily get distracted by small things like that. Sorry!

    • Jaiz,

      You’re right about the consistency of the formatting. I started updating the article for consistency but I don’t have a lot of time right now to go through the whole thing. In the future, I will be more careful about formatting of variables in the equations and in the text.

Leave a Reply

Your email address will not be published. Required fields are marked *