Table of Contents### Using Sine and Cosine for Directional Movement

##### Figure 11.15. The x and y components of this car, which moves a certain distance, is determined by the cosine and sine of its angle, theta.

Entire Site

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 to help you do the calculations. For example, say that you want to create a racing game that features 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. If the car is pointed up, it'll move forward. If the car is pointed to the right, it'll move to the right, and if the car is pointed at 75 degrees, it'll move in that direction. 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 both 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 in the middle, 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. However, sine and cosine 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. Hence, you need to use cosine for the y component and sine for the x component (Figure 11.16).