Game Development Reference

In-Depth Information

equation using what are known as the
inverse
trig functions. The

names of these functions match their counterparts, but prefixed

with the word
arc
. In this case, we need to use
arctangent
to find

the value of each of these angles.

B

A

=

arctan

15/20

ð

Þ

90
°

A

B

=

arctan

20/15

ð

Þ

Adjacent
=
20

Based on these equations, angle
A
would be ~37

°

and
B
would

Figure 11.6
A triangle

where we know just two

of the sides, but no

angles and no

hypotenuse.

be ~53

°

. If you add these together with the right angle of 90

°

,you

can see that we indeed have a proper triangle of 180

.

For our final theoretical example, look back again to
Fig. 11.6
.

Suppose all you needed was the hypotenuse and you weren

°

t inter-

ested in the angles at all. You could do what we did previously,

using arctangent to get the values of the angles and then use those

angles with either sine or cosine to determine the hypotenuse.

However, as this is a multiple-step process, it is inefficient when

we have a much quicker way. In addition to the standard trig

functions, there is another equation to determine the third side of

a triangle when you know the other two, which is known as the

Pythagorean theorem. The theorem states that the hypotenuse of a

triangle, squared, is equal to the sum of the squares of the other

two sides. Let

'

s look at this as an equation, calling the two shorter

sides
a
and
b
and the hypotenuse
c
.

'

a
2

b
2

c
2

+

=

Finding any one side when you know the other two is just a

simple permutation of this equation as follows:

q

ð

q

ð

q

ð

c

=

a
2

+

b
2

,
b

=

c
2

−

a
2

,
a

=

c
2

−

b
2

Þ

Þ

Þ

For our purposes, we know sides
a
and
b
to be 15 and 20 (or 20

and 15; it doesn

t really matter). From these values, the hypotenuse

would therefore be equal to

'

(15
2
+20
2
), or 25.

Now that we have defined these functions and have seen how

to use them, let

√

s look at a couple of practical examples in Flash

and how to apply the functions there.

A fairly common use of the trig functions is finding the angle of

the mouse cursor relative to another point. This angle can then be

applied to the rotation of a DisplayObject to make the object

'

“

look

”

at the mouse. If you open the MousePointer.fla file, you

ll find just

such an example setup. It consists of a triangle MovieClip called

“

'

on the Stage. One of the corners of the triangle is colored

differently to differentiate the direction it is pointing. For simplicity,

the ActionScript to perform this math is on the timeline; if you

were using this code as part of something larger, it would make

sense to put it in a class. Let

pointer

”

'

s look at this code now.