Foundation ActionScript. Animation. Making Things Move Authors: Peters K. Published year: 2005 Pages: 28-29/137

## Important Formulas in This Chapter

Look at this. You have a brand-new shiny toolbox, and already you have more than a half dozen tools to put in it. The full set of tools will also appear Chapter 19, but lets look at what youve added so far.

Calculate basic trigonometric functions:

```sine of angle = opposite / hypotenuse
cosine of angle = adjacent / hypotenuse
tangent of angle = opposite / adjacent
```

```radians = degrees * Math.PI / 180
degrees = radians * 180 / Math.PI
```

Rotate to the mouse (or any point):

```// substitute _xmouse, _ymouse with the x, y point to rotate to
var dx = _xmouse - movieclip._x;
var dy = _ymouse - movieclip._y;
movieclip._rotation = Math.atan2(dy, dx) * 180 / Math.PI;
```

Create waves:

```// assign value to _x, _y or other property of movie clip,
// use as drawing coordinates, etc.
// note: angle does not have to be zero,
// but must be defined as something prior to adding speed
angle = 0;
onEnterFrame = function(){
value = center + Math.sin(angle) * range;
angle += speed;
}
```

Create circles:

```// assign position to _x and _y of movie clip,
// use as drawing coordinates, etc.
onEnterFrame = function(){
xposition = centerX + Math.cos(angle) * radius;
yposition = centerY + Math.sin(angle) * radius;
angle += speed;
}
```

Create ovals:

```// assign position to _x and _y of movie clip,
// use as drawing coordinates, etc.
onEnterFrame = function(){
xposition = centerX + Math.cos(angle) * radiusX;
yposition = centerY + Math.sin(angle) * radiusY;
angle += speed;
}
```

Get the distance between two points:

```// points are x1,y1 and x2,y2
// can be movie clip positions, mouse coordinates, etc.
dx = x2  x1;
dy = y2  y1;
dist = Math.sqrt(dx*dx + dy*dy);
```

Of course, if I just wanted to publish a list of formulas, I could have saved myself a lot of time and done just that. So look these over and make sure you fully understand how each one works. If you have any questions, go back to the point in the chapter where it was introduced, experiment with it, and research it more if you need to, until you can really think with the concept.

## Summary

This chapter covered nearly all the trigonometry you will need for animating in ActionScript. There is one principle, called The Law of Cosines, that I left out for now, as it is a lot more complex and deals with triangles that are not right triangles (they have no angle measuring 90 degrees). If you are now addicted to trig and just cant get enough, you can jump ahead to Chapter 14, which covers inverse kinematics, where trig really comes in handy.

But, for now, you know about sine, cosine, and tangent and their opposites: arcsine, arccosine , and arctangent, as well as the ActionScript methods to calculate each one.

Best of all, you got some hands-on experience using most of them in ActionScript, with some of the most common real-life uses of them. As you move through the book, youll see many more ways in which these techniques become useful. But you now have a solid footing with the concepts, and when you come across those examples, you should have no problem understanding them or how they work.

The next chapter covers some of the more common rendering techniques for getting graphics on the screen, including the all-important drawing API. As you go through that chapter, see if you can find ways to use the rendering methods to visualize some of the trig functions youve learned here. Im sure youll have no trouble creating some beautiful pictures or animations with trigonometry.

 Foundation ActionScript. Animation. Making Things Move Authors: Peters K. Published year: 2005 Pages: 28-29/137