Detecting the Mouse Angle
This article has been added to your 'Articles to Read' list.

Page 2 of 2
FlashJunkie
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 FlashJunkieSince the 0,0 coordinate in this demonstration never moves, all we need to know to do some Trigonometry is the length of two sides of the triangle. Since we do know two sides (the x and y value of the mouse are distances... and are the two sides of the triangle) we can calculate the third side of the triangle (the hypotenuse) using the formula (a^2 + b^2 = c^2). More on that:
//Comment: ----------------------------------------------
//Comment: determine hypotenuese length
//Comment: ----------------------------------------------
//Comment: a squared plus b squared = c squared... (c=hypotenuse) so what is c?
Set Variable: "input" = (mouse_x * mouse_x) + (mouse_y * mouse_y)
Call ("sqrt")
Set Variable: "hyp" = output
For now, let's just say you set a variable "input" call "sqrt" and it sends back a variable "output", which is the square root of the number you sent.
Now we know the length of the hypotenuse. 'Hyp'. Since the x position of the user's mouse is "A" and the y position of the user's mouse is "O" we now have more than enough information to determine the angle "Theta".
//Comment: ----------------------------------------------
//Comment: determine sin(theta)
//Comment: ----------------------------------------------
//Comment: now we know "c" (the hypotenuse) how do we find the angle? Sine(Theta) is Opposite over
Hypotenuse, then lookup the Sine value in the lookup table.
Set Variable: "sin_theta" = mouse_y/hyp
If (sin_theta<0)
Set Variable: "sin_theta" = sin_theta+(sin_theta*-2)
End If
How To Find The Mouse Location
//Comment: ----------------------------------------------
//Comment: lookup angle in table
//Comment: ----------------------------------------------
Set Variable: "counter" = 0
Set Variable: "found" = 0
Loop While (not(found))
If (sin_theta>=Substring (..:sine_lookup_table,(counter*6)+1,5))
Comment: this counter will only increase if sin(Theta) is greater than or
exactly the angle match in the lookup table. This way, if the sine value has no exact match then the counter will point to the angle closest (less than) the correct answer.
Set Variable: "angle" = counter+1
Else
Set Variable: "found" = 1
End If
Set Variable: "counter" = counter+1
End Loop
What does this loop do? It starts a counter at 0. It looks at the first sine value in the table. If that sine value is less than the calculated vale for our angle, then nothing happens. we increase "angle" by one because it obviously isn't this low. This goes on until our calculated value for our angle becomes LESS than the lookup value in the table. Than means the actual angle we are at is less than the one the counter is at. In that case, we don't increase "angle", we set a variable "found" to "1" (another way of saying "found is true") and now the Loop While (not(found)) statement is false. Loops continue endlessly until the condition in the brackets is true. Since found starts out as "0" (false), not(found) can also be read as "not false". In other words, not(false)=true. So the loop reads (Loop while true). As soon as it the condition changes to "false" it quits the loop, stopping any further calculations because we've found the angle we are looking for.
This is just a fancy way to make the loop read more English-like.
Comment: ----------------------------------------------
Comment: add back full circle values
Comment: I also change the "0" point on the compass to the right hand side, with angle increasing counter clockwise.
Comment: ----------------------------------------------
If (mouse_x>=0 and mouse_y<0)
Set Variable: "angle" = angle
End If
If (mouse_x<0 and mouse_y<0)
Set Variable: "angle" = (90-angle)+90
End If
If (mouse_x<0 and mouse_y>=0)
Set Variable: "angle" = angle+180
End If
If (mouse_x>=0 and mouse_y>=0)
Set Variable: "angle" = (90-angle)+270
End If
Why do we need this? Hey. There is probably a more efficient way to make this whole code work. I'm certainly no programmer. But remember that the angle we calculated is between 0 and 90 degrees. Well, a full circle has 360 degrees. So how do we tell when to add 90, 180, or 270 degrees? Easy. Which quadrant was the mouse in? If the x and y values were both positive, then it was in the bottom right hand corner. (Flash coordinates start in the top left corner and increase down to the right.) So why is there a 90-angle phrase in the math there? I don't know. It's probably obvious to a mathematician. I just found that the numbers worked perfectly only by adding that in there.
Closing Thoughts
Download the ".FLA" and remove those bits to see what I mean.
//Comment: ----------------------------------------------
//Comment: redraw pointer line
//Comment: ----------------------------------------------
Set Property ("../line", Rotation) = -angle+90
Okay, so except for the bad programming, and the flawed logic (for some reason "0", "90", "180", and "270" don't show up ever in the calculations) it works! And this is the first trigonometry I've done in YEARS.
Spread The Word
Related Articles
1 Response to "Detecting the Mouse Angle" 
|
said this on 19 May 2007 7:53:11 PM CST
Another approach would be
Howe Meth nroot = Math.sqrt& Method 2: This is base function sqrt(nFi // Comme nguess = nFin // Comment: nroot = nguess +  nac // the loop can while (Math nroot naccuracytest return nroot; } // // This // For naccuracytest = |


Author/Admin)