Is it all coming back to you yet? I hope so, because here's where we get to the real inner workings of trigonometry where you can see how it's all going to come together. In this section we will cover the sine, cosine, and tangent functions, as well as projection. With knowledge of these operations under your belt, you will be able to understand the programming concepts you'll encounter in the following chapters (especially Chapter 6, "Collision Reactions").
Sine, Cosine, and Tangent
Sine, cosine, and tangent are known as trigonometric functions. Although what they mean is very simple, many people have trouble understanding them. This conceptual problem happens because it is easy to think that the trigonometric functions give a result by some esoteric or even mystical process. The truth is that these functions just use various ratios of the triangle side lengths to give results. Look at the triangle in the figure below. Notice that we are using x and y instead of a and b to label the side lengths. These are more common side names in programming. Notice the angle in the figure labeled angle.
All three of the trigonometric functions are defined by taking ratios of the sides of this triangle. A trigonometric function takes an angle (in Flash it must be measured in radians) and returns a value. For instance, the sine of 45° is .707. To test this for yourself in Flash, here is an ActionScript code snippet you can write:
angle=45; radians=angle*Math.PI/180; trace(Math.sin(radians));
Line 1 in the above code block sets the angle in degrees. Line 2 converts degrees to radians, and line 3 computes the sine and displays the result in the output window.
Table 3.1 lists these "big three" functions, their definitions, which methods of the Math object they correspond to in Flash, and a valid value range that can be returned from these functions.
It wouldn't hurt to commit some simple results of the trigonometric functions to memory. This can help tremendously when debugging a script. Table 3.2 shows some simple values for you to remember, should you choose to.
Since you are able to calculate the sine, cosine, and tangent of an angle, it makes sense that there would also be some way to go from a number back to an angle. There is a set of functions for this, called the inverse trigonometric functions: inverse sine, inverse cosine, and inverse tangent. Some people use the term arc (as in arcsine) rather than inverse. Table 3.3 contains a list of the available inverse trigonometric functions.
The inverse trigonometric functions take a number as an input parameter and return an angle in radians. To convince yourself of how this works, try this example in Flash:
Line 1 sets a variable called input with a value of .707. Line 2 uses the inverse sine method of the Math object (which returns an angle in radians) and then converts it to degrees. The result is traced in the Output window and should be very close to 45°. (It is not exactly 45° because the true sine of 45° has many more decimal places than .707.)
Imagine a diagonal line of length len drawn in Flash at angle ang. A piece of this line extends along the x-axis and another piece of it along the y-axis. If the angle were 0°, then the line would extend only along the x-axis. If the angle were 90° then the line would extend only along the y-axis. With any other angle, the line extends both in the x direction and the y direction. (Put another way, no two coordinates on the line have the same x or y value: A horizontal line always has the same y value for all of its coordinates. A vertical line always has the same x value for all of its coordinates. A diagonal line never repeats an x or y coordinate.) If you were to draw a right triangle from this diagonal line, then the two other sides of that triangle would be the pieces that extend along the x-axis and y-axis.
Finding the length of either (or both) of those pieces by using the values ang and len is called projection. These values are found by using the trigonometric functions that we've already discussed above.
As seen in the previous section:
In the example here, angle is replaced with ang and c with len. So:
To find the projection of len along the x-axis, we solve for x:
Or with ActionScript:
To find the y projection we use
And solve for y:
Which converts to this in ActionScript:
Think of projection like a shadow cast from an object onto the floor or a wall. For the example given in this section, first we would imagine a light source coming from below to cast a shadow on the x-axis. The length of the shadow cast from the line on the x-axis is the same as the projection we would calculate using trigonometry. Next we would imagine a light source coming from far off to the right shining left. The shadow cast on the y-axis is equal to that which we would calculate using trigonometry.