This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org. PURPOSE:This is a look at trigonometry for practical use in flash actionscript. I am one of those people who could have cared less about trigonometry when it was taught to me in high school. Today I am kicking myself for not paying more attention in math class.
WHY: The reason for this refresher is for the purpose of writing/programming actionscript in flash that will fake 3D in a 2D space. This was written for my own notes, but I decided someone else out there might get some use out of it.
WARNING: The trigonometry is a review for any one with some common sense and the desire to learn. I do however expect the readers of this material to be knowledgeable about actionscript programming techniques when it comes to writing code for flash. With that said. Expect very little explanation about flash and the code itself. However anyone who considers themselves a flash actionscript guru should be fine. Time for a reality check yet? (You need math to understand trigonometry! You need Colin Moock's book on actionscript to understand scripting techniques in flash.) Last word, reader be ware I am not a writer and this was not written for the the purpose of regenerating these ideas in flash. Think of this material as just a tutorial on the concept of trigonometry and flash, an overview really.
DISCLAIMER: Ya, I know. This stuff is not new and for the most part I am just rehashing what I've read in books, examples and all. So don't go thinking I am some super smart guy. The reality is I just read a lot of books. And the people who wrote those books are just people who read a lot of books. And the people who wrote those books are, well, you get the idea. Credit will be giveing to those books at the bottom of this page.
| Normal Cartesian coordinate system: This is a regular old Cartesian coordinate system |
| Flash Cartesian coordinate system: Take careful note of the differences. Which is that the y axis is flipped. For future reference also consider that there is a z axis coming strait out of the center of this graph and heading strait for you eyes (more later). |
![]() | ![]() |
![]() | This is actually what the Cartesian coordinate system looks like in flash on the root timeline. It will always start in the upper left hand corner on the root timeline. Note that any object defined by the Cartesian coordinate system uses points whether it be a single point or a box with 4 separate coordinates for each side of the box. The top upper left hand corner in flash has the coordinates (0,0). Note: coordinates represent positions in terms of width along the x axis and height along the y axis. |
![]() | This is actually what the Cartesian coordinate system looks like in a movie clip in flash. For example lets say that the orange ball is in a movie clip called ball. The center of the orange ball would be at (0,0) in the movie clip if the ball graphic was center inside of the clip (demonstrated below). The exact center of this ball would be at (0,0). We could go on to define the balls height and width by measuring the distance the ball covers along the y and x axis. |
The maximum rotation around any Cartesian coordinate system is 360 degrees. In Flash if you are moving clock wise this will be a positive number in degrees until you reach 180 degrees. If your are moving counter clock wise this will be a negative number until you reach 180 degrees. Remember all rotation in flash starts from 0 degrees. Your options are either moving clock wise or counter clock wise. If you want to move counter then you need a negative number. If you want to move clock wise you need a positive number. I know this might be a stretch but if you think about 45 degrees is also the same as -315 degrees. It just all depends on which direction around the clock you are moving and your purpose for rotating around a circle.
Now using the _rotation property of a movie clip will return a value between 0 degrees and 180 degrees or if you are moving counter clock wise a value between 0 degrees and -180 degrees. Take a look at the swf. I know this may be a little confusing. Just remember that if you use the movie property _rotation that if you want to move counter clock wise use a negative number and if you want to move clock wise use a positive number.
When getting the angle value for a movie clip on the stage remember that you could get a negative number. This is shown by dragging the movie clip around the stage in the swf. below. The value returned and used by the _rotation property is in degrees. This gets complicated because flash math functions return values of an angle in radians (Which is an angle in terms of the constant pi or 3.14 ). Radians are good for math but not so good when you want to alter the _rotation property of a movie clip If you are not using the _rotation property then be prepared to get a handle on radians.
![]() |
Drag the orange circle with the mouse |
[as]onClipEvent (mouseDown) {
startDrag (this, true, 0, 0, 150, 150);
}
onClipEvent (mouseUp) {
stopDrag ();
}
onClipEvent (enterFrame) {
Radians = Math.atan2((this._y-_root.center._y), (this._x-_root.center._x));
this.degreetextbox = Math.round(Radians*180/Math.PI)+" degrees";
}[/as]
Use this equation when you want to change radians to degrees. Flash usually returns the value of an angle in radians when using a math function.
[as]Degrees=Math.round(your_angle*180/Math.PI) [/as]
Remember, your angle is in Radians before you convert it.
READ THIS: The swf below shows what the out put would be if you did not convert from radians to degrees.
[as]onClipEvent (mouseDown) {
startDrag (this, true, 0, 0, 150, 150);
}
onClipEvent (mouseUp) {
stopDrag ();
}
onClipEvent (enterFrame) {
radians = Math.atan2((this._y-_root.center._y), (this._x-_root.center._x));
this.radiantextbox = radians;
}[/as]
[as]c = square root (a*a + b*b)
[/as]
| Drag clip a with the mouse notice that both clip a and b give the distance from each other which is of course always the same. However it might come in handy to have two thinking movie clips that both now how far they are from each other. |
[as]onClipEvent (load) {
var y1 = 0;
var y2 = 0;
var x1 = 0;
var x2 = 0;
}
onClipEvent (mouseDown) {
startDrag (this, true, 0, 0, 150, 150);
}
onClipEvent (enterFrame) {
y1 = _root.aclip._y;
x1 = _root.aclip._x;
y2 = _root.bclip._y;
x2 = _root.bclip._x;
x_cord = x2-x1;
y_cord = y2-y1;
_root.aclip.distance_from_bclip = Math.round(Math.sqrt(x_cord*x_cord+y_cord*y_cord));
}
onClipEvent (mouseUp) {
stopDrag ();
}[/as]
[as]onClipEvent (load) {
var y1 = 0;
var y2 = 0;
var x1 = 0;
var x2 = 0;
}
onClipEvent (enterFrame) {
y1 = _root.aclip._y;
x1 = _root.aclip._x;
y2 = _root.bclip._y;
x2 = _root.bclip._x;
x_cord = x2-x1;
y_cord = y2-y1;
_root.bclip.distance_from_aclip = Math.round(Math.sqrt(x_cord*x_cord+y_cord*y_cord));
}[/as]
Before I go on, consider what purpose the flash math functions below (in dark gray box) would serve you as you work with angles, triangles, cosine, and sine. Also review the relationship of sine, cosine, and tangent in the image below.
THIS IS VERY IMPORTANT: Most likely you will know the lengths of the opposite and adjacent sides of the triangle because they represent the y and x coordinates of a point. See if you can make sense of this by studying the image below.
| ||
| Math.Atan2(y, x);........calculates the angle (in radians) from the x-axis to a point on the y-axis. |
IMPORTANT: Remember that radius is the distance from the center of a circle to the edge of the circle. And that cosine represents the horizontal (x) and sine the vertical (y) value of the Cartesian coordinate system
Your goal is to find what the x and y coordinate are for the center of the orange circle below.
Remember that you must first change you angle into radians because we are talking about a 45 degree angle, but flash sees it in radians not degrees. The following code is an example of actionscript that will convert the angle into radians and then by using the math functions cos( ) and sin ( ) return the (x, y) coordinates of the orange circle below.
[as]var degrees=45;
radians = degrees* (Math.PI/180);
xcoordinate = radius * cos(radians);
ycoordinate = radius * sin(radians);[/as]

If you look at the animation's below the first example is a ball rotating around the y axis. Really quick before we go on here is a visual to understand what I mean by rotate around the y axis. This image shows how an orange circle could rotate around any of the three different axis.
In the swf below I am faking the rotation around the y axis by using perspective to make the ball larger then smaller as it moves along the x axis. The ball of course is smaller the farther it gets from our view and big the closer it gets to our view. Check out the code and see how it differs from the pervious code. I would like to note that this is still not true perspective because I am only changing the scale of the circle. For true perspective I must also add perspective to the path the ball follows. I am not going to go into detail about this in this example but I will show you the difference by providing two examples. Consider that the first example does not show the ball crossing over to the -z axis. It only looks like it is going away from you down the +z axis. The gray ball however does rotate into the negative side of the z axis. See the difference? For a full explanation check out Flash 5 Studio from friends of Ed, chapter 15 - page 469.
The perspective problem is simple really. You are trying to get a 3 dimensional point in flash, but you must do that by taking what is called an ordered triplet and turn it in to an ordered pair. this is an ordered triplet: (x,y,z) this is an ordered pair: (x,y) What you want to do is drop one of the coordinates so you have an ordered pair and then let flash fake the third coordinate by using perspective. Meaning that flash will be changing the scale of an object while it is rotating around a particular axis according to a perspective variable. look at these examples and see if you can figure out what I mean. If you want to duplicate these examples in flash just drop the code you see in the example on a movie clip. Make sure you have some graphic on the timeline of that movie clip or it will be blank, I've used an orange circle. You will also need another movie clip name "center_cross_hairs". This clip has to be center in the middle of the stage in order for flash to see the origin of the rotation as the center of the swf (or stage). Don't forget to give name it. For a further explanation of the math below reference the book Flash 5 Studio from friends of Ed, chapter 15 rotation points around the z axis = setting the x and y coordinate of a point using sine and cosine. (dropped z)The action script for this movie does not use the distance equation because we are not using the z axis to find any coordinates.
1. ActionScript the Definitive Guide, Colin Moock 2. Super Samurai (http://www.supersamuraiflash.com/) 3. Flash Studio 5, Friends of ed (http://www.friendsofed.com/) 4. Flash Game Studio, Friends of ed (http://www.friendsofed.com/) 5. Visual QuickPro Guide Flash Advanced, Russel Chun Contact: cody@o-positive.com |