Introduction
Any 3D developer ought to understand the essential mathematics behind
transformations. Although the math looks complicated, it really isn't
too bad, once you get past the unfamiliar notation.
Points and Vectors
You have been using the term vector quite a bit. It is
used to describe a point in 2D or 3D space, and alternatively a line
segment between the origin and that point. The term also has a
mathematical definition. A vector in 3D geometry would consist of
three floats, usually arranged like this:
_ _ | | | x | | | | y | | | | z | |_ _|The nice thing about thinking about a point as a vector is there are certain mathematical properties associated with vectors. If you use the computer to apply these principles, you can generate any kind of transformation you wish.
_ _ _ _ _ _ | | | | | | | 0 | | 0 | | 0 | | | | | | | | 0 | + | 1 | = | 1 | | | | | | | | 0 | | 0 | | 0 | |_ _| |_ _| |_ _|In essence, translating an object amounts to adding a vector to each point in the object.
_ _ _ _ _ _ _ _ | | | | | | | | | 0 | | 0 | | 1 | | 2 | | | | | | | | | | 1 | * 2 = | 2 | | 0 | * 2 = | 0 | | | | | | | | | | 0 | | 0 | | 0 | | 0 | |_ _| |_ _| |_ _| |_ _|In fact, you can actually scale in X, Y, and Z axes independantly. Most 3D packages allow you to specify a vector for scaling which separately indicates how much you scale in each axis. Although it's not exactly done this way mathematically, you can think of multiplying two vectors together for a scale, so if you have a point (2, 2, 2) and you want to scale it by (3, 4, 5), you could (somewhat inaccurately) think of it like this...
_ _ _ _ _ _ | | | | | | | 2 | | 3 | | 6 | | | | | | | | 2 | * | 4 | = | 8 | | | | | | | | 2 | | 5 | | 10 | |_ _| |_ _| |_ _|Please note that this is NOT exactly how it's done. However, I have simplified the math to make the concept a little easier to understand. In reality, the scale is usually done with either a single value, one direction at a time, or in a 3 x 3 matrix. Since I haven't shown you matrices yet, I'm simplifying the math. See any linear algebra book for the complete truth.
x' = (x * cos(a)) + (y * -sin(a)) = (1 * 0) + (1 * -1) = 0 - 1 = -1 y' = (x * sin(a)) + (y * cos(a)) = (1 * 1) + (1 * 0) = 1 + 0 = 1 (x', y') = (-1,1)3D Rotations
x' = (x * cos(a)) + (y * -sin(a)) y' = (x * sin(a)) + (y * cos(a)) z' = z
x' = (x * cos(a)) + (z * sin(a)) y' = y z' = (x * -sin(a)) + (z * cos(a))... and rotation about the x axis uses a similar set of functions.
x' = x y' = (y * cos(a)) + (z * -sin(a)) z' = (y * sin(a)) + (z * cos(a))Don't get all hung up on memorizing these formulas. You can look them up when you need them. The more important thing is to understand the pattern. You'll see a technique for combining these formulas into a cleaner structure in a few minutes. For the time being, note that there is a consistent pattern emerging.
x' = (x * cos(a)) + (y * -sin(a)) y' = (x * sin(a)) + (y * cos(a)) z' = zHere is another way of viewing the same information:
| x * | y * | z * | |
|---|---|---|---|
| x' | cos(a) | -sin(a) | 0 |
| y' | sin(a) | cos(a) | 0 |
| z' | 0 | 0 | 1 |
x' = (x * cos(a)) + (y * -sin(a)) + (z * 0)All the data of all three functions can be encapsulated on this table.
_ _ | | | cos(a) -sin(a) 0 | | | | sin(a) cos(a) 0 | | | | 0 0 1 | |_ _|This particular matrix can be set at any angle of a. Given any value of A, this matrix can generate the formulas to calculate a rotation of a radians around the z axis. Similar matrices can be formulated for the other axes.
_ _ _ _ _ _ | x | | a b c | | xa + yb + zc | | | | | | | | y | * | d e f | = | xd + ye + zf | | | | | | | | z | | g h i | | xg + yh + zi | |_ _| |_ _| |_ _|You can also multiply a matrix by another matrix of the same size, resulting in a third matrix of the same size as the first two.
_ _
| Sx 0 0 |
| |
| 0 Sy 0 |
| |
| 0 0 Sz |
|_ _|
If you multiply this scale matrix by each of the rotation matrices,
you'll get a three-by-three matrix which encapsulates all the rotation
and scaling on each point in the shape.