• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 11. Manipulating Information > Using Sine and Cosine for Directional Mo...

Using Sine and Cosine for Directional Movement

When you want to control how far an object on the Stage travels based on its angle, you can use the sine and cosine trigonometric functions. Suppose that you want to create a racing game featuring a car that your viewer moves around a track. The car travels at a certain speed, and it moves according to where the front of the car is pointed.

Calculating just how far the car moves in any direction requires the Math.sin() and Math.cos() methods of the Math object. The new location of the car is determined by the x and y components of the triangle that is formed by the angle of the car. When the car is angled up, the y component is 1, and the x component is 0. When the car is angled to the right, the y component is 0, and the x component is 1. When the car is angled somewhere between up and right, the sine and cosine of the angle give you the x and y contributions (Figure 11.15). The sine of the angle determines the magnitude of the y component, and the cosine of the angle determines the magnitude of the x component. Sine and cosine, however, are based on angles that begin at 0 degrees from the horizontal axis. The rotation property, on the other hand, is based on angles that begin at 0 degrees from the vertical axis (Figure 11.16). Moreover, the y component for the sine function is positive above the origin of the circle and negative below it. When you deal with a movie clip’s coordinate space, you must do two transformations: Subtract the rotation property from 90 to get the angle for sine and cosine, and then use the negative value of sine for the change in the y direction:


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint