Transformation Matrices

In a robust game environment objects are constantly changing. They are moving around the screen (translation). They are moving closer and farther away giving the appearance of shrinking or growing (scaling). And they are changing their orientation (rotation). In this section, we will look at the transformations of translation and scaling and their interconnectedness through concatenation.
Most objects consist of a collection of polygons. Each polygon is described by its vertices. We can represent these vertices as vectors. So we can think of a transformation of an object as a transformation on the vertices or vectors that make up its polygons.

# Translation

To translate a point $P(x,y,z)$ and therefore its position vector $\vec{p}=<x,y,z>$ by $\Delta x$ in the x direction, $\Delta y$ in the y direction and $\Delta z$ in the z direction we have the option of using either a matrix addition or a matrix multiplication. For our later purposes of concatenation, the multiplication approach will be preferred.
Using matrix addition we have the following

(1)
\begin{align} \left [ \begin{array}{c} x\\ y\\ z\\ 1\\ \end{array} \right ] + \left [ \begin{array}{c} \Delta x\\ \Delta y\\ \Delta z\\ 1\\ \end{array} \right ] = \left [ \begin{array}{c} x+\Delta x\\ y+\Delta y\\ z + \Delta z\\ 1\\ \end{array} \right ] \end{align}

Using matrix multiplication we have the following. Notice that this cannot be accomplished without the homogeneous coordinate. This is why we coded $w$ into our vector class.

(2)
\begin{align} \left [ \begin{array}{cccc} 1 & 0 & 0 & \Delta x\\ 0 & 1 & 0 & \Delta y\\ 0 & 0 & 1 & \Delta z\\ 0 & 0 & 0 & 1\\ \end{array} \right ] \left [ \begin{array}{c} x\\ y\\ z\\ 1\\ \end{array} \right ] = \left [ \begin{array}{c} x+\Delta x\\ y+\Delta y\\ z + \Delta z\\ 1\\ \end{array} \right ] \end{align}

Regardless of whether we use the addition or multiplication approach, the result is the same. The advantage of using multiplication is that it becomes possible to combine multiple transformations into a single matrix multiplication.

Exercise
Move the point, $(-3,-8,5)$, 4 units in the positive x-direction, do not move at all in the y-direction, and move 2 units in the negative z-direction using first matrix addition and then matrix multiplication.

Exercise
Move the 2D object consisting of vertices , $(1,1,)$, $(4,1)$, $(4,6)$ , 5 units in the negative x-direction and 6 units in the negative y-direction using matrix multiplication. # Scaling

To scale a vector $<x,y,z>$ by a factor of $S_x$ in the x direction, a factor of $S_y$ in the y direction and a factor of $S_z$ in the z direction we use the following matrix multiplication.

(3)
\begin{align} \left [ \begin{array}{cccc} S_x & 0 & 0 & 0\\ 0 & S_y & 0 & 0\\ 0 & 0 & S_z & 0\\ 0 & 0 & 0 & 1\\ \end{array} \right ] \left [ \begin{array}{c} x\\ y\\ z\\ 1\\ \end{array} \right ] = \left [ \begin{array}{c} S_xx\\ S_yy\\ S_zz\\ 1\\ \end{array} \right ] \end{align}

For a simple 2D example say we had a triangle with vertices (1,1), (2,1) and (2,2) and we wanted to scale by a factor of 2 in the x direction and a factor of 3 in the y direction.

(4)
\begin{align} \left [ \begin{array}{ccc} 2 & 0 & 0\\ 0 & 3 & 0\\ 0 & 0 & 1\\ \end{array} \right ] \left [ \begin{array}{ccc} 1 & 2 & 2\\ 1 & 1 & 2\\ 1 & 1 & 1\\ \end{array} \right ] = \left [ \begin{array}{ccc} 2 & 4 & 4\\ 3 & 3 & 6\\ 1 & 1 & 1\\ \end{array} \right ] \end{align}

The image below shows the result of our scaling. Notice the possibly unintended consequence of moving the object as well as scaling it. This is because of the fact that the scaling is really stretching the points away from the axes. One way to ovoid this is to scale an object about its center.

Exercise
Scale the 2D object consisting of vertices , $(1,1,)$, $(4,1)$, $(4,6)$ , by a factor of 2 in the x-direction and a factor of $\frac{1}{2}$ in the y-direction using matrix multiplication. If we scale an object whose center is at the origin, the scaled object will still have its center at the origin. This avoids the unintended movement that we saw previously. To accomplish this, we must first translate the object so that its center is at the origin. Then we will scale the object. Then we will translate its center back to its original location. The following matrix multiplication shows the scaling of a vector by factors $S_x,S_y,S_z$ about the object's center $(x_c,y_c,z_c)$

(5)
\begin{align} \left [ \begin{array}{cccc} 1 & 0 & 0 & x_c\\ 0 & 1 & 0 & y_c\\ 0 & 0 & 1 & z_c\\ 0 & 0 & 0 & 1\\ \end{array} \right ] \left [ \begin{array}{cccc} S_x & 0 & 0 & 0\\ 0 & S_y & 0 & 0\\ 0 & 0 & S_z & 0\\ 0 & 0 & 0 & 1\\ \end{array} \right ] \left [ \begin{array}{cccc} 1 & 0 & 0 & -x_c\\ 0 & 1 & 0 & -y_c\\ 0 & 0 & 1 & -z_c\\ 0 & 0 & 0 & 1\\ \end{array} \right ] \left [ \begin{array}{c} x\\ y\\ z\\ 1\\ \end{array} \right ] \end{align}

# Concatenation

Since each of our transformations in 3D is a multiplication by a $4\times4$ matrix, we can multiply those matrices together to obtain a single $4\times 4$ matrix that will achieve multiple transformations at once. This new matrix is refered to as a concatenation. If we multiply the three matirces used for scaling about a center we obtain the following concatenation.

(6)
\begin{align} \left [ \begin{array}{cccc} 1 & 0 & 0 & x_c\\ 0 & 1 & 0 & y_c\\ 0 & 0 & 1 & z_c\\ 0 & 0 & 0 & 1\\ \end{array} \right ] \left [ \begin{array}{cccc} S_x & 0 & 0 & 0\\ 0 & S_y & 0 & 0\\ 0 & 0 & S_z & 0\\ 0 & 0 & 0 & 1\\ \end{array} \right ] \left [ \begin{array}{cccc} 1 & 0 & 0 & -x_c\\ 0 & 1 & 0 & -y_c\\ 0 & 0 & 1 & -z_c\\ 0 & 0 & 0 & 1\\ \end{array} \right ] = \left [ \begin{array}{cccc} S_x & 0 & 0 & x_c(1-S_x)\\ 0 & S_y & 0 & y_c(1-S_y)\\ 0 & 0 & S_z & z_c(1-S_z)\\ 0 & 0 & 0 & 1\\ \end{array} \right ] \end{align}

So given an object's center and the directional scaling factors, we simply multiply each vector of the object by the concatenation below to obtain a scaling about its center.

(7)
\begin{align} \left [ \begin{array}{cccc} S_x & 0 & 0 & x_c(1-S_x)\\ 0 & S_y & 0 & y_c(1-S_y)\\ 0 & 0 & S_z & z_c(1-S_z)\\ 0 & 0 & 0 & 1\\ \end{array} \right ] \end{align}

Exercise
Scale the 2D object consisting of vertices , $(1,1,)$, $(4,1)$, $(4,6)$ , by a factor of 2 in the x-direction and a factor of $\frac{1}{2}$ in the y-direction about its center using matrix multiplication. Exercise
Find the concatenation that would first double the size in the x and z-directions while reducing the size by 10% in the y-direction, and then move the object in the direction of $<10,-5,0>$

# Exercises

1) In which order are the following operations occurring on the object?

(8)
\begin{align} [\mbox{Scale}][\mbox{Translate}][\mbox{Object}] \end{align}

2) What is the side effect of raw scaling and why does it occur?
3) How can you avoid the side effect of raw scaling?
4) Give the matrix that using matrix multiplication would translate an object in the direction $<5,-3,9>$
5) Give the matrix that would raw scale an object by a factor of 4 in the x-direction, a factor of -2 in the y-direction and 0.5 in the z-direction
6) Below are the vertices of an object in the xy-plane.

(9)
\begin{eqnarray} (0,2,0,1)\\ (1,1,0,1)\\ (1,4,0,1)\\ (3,2,0,1)\\ (4,4,0,1) \end{eqnarray}

a) Come up with an estimate of its center
b) Give the concatenation that scales the object about its center by a factor of 10 in the x-direction and 0.25 in the y-direction.
c) Give the new vertices

7) Give the concatenation that would first translate an object in the direction $<1,-1,2>$ and then scale the object by a factor of 3 in the x-direction, 4 in the y-direction, and -1 in the z-direction.

8) Give the concatenation that would first scale an object by a factor of 3 in the x-direction, 4 in the y-direction, and -1 in the z-direction and then translate the object in the direction $<1,-1,2>$.

page revision: 55, last edited: 24 Jan 2018 20:56