In the last installment of the math primer series, we looked at the basics of matrices. Today, we’ll take a look at using matrices for linear transformations, which are one of the most common uses of matrices in games. But, before we dive into linear transformations, let’s take a quick look at what a *transformation* is, and why we represent them using a matrix in the first place.

A transformation is any operation that maps values from one space to another. In the context of standard algebra, we are usually introduced to transformations in the form of *functions*. Functions and transformations are really one and the same, though the term transformation is more common in linear algebra. In fact, sometimes you may even see transformations or functions referred to as *mappings*. Again, for our purposes these are all equivalent ways of conveying the same meaning. We take in a set of values, called the domain, and we produce another set of values, called the range.

When discussing transformations of affine and vector spaces, it is quite convenient to represent the transformations in the form of a matrix. This allows for succinct notation, as well as easily combining transformations through matrix concatenation (multiplication). For example, take the following transformation:

Here, we say that the matrix ** M **is multiplied with the vector

**, and is represented by the transformation**

*x**T*. We also know, from our discussion about matrices and vectors, that if we are using column vectors and column-major matrices, then the result of this transformation will be another vector. If we’re using row vectors and row-major matrices, then our result would be a matrix. If we want to use row vectors and row matrices, and still get a vector result, we would write the transformation as:

A *linear* transformation is a transformation that maps from vector space to vector space, maintaining the vector addition and scalar multiplication properties of the vectors. The first part of the definition, mapping from vector space to vector space, means that our transformation matrix must be exactly *n* x *n* in size, where *n* is the dimension of the vector space. The number of columns must be n in order to be compatible for multiplication with the vector. The number of rows must be n to give us an n-dimensional vector as a result. For 2 dimensional space, that means a 2×2 matrix. For three dimensional space, that means a 3×3 matrix.

The second part of the definition means that the addition of two vectors, and scalar multiplication of a vector, must be maintained after the transformation. Put in equation form:

The first equation says that the transformation of the sum of two vectors is equal to the sum of the transformation of the original vectors. This is what we mean when we say that the vector addition property of the vectors is maintained. The second equation says that the transformation of a scaled vector is equal to scaling the transformation of the original vector. This is what’s meant by maintaining the scalar multiplication property of vectors. Because of this, an easy way to determine if a transformation is linear is to combine the above equations and try and prove that they hold true for a particular transformation:

If you can prove that the above does not hold true for a given transformation, then that transformation is not linear.

Next, we’ll look at two of the most common linear transformations encountered in games: scaling and rotation. In addition to describing each transformation, I’ll show that they’re linear by using the equations above, and also use each one with some real numbers plugged in as an example. Hopefully, this will make them easier to understand.

#### Scale

The first linear transformation we’ll look at is scaling. Three dimensional scaling can be represented with the following matrix:

Where s_{i }represents the amount of scale along the ** i** vector, s

_{j}along the

**vector, and s**

*j*_{k}along the

**vector. Solving the linear transformation equation from before gives us:**

*k*Which is what we’d expect. Now, let’s show that this is linear by plugging in the two sides of the equation for showing linearity from above and seeing that the results are equal in both cases:

We can see that in either case, we ended up with the same answer. This shows that the scaling transformation is indeed a linear one.

Now, let’s look at an example you may encounter in a game. For this example, let’s say that we are trying to scale up a triangle with vertices (-1, 0, 0), (0, 1, 0), and (1, 0, 0) by a factor of 3 in all directions. I’ve intentionally used simple numbers to make this easier to visualize and show in a blog post. The same concept applies regardless of the numbers. This gives us a matrix with 3 all along the diagonal:

To scale our triangle, we would need to multiply each vertex by the scale matrix:

This gives us the 3 scaled vertices we expect: (-3, 0, 0), (0, 3, 0), and (3, 0, 0).

#### Rotation

The other linear transformation we’ll look at is rotation. Rotation consists of an *axis of rotation*, and the *angle of rotation*. When looking directly into the axis of rotation (the axis is pointing towards you), the rotation angle is measured *counterclockwise*. We’ll start by examining the matrix representations of rotations about the axes of the standard basis (** i**,

**, and**

*j***). Then, we’ll show linearity and solve an example.**

*k*Let’s start by looking at the matrix for rotation about the ** i, j, **and

**vectors (axes). These are sometimes referred to as**

*k**pitch, yaw,*and

*roll*respectively

*.*

We can then combine these through concatenation to form a general rotation matrix representing the combined rotations. However, we’ve seen that concatenation is order dependent, so we must be careful which order we combine them in. Combining pitch, then yaw, then roll may give a different result from combining as yaw, then pitch, then roll. There’s not really any standard for the order in which they’re combined, so be sure to stay consistent.

So, are rotations linear transformations? I’ve said that they are, but let’s show that this is the case. We’ll show this for one of the rotations above, but the same thing can be done for each of them:

As we can see, both ways lead to the same result, which confirms that rotations are in fact linear transformations.

Finally, let’s consider an example with real numbers. Let’s say we have a vector** u** = (3, 4, 5). We want to rotate this vector by 45^{o} counterclockwise about the ** i** vector, then 30

^{o}counterclockwise about the

**vector:**

*j*And that gives us the new vector after the two rotations: (5.7795, –0.707, 2.3285).

To wrap up, we’ve discussed transformations in general and looked at a special class of transformations known as linear transformations. We’ve looked at the two most common linear transformations in video games, scale and rotation. In the next installment of the math primer series, we’ll discuss affine transformations. As always, please let me know if there’s anything that I can help to explain better.

I can not thank you enough you are a life saver I do this as a hobby and your posts/lessons have been a blessing thank you so much!!!!

I'm glad they are helping you and that you're enjoying them!

Hey Reza, hope you still plan to post here. Don't for a moment think that we are not waiting for new articles. Great job so far!! Thanks.

Things have been pretty busy here, but I definitely plan on posting more! I'm glad you're enjoying the series so far. I hope to have the math primer finished soon, and continue with the physics articles. I may also start adding some graphics and GPU articles in the near future.

Let me know if there are particular topics you are interested in, and I'll try to cover them if I can.

Thanks for the articles, I'm enjoying the series.

There looks to be a problem in the math with the rotation matrices. The length of the resulting vector doesn't match the length of the starting vector. 3^2 + 4^2 + 5^2 != 5.7795^2 + -0.707^2 + 2.3285^2.

Thanks for pointing that out, I'll check the numbers when I get a chance and update the post.

Glad you're enjoying the series! I'm hoping to finally have some time to put up some more content soon.