Manipulating 3D Objects


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:

  • Translation You can think of translation as actually moving the object. It's essentially adding a new point in 3D space to every vertex in your model to move it to a new location. For example, if you had a single vertex at point (1,2,3) and translated it (-2,3,1), you would calculate the new vertex position by simply adding each of the axis coordinates. In this case, the new vertex would be located at (-1,5,4) after the translation. Doing this for every vertex in your object has the effect of moving the entire object

  • Scaling You use scaling to change the actual size of the object, by multiplying each coordinate of a vertex by a fixed amount. Assume that the size of the Loopy model earlier was (3,8,3) or 3 units wide, 8 units high, and 3 units deep. You have two options when scaling, either uniformly or nonuniformly: uniform scaling uses the same scaling factor for each axis, but nonuniform scaling uses differing scaling factors. Using our Loopy model, scaling uniformly by 5 (5,5,5) makes the new size (15,40,15); Loopy still looks the same, just much larger. However, scaling nonuniformly by something such as (3,0.5,3) causes Loopy to look squished, being "compressed" into a new size of (9,4,9).

  • Rotation You can rotate an object around any of the three axes you have defined in your coordinate system. Actually, by combining these different rotations on each of the axes, you can simulate rotating around any arbitrary axis in a 3D world. Think of rotating an object around an axis with the sheet of paper you stuck the pencil through. Holding on to the pencil, spin the paper around the pencil, and you are rotating it around the z axis. See Figure 10.5 for an illustration.

    Figure 10.5. Rotating around the z axis.


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) Objects

The formula here is quite simple based on the description I gave earlier (where T is the distance you want to move your object):

Scaling

The scaling formula is also simple based on the description (where S is the scaling factor you want to move your object):


Rotation

I 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:

  • X axis


  • Y axis


  • Z 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 Systems

As 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.



Beginning 3D Game Programming
Beginning 3D Game Programming
ISBN: 0672326612
EAN: 2147483647
Year: 2003
Pages: 191
Authors: Tom Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net