1

I want to make a symbol rotate to point at the mouse. I'm using this function, but it doesn't work below the symbol's pivot. The inverse tan function has a range of 180 degrees right? So how can i get 360 degrees of movement?

Would I need to add an if statement to check the mouse position or is there a more elegant solution?

function panelTrack(){
    angle = -180/Math.PI * Math.atan((mouseX - panel.x)/(mouseY - panel.y));
    panel.rotation = angle;
    trace(panel.rotation);
}
null
  • 5,207
  • 1
  • 19
  • 35
AndyMoore
  • 1,324
  • 2
  • 11
  • 18

2 Answers2

3

Math isn't my strong point - so perhaps someone else will provide a better answer, but to get all 4 quadrants, you need to use atan2.

angle = Math.atan2(mouseY - panel.y, mouseX - panel.x) * 180 / Math.PI;

I seem to recall it has to do with a check for a value of 0 (that Math.atan doesn't do).

BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
0
 const radiance:Number=180/Math.PI;    

angle=-(Math.atan2(mouseX-panel.x, mouseY-panel.y))*radiance;

I used minus because usually the orientation is reverse when you don't add minus.

hope this helps.

HITMAN
  • 538
  • 1
  • 4
  • 13
  • Ah nice didn't notice the tick before. Pretty new to using stack overflow - kept coming across answers so made an account – AndyMoore Jun 04 '15 at 14:13
  • thank you.and a vote from me. your question is important – HITMAN Jun 04 '15 at 14:17
  • Please explain how your answers differs from mine. Proper way to use atan2 is by passing the `y` value first. Then there is no need to negate the result and you're following the documentation properly. – BadFeelingAboutThis Jun 08 '15 at 20:10
  • Ask it from @AndyMoore! and why you don't test your code and mine to see what's happening? and Math is exactly my strong point ! – HITMAN Jun 09 '15 at 04:46
  • @HITMAN - I did test both before I commented. Worked just fine. Only difference is the starting orientation of the object to be rotated (which isn't specified). If you had an arrow for instance, pointing left, my code will work the best. If that arrow was pointing up, your example would work best. Either way, there is no real difference except that mine follows the documentation. If Math is your strong point, then make your answer more useful by explaining what you are doing and why, then it would be deserving of a separate answer and upvote. – BadFeelingAboutThis Jun 09 '15 at 15:57
  • but he didn't ask me to explain what is going here.just wanted the answer. but I can explain it for you if you want. – HITMAN Jun 09 '15 at 16:01
  • Useful answers explain things, whether explicitly asked for or not. All I'm saying, is if you're going to post an answer that's essentially the same as an existing answer, explain how it is different. Answers that just post code are generally frowned upon in this community. – BadFeelingAboutThis Jun 09 '15 at 16:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80091/discussion-between-hitman-and-batman). – HITMAN Jun 09 '15 at 16:06