Tricks of the Windows Game Programming Gurus:Vector Rasterization and 2D Transformations

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

#### Translation

Translation is nothing more than moving an object or point from one position to another. Let’s assume that you have a single point, (x,y), that you want to translate by some amount, (dx,dy). The process of translation is shown in Figure 8.17.

Figure 8.17 Translating a single point.

Basically, you add the translation factors to the (x,y) coordinate, come up with a new position, and call it (xt,yt). Here’s the math:

    xt = x + dx;

    yt = y + dy;

Here, dx,dy can be either positive or negative. If we’re taking about translation with standard display coordinates, where (0,0) is in the upper-left corner, positive translations in the x-axis move an object to the right. Positive translations in the y-axis move an object down. Negative translations in the x-axis move an object to the left. And finally, negative translations in the y-axis move an object up.

To translate an entire object, you simply apply the translation transformation to the center of the object, if the object has an x,y position that all points are relative to (as does the polygon structure). If the object doesn’t have a general x,y position, you must apply the formula to every point that makes up the polygon. This brings us to the concept of local and world coordinates.

Generally, in 2D/3D computer graphics you want to define all objects to have at least local and world coordinates. The local coordinates of an object are those relative to (0,0) or (0,0,0) in 3D. Then the world coordinates are found by adding the world position (x0,y0) to each of the local coordinates, in essence translating the local coordinates each by (x0,y0) to place the object. This is shown in Figure 8.18.

Figure 8.18 Translation of an object and the resulting vertices.

In light of this, in the future you may decide to add more data storage to your polygon so that you can store both local and world coordinates. In fact, you will do this later. Moreover, you’ll add storage for camera coordinates. The reasoning for the added storage is that once you transform an object to world coordinates and are ready to draw it, you don’t want to have to do it each frame. As long as the object doesn’t move or transform, you won’t have to. You can just save the last calculated world coordinates.

With that all in your little neural net, let’s take a look at a general polygon translation function. It’s so simple, it should be illegal <BG>.

    int Translate_Polygon2D(POLYGON2D_PTR poly, int dx, int dy)

    {

    // this function translates the center of a polygon

    // test for valid pointer

    if (!poly)

       return(0);

    // translate

    poly->x0+=dx;

    poly->y0+=dy;

    // return success

    return(1);

    } // end Translate_Polygon2D

I think that deserves a Pentium.II.AGP double snap!!!

#### Rotation

Rotation of bitmaps is rather complex, but rotation of single points in the plane is almost trivial—or at least the actual rotation is. Deriving the rotation is a bit more complex, but I have a new derivation that I think is very cool. However, before I show it to you, let’s take a look at what you want to do. Referring to Figure 8.19, you can see the point p0 with coordinates (x,y). You want to rotate this point an angle theta about the z-axis (which is running through the paper) to find the rotated coordinates p01 with coordinates (xr,yr).

Figure 8.19 The rotation of a point.

Trigonometry Review

Obviously, you’re going to need to use some trigonometry here. If you’re rusty, here’s a quick review of some basic facts.

Most trigonometry is based on the analysis of a right triangle, as shown in Figure 8.20.

Figure 8.20 The right triangle.

Table 8.1 shows the differences between radians and degrees.

Table 8.1 Radians Versus Degrees

Degrees | Radians (pi) | Radians (Numerically)
--- | --- | ---

360 | 2*pi | 6.28
180 | 1*pi | 3.14159
90 | pi/2 | 1.57
57.295 | pi/pi | 1.0
1 | pi/180 | 0.0175

Here are some trigonometric facts:

  • There are 360 degrees in a complete circle, or 2*pi radians. Hence, there are pi radians in 180 degrees. The computer functions `sin()` and `cos()` work in radians, not degrees—remember that! Table 8.1 lists the values.
  • The sum of the interior angles theta1 + theta2 + theta3 = 180 degrees or pi radians.
  • Referring to the right triangle in Figure 8.20, the side opposite theta1 is called the opposite side, the side below it is called the adjacent side, and the long side is called the hypotenuse.
  • The sum of the squares of the sides of a right triangle equal the square of the hypotenuse. This is called the Pythagorean theorem. Mathematically, you write it like this:

      hypotenuse2 = adjacent2 + opposite2

  Or sometimes you can use a, b, and c for dummy variables:

      c2 = a2 + b2

  Therefore, if you have two sides of a triangle, you can find the third.

  • There are three main trigonometric ratios that mathematicians like to use, called the sine, cosine, and tangent. They’re defined as

                       adjacent side      x

      cos(theta)  =  ---------------- = -------

                        hypotenuse        r

  Domain: 0 <= theta <= 2*pi

  Range: -1 to 1

                      opposite side       y

      sin(theta)    = ---------------- = -------

                       hypotenuse         r

  Domain: 0 <= theta <= 2*pi

  Range: -1 to 1

                      sin(theta)       opposite/hypotenuse

      tan(theta)    = --------------  = ----------------------

                      cos(theta)       adjacent/hypotenuse

                                  opposite       y

                              = ------------ = --- = slope = M

                                  adjacent       x

  Domain: -pi/2 <= theta <= pi/2

  Range: -infinity to +infinity

Math Note the use of the terms domain and range. These simply mean the input and the output, respectively.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.