Mouse Angle Detection II

Page 1 of 2
Kyle McDonald
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.
View all articles by Kyle McDonaldWritten by: Kyle McDonald
Website: www.kylemcdonald.com
Difficulty Level: Advanced Download FLA
INTRODUCTION
I am writing this tutorial because I feel that the current tutorial on actionscript.org which defines how to detect the angle of a mouse relative to an object on the screen is unnecesarily complex. Instead of going through sine tables, I think that it would be much better if you understand how to do two things in a simpler way:
1.) Detect the angle created by the mouse and a movie clip symbol.
2.) Rotate a movie clip symbol about its vertex based on the position of the mouse.
PART 1: Back to the basics: Trigenometry and Algebra
When you are detecting the angle of the mouse relative to an object, what you are really doing is finding the angle

If anybody remembers the pnumatic device "sohcahtoa", then you might remember it means:
sin(
cos(
tan(
You can see where "opposite", "adjacent", and "hypostenuse" are by looking at the next picture.

Also, just incase you are wondering, "opposite", "adjacent", and "hypostenuse" are lengths of the sides of the triangle. So, since everyone past about 8th grade knows, it takes a little bit more math to find the hypotenuse, and since we already have the length of the adjacent and opposite sides (the x and y of the mouse relative to the movie clip symbol, in this case the mouse) we can just go ahead and use algebra to manipulate this equation to solve for
tan(
Really, tan-1(), arc-tan(), and inverse-tan() are all ways of saying the same thing. Hopefully, this clears it up for you, bcause I know I was confused for a while. Flash uses the term "atan" as the function for arc-tan/inverse-tan/tan-1. So, now that we have our understanding of the concept and the equation (
PART 2: Stupid Flash Tricks
By now you probably have a good understanding of the trigenometry behind the script we will write, or you just skipped the previous section. Either way, the first thing we need to do is re-define our equation to be used. Like most programming languages, ActionScript computates it's trigenometry functions in Radians. For example, let us say that the mouse is at a 45 degree angle, that means that it is an equal x distance away as it is y distance ((any number / itself)=1). So, the equation we would use to get it's angle is Math.atan(1). Math.atan() is the function with which Flash will do arc-tangent lookups for you. So, to test this out we can make a simple movie with a dynamic text box with the variable name "text", and an actionscript on the frame that says:
_root.text=Math.atan(1);
stop();
_root.text=Math.atan(1)/(Math.PI/180);
stop();


