I’ve a movieclip representing an arrow (with registration point in its middle). When I click a button, the arrow must point to a certain movieclip on stage. I use this code to execute the Tween:
TweenLite.to(arrow_clip,1,{rotation:degrees});
but I can’t understand how to calculate the degrees.
I tried the following with no luck:
var degrees =Math.atan2((clip.y-arrow_clip.y),(clip.x-arrow_clip.x))*(180/Math.PI);
Can you help me?
[EDIT]: I found the following is working but I can’t completely understand why:
var degrees = -(Math.atan2(arrow_clip.x-clip.x, arrow_clip.y-clip.y))*(180/Math.PI);
To find the angle between two points, you can use:
atan(dy/dx)oratan2(dy,dx)where
dy = to.y - from.yanddx = to.x - from.xThis will get you radians from the horizontal axis, with 0 being to the right. The
rotationproperty in Flash, however, is degrees from the vertical axis, with 0 being up. So you need to turn that into degrees, and the rotate by 90 degrees, because ifatansays 0 radians then in Flash space, thats 90 degrees and -PI/2 radians = 0 degrees, and so on.The following should work:
The second example you posted works because it does does the 90 degree rotation by switching around the axes and inverting the result. It calculates from the
clipto thearrow, then does-atan(dx/dy). Notice that itsdxoverdy(instead of what its supposed to be) and negates the result. The essentially does the 90 degree rotation for you.