Weird results with angleBetween (FlxAngle VS FlxPoint)

  • Hi,
    Today I was optimizing my code, when I stumbled into weird things.
    I have some FlxSprite that I used for debuging, and I calculate the angle between them using FlxAngle.
    I was thinking that maybe FlxPoint takes less memory or something, so I replaced the FlxSprite by some FlxPoint.
    But now I have different results. Here's an example:

    var x1=10;
    var y1=400;
    var x2=600;
    var y2=20;
    var startSprite:FlxSprite = new FlxSprite(x1, y1);
    var endSprite:FlxSprite = new FlxSprite(x2, y2);
    trace(FlxAngle.angleBetween(startSprite, endSprite, true));
    // it gives 32.78428086586915 which is correct
    var startPoint:FlxPoint = new FlxPoint(x1, y1);
    var endPoint:FlxPoint = new FlxPoint(x2, y2);
    trace(startPoint.angleBetween(endPoint) - 90);
    // I have to substract 90° because 0 is pointing to the up direction for reasons...
    // it gives 35.25773195876289 which is wrong

    Isn't that weird? Or maybe I'm doing something wrong?
    So right now I'm staying with invisible FlxSprite...

    Thanks for your help.

  • The way I see it FlxAngle.angleBetween() uses the centers of the sprites not their upper left:

    	 * Find the angle (in radians) between the two FlxSprite, taking their x/y and origin into account.
    	public static inline function angleBetween(SpriteA:FlxSprite, SpriteB:FlxSprite, AsDegrees:Bool = false):Float
    		var dx:Float = (SpriteB.x + SpriteB.origin.x) - (SpriteA.x + SpriteA.origin.x);
    		var dy:Float = (SpriteB.y + SpriteB.origin.y) - (SpriteA.y + SpriteA.origin.y);
    		if (AsDegrees)
    			return asDegrees(Math.atan2(dy, dx));
    			return Math.atan2(dy, dx);

    It says

    taking their x/y and origin into account

    which is their centers by default.

  • Ok, but that shouldn't change anything for the angle and the distance between the corners or betweens the centers:

    alt text

    By the way, I didn't put it into my code example, but I gave those sprites a red pixel in order to verify their positions:

    var startSprite:FlxSprite = new FlxSprite(x1, y1);
    startSprite.makeGraphic(1, 1, 0xffff0000);
    var endSprite:FlxSprite = new FlxSprite(x2, y2);
    endSprite.makeGraphic(1, 1, 0xffff0000);

    So if it was a top-left / center problem, the error in the result shouldn't be that big.

    And by the way, I'm wondering why FlxAngle.angleBetweenPoint() gives the angle between an FlxSprite and an FlxPoint, and not between two FlxPoint... >:(

  • I believe this is a bug of some kind. Better report it on GitHub.

Log in to reply

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.