Translation

Translation is a transformation that moves a point, like so.

images/transformations/translation.png

It changes the coordinates of the point by adding to or subtracting from them. For example, if the point had an x coordinate of 3, and you moved it 4 units in x, it would wind up with an x coordinate of 7.

Joe asks:
Joe asks:
Can’t we just use vectors to translate points?

Well, yes, as a matter of fact, we can. You saw in Chapter 1, Tuples, Points, and Vectors how to add a vector to a point and thus translate the point in the direction of the vector. This works well.

The problem with it is that it can only do translation—we can’t use the same operation (that is, adding a vector) and get rotation, or scaling, or shearing. What we want is a single operation that can produce any of these transformations and concatenate them in arbitrary order.

Matrix multiplication happens to be just such a tool.

The workhorse here will be a new translation(x,y,z) function which should return a 4x4 translation matrix. Implement the following test to show it in action. Don’t worry about making these next few tests pass yet, though; I’ll show you the secret sauce shortly.

 Scenario​: Multiplying by a translation matrix
 Given​ transform ← translation(5, -3, 2)
 And​ p ← point(-3, 4, 5)
 Then​ transform * p = point(2, 1, 7)

Further, if you take the inverse of a translation matrix, you get another translation matrix that moves points in reverse. Add the following test to your suite to demonstrate this.

 Scenario​: Multiplying by the inverse of a translation matrix
 Given​ transform ← translation(5, -3, 2)
 And​ inv ← inverse(transform)
 And​ p ← point(-3, 4, 5)
 Then​ inv * p = point(-8, 7, 3)

Now let’s throw a wrench into things: multiplying a translation matrix by a vector should not change the vector! Remember, a vector is just an arrow. Moving it around in space does not change the direction it points. Add the following test to show that vectors are not changed by translation:

 Scenario​: Translation does not affect vectors
 Given​ transform ← translation(5, -3, 2)
 And​ v ← vector(-3, 4, 5)
 Then​ transform * v = v

You might wonder how you’re going to pull that off. A matrix that affects points but not vectors? Can it really be so?

Gather round!

In Chapter 1, Tuples, Points, and Vectors, you read that the difference between a point and a vector was just that a vector had a 0 in its w component. This is where that feature pays dividends. It turns out that the way a translation matrix is constructed makes it so that a 0 in the w component of a tuple will cause the translation to be ignored.

Let’s look at this mysterious (spoiler: not really mysterious) translation matrix and see just how it is structured. Start with an identity matrix t, and then add the desired x, y, and z values to (respectively) the t03, t13, and t23 elements, as shown in the following figure.

images/jbmath/xforms.translation.png

You will find that, when multiplied by a vector, the 0 in w causes those translation values to disappear, like magic. With a point, though, the 1 in w has the desired effect, and causes the point to move.

Slick!

So that’s translation. Make those tests pass, and we’ll look at scaling next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.76.0