AndyMoore
AndyMoore

Reputation: 1444

How can I make a symbol point at the mouse?

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);
}

Upvotes: 1

Views: 84

Answers (2)

HITMAN
HITMAN

Reputation: 548

 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.

Upvotes: 0

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

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).

Upvotes: 3

Related Questions