Conic Sections and NURBS Curves

text only
 
progress indicator progress indicator progress indicator progress indicator

Some Simple Uses for Trigonometric Functions

The code for Chapter 1, "Polynomial Curves," featured some trigonometry that I promised I would explain in this chapter. The code illustrated the derivative of a curve by moving a straight line along the curve and tangent to the curve. Figure 2.6 is a screenshot from Chapter 1, only this time I have added lines to construct a right triangle like the one shown in Figure 2.1.


Figure 2.6: Adding a triangle to the screenshot from Chapter 1.

If you combine the derivative information from the last chapter with the trigonometric information from this chapter, you will get the following equation.

(2.4) Equating slope and tangent. 
Note 

The inverse of each trigonometric function begins with "arc". These functions are arcsine, arccosine , and arctangent. The results of these functions are given in radians.

From that, you can get the angle of the line by using arctangent as shown in the next equation.

(2.5) Finding the angle with arctangent. 

The angle itself isn't the end goal; you really want to find another point on the slope that defines that angle. Equation 2.5 gives you a value that you can feed back into the sine and cosine functions. These functions will give you the x and y values for another point on the line. Remember, the results of sine and cosine have a maximum value of 1.0, so you can scale the result by some value to make the line longer or shorter. Figure 2.7 shows graphically how all the steps come together to give you another point along the line that shows the slope. Once you have two points, you can draw a line. This technique will be used throughout the curve chapters to draw the slope at points along different curves.


Figure 2.7: Putting the pieces together.

The preceding example shows one of the many uses for these functions as they relate to angles. Sine waves can be extremely useful for many applications. Imagine writing a very simple shooting gallery game where a target moves back and forth on the screen. That motion could be driven by a sine function. You could alter the limits of motion by changing the amplitude. You could also change the speed of the target by changing the frequency.

The code for this chapter can be found in the \Code\Chapter02 directory on the CD. It is nearly identical to the code from Chapter 1, but I have added two functions that illustrate interesting wave applications. Use this code as a starting point for your own experimentation.

The first application shows a very simple way to generate interesting behaviors. If you are writing a hockey game, you don't want the players to always skate in straight lines. Instead, you might compute the basic path of a player and then use a sine wave to oscillate along that path .

The function for this is shown next and a screenshot is shown in Figure 2.8.


Figure 2.8: Oscillating along a line.
 #define FUNCTION_THETA(x) (x + 50.0f * sin(0.1f * (float)x)) #define DERIVATIVE_THETA(x) (1 + 5.0f * cos(0.1f * (float)x)) 

You could generate different behaviors for each player on the team by tweaking the parameters of the wave equation. You could also use small random changes to make the motion less predictable. Obviously, you can do much more to determine and adjust the behaviors of the players, but this is one starting point.

A different application of the same technique could be used to generate fake stock market data. You could use a high-amplitude, low-frequency wave to generate the general trend of the stock. You could then use smaller high-frequency waves to generate the daily trading activity. The following code shows a simple example of that.

 #define GENERAL_TREND(x)  (350.0f * sin(0.0035f * (float)x)) #define RANDOM            (float)rand() / (float)RAND_MAX #define DAILY (x) (40.0f * RANDOM * sin(0.035f * (float)x + 10.0f * RANDOM)) #define FUNCTION_THETA(x) GENERAL_TREND(x) + DAILY (x) 

Figure 2.9 shows a screenshot of this. The derivative functionality does not work for this sample because of the random factors.


Figure 2.9: Simple stock data simulation.

Again, you would probably want to do more to model real stock fluctuations, but this type of approach shows you how a periodic function could be used to very quickly generate something passable.

Whenever you are modeling something periodic, consider using a sine wave as your basic primitive. The fact that it is periodic and bounded by -1 and 1 make it convenient for many tasks . There are occasions where using standard API functions like sin() may be impractical , or in the case of DirectX vertex shaders, nonexistent. That's when you might want to compute sine yourself.

progress indicator progress indicator progress indicator progress indicator


Focus on Curves and Surfaces
Focus On Curves and Surfaces (Focus on Game Development)
ISBN: 159200007X
EAN: 2147483647
Year: 2003
Pages: 104
Authors: Kelly Dempski

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