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

Figure 8.21 shows graphs of all the functions. Notice that all the functions are periodic (repeating) and that `sin(theta)` and `cos(theta)` have periodicity of 2*pi, while the tangent has periodicity of pi. Also, notice that `tan(theta)` goes to +-infinity whenever theta mod pi is pi/2.

Figure 8.21 Graphs of basic trigonometric functions.

Now, there are about a bazillion trigonometric identities and tricks, and it would take a math book to prove them all. I’m just going to give you a table of the ones that a game programmer should know. Table 8.2 lists some other trigonometric ratios, as well as some neat identities.

Table 8.2 Useful Trigonometric Identities

Cosecant | `csc(theta) = 1/sin(theta)`
Secant | `sec(theta) = 1/cos(theta)`
Cotangent | `cot(theta) = 1/tan(theta)`

Pythagorean theorem in terms of trig functions:

    sin(theta)2 + cos(theta)2 = 1

Conversion identity:

    sin(theta1) = cos(theta1 – PI/2)

Reflection identities:

    sin(-theta) = -sin(theta)

    cos(-theta) =  cos(theta)

Addition identities:

    sin(theta1 + theta2) = sin(theta1)*cos(theta2) + cos(theta1)*sin(theta2)

    cos(theta1 + theta2) = cos(theta1)*cos(theta2) - sin(theta1)*sin(theta2)

    sin(theta1 - theta2) = sin(theta1)*cos(theta2) - cos(theta1)*sin(theta2)

    cos(theta1 - theta2) = cos(theta1)*cos(theta2) + sin(theta1)*sin(theta2)

Of course, you could derive identities until you turned many shades of blue. In general, identities help you simplify complex trigonometric formulas so you don’t have to do the math. Hence, when you come up with an algorithm based on sin, cos, tan, and so on, always take a look in a trigonometry book and see if you can simplify your math so that fewer computations are needed to get the result. Remember, speed, speed, speed!!!

Rotating a Point in a 2D Plane

Now that you have an idea of what sin, cos, and tan are, let’s use them to rotate a point in a 2D plane. Take a look at Figure 8.22, which shows the setup for the rotation formulas.

Figure 8.22 Derivation of the rotation equations.

Start off by showing that any point on a circle of radius R is computed as

    xr = r*cos(theta)

    yr = r*sin(theta)

Hence, if you always wanted to rotate a point that had coordinates (x,0), you could use this equation. However, you need to generalize a little. You want to rotate a point (x,y) about an angle theta, as shown in Figure 8.22. You can think of this in two ways: as rotating the point P, or as rotating the axes themselves. If you think of it as rotating the axes themselves, you have two coordinate systems: one before the rotation, and one after.

In the coordinate system before the rotation, you have

    xr = r*cos(theta)

    yr = r*sin(theta)

But after the rotation, you have

Equations 1:

    xr = r*cos(theta + alpha)

    yr = r*sin(theta + alpha)

Equations 2:

    x = r*cos(alpha)

    y = r*sin(alpha)

Here, `alpha` is basically the angle created from the x-axis of the new system and the position vector from the origin to P.

If you’re confused, let me explain what you’re doing here in another way. You always know how to find the point (x,0) rotated about theta, and if you rotate the axes by theta, you can compute P in both the old axes and the new. Then, based on these two formulas, you can come up with the rotation equations. If you take Equations 1 and use the addition identities, you’ll get the following results.

Equations 3:

    xr = r*cos(theta)*sin(alpha) – r*sin(theta)*sin(alpha)

    yr = r*sin(theta)*cos(alpha) + r*sin(theta)*cos(alpha)

Wait a minute, let me put some boom in it. You know that x,y are also equal to

    x = r*cos(alpha)

    y = r*sin(alpha)

Substituting these values into the bolded parts of Equations 3 gives you the results you desire.

Math For you math-heads, this derivation is very similar to a polar-only rotation with conversion to Cartesian coordinates at the end. That’s how I came up with it.

Equations 4, the rotation formulas:

    xr = x*cos(theta) – y*sin(theta)

    yr = x*sin(theta) + y*cos(theta)

    Q.E.D.

Back to reality. You now know that to rotate a point (x,y) by an angle theta, you can use Equations 4. However, there is one detail to remember: The equations rotate a point in the counterclockwise direction for positive theta and in the clockwise direction for negative theta. However, there is one more problem… you did the derivation for quadrant I of the normal Cartesian coordinate system. Thus, the y-axis is inverted on the display screen and the roles of positive and negative are reversed.

Later, when you do 3D graphics, you’ll transform all screen coordinates so that the x,y-axes are centered in the middle and are both pointing in the positive directions, just like quadrant I of the 2D Cartesian system. But for now, who cares?

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.