You probably remember earlier when I briefly described transformations, or moving from one coordinate system to another, and how I would discuss it more in depth later. Well, it is later now, so it seems like the perfect time to get into it. There are three basic transformations you want to accomplish:
What kind of math chapter would this be if there weren't any nifty formulas for you to look at? Obviously, everything I just described could be written as a formula, so now you should look at the formulas for each of these items. Translating (Moving) ObjectsThe formula here is quite simple based on the description I gave earlier (where T is the distance you want to move your object): ScalingThe scaling formula is also simple based on the description (where S is the scaling factor you want to move your object): RotationI didn't really mention a formula for this concept earlier because rotation is more complex than the two preceding sets of formulas. Rotations always rotate around an axis, and the amount of rotation is specified as an angle. It's also interesting to note that the coordinate on the axis you are rotating with will never move; unlike earlier, when all three axis coordinates had the potential to change, during rotation, only two ever change, and the two that change depend on the axis of rotation (or theta, q). Look at the formulas for each axis:
Exactly why these formulas work is a matter for another book or a trigonometry class. (Writing up a proof would take a lot more than this chapter.) It is important that you understand what the formulas are, however, and I highly recommend that you do a few calculations rotating around various axes and determining the new coordinates after rotation so you can see how the objects are rotated. Coordinate SystemsAs mentioned earlier, the idea of transformation is to move between one coordinate system and another, but what exactly does that mean? The rotation formulas only work when the object is centered at the origin. For example, let's take Loopy: rather than center him at the origin, let's say his center is (30,20,10). Now, you want to rotate him on the z axis 90°, and I bet you're expecting him to rotate in place, but you will be vastly surprised. Let's simply use the earlier formula on the center point (which, if he were rotating in place, wouldn't change): Finishing up the equation, you see the following: That gives us a new coordinate of (-20,30,10), which I imagine you found completely surprising (I know I did the first time I tried it). To combat this, 3D graphics worlds normally have a multitude of coordinate systems, such as local and world. The local coordinate system is what you use for your models, such as Loopy. Look back at Figure 10.4, and imagine where the center point of that Loopy model appears. In the local coordinates, the head of Loopy would be slightly above the center, and the feet would be slightly below and to the left or right of the center. See Figure 10.6 for an example of how the local coordinates for Loopy might be envisioned. Figure 10.6. A local coordinate system.This point is important because now no matter what you do with Loopy in the local coordinate system, it behaves how you expect. It doesn't matter if his center in the world is over at (30,20,10) because when you do the rotations, you do so in the local coordinate system, which is centered on his body and never changes. |