Search code examples
actionscript-3atan2

How to calculate third point on line using atan2?


I'm trying to animate some bitmaps out in relation to a center point. They don't all start at that center point, but I want them to fly out as though a force from that center point slammed into them and pushed them outwards radially, such that they fly completely off the stage.

So: I know the center point, and the x and y position of each bitmap arranged around it. For each one I can draw a line from the center to that x,y point. I should then be able to get the angle formed by that line to the horizontal, and then set a destination point farther out on that line. The bitmap will be tweened out to that point. I believe that that is what Math.atan2 is for.

Here's what I've got as I iterate through the array of bitmaps (i is an object):

var angle:Number = Math.atan2(i.bitmap.y - centerY, i.bitmap.x - centerX) * 180 / Math.PI;
var dist:Number = 200;              //arbitrary number, just to test
 destX = centerX  + dist * Math.cos(angle);  //destination x
 destY = centerY  + dist * Math.sin(angle);  //destination y

Instead of these things gliding out radially, they're jumping around.

I'm having trouble understanding atan2 and exactly what I'm doing wrong.

Thanks,

David


Solution

  • Try removing the *180/PI to keep the angle in radians.

    var angle:Number = Math.atan2(i.bitmap.y-centerY, i.bitmap.x - centerX);
    

    Then change destX and destY to

    destX = i.bitmap.x  + dist * Math.cos(angle);
    destY = i.bitmap.y  + dist * Math.sin(angle);