Table of Contents### Calculating Angles with the Math Object

Entire Site

The angle that an object makes relative to the Stage or to another object is useful information for many game interactions, as well as for when you want to create dynamic animations and interfaces based purely in ActionScript. For example, to create a dial that controls the sound volume, you need to compute the angle at which your viewer drags the dial relative to the horizontal or vertical axis, and change the dial's rotation and sound's volume accordingly. Calculating the angle also requires that you brush up on some of your high school trigonometry, so a review of some basic principles related to sine, cosine, and tangent is in order.

The mnemonic device SOH CAH TOA can help you keep the trigonometric functions straight. These are acronyms that stand for Sine = Opposite over Hypotenuse, Cosine = Adjacent over Hypotenuse, and Tangent = Opposite over Adjacent (Figure 11.1). Knowing the length of any two sides of a right triangle is enough information for you to calculate the other two angles. You will most likely know the lengths of the opposite and adjacent sides of the triangle because they represent the y and x coordinates of a point (Figure 11.2). When you have the x and y coordinates, you can calculate the angle (theta) using the following mathematical formulas: