Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix distanceTo regression in FlxPoint #3365

Merged
merged 1 commit into from
Feb 14, 2025

Conversation

oscarcederberg
Copy link
Contributor

@oscarcederberg oscarcederberg commented Feb 14, 2025

Fix regression introduced in 6.0.0 where a multiply in distanceSquaredTo was changed to an add, meaning all distanceTo/dist-functions got broken.

Added tests for all the affected functions as well.

In case a minimal setup to test the issue is needed:

package;

import flixel.math.FlxPoint;
import flixel.FlxG;
import flixel.FlxSprite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.util.FlxColor;

class PlayState extends FlxState
{
    var player = new FlxSprite(0, 128);
    var enemy = new FlxSprite (0, 0);
    var text = new FlxText(0, 0, 0, "", 16);

    override public function create()
    {
        super.create();
        player.makeGraphic(32, 32, FlxColor.WHITE);
        enemy.makeGraphic(32, 32, FlxColor.RED);

        player.screenCenter(X);
        enemy.screenCenter(XY);

        add(text);
        add(player);
        add(enemy);
    }

    override public function update(elapsed:Float)
    {
        var up:Bool = FlxG.keys.pressed.UP;
        var down:Bool = FlxG.keys.pressed.DOWN;
        var left:Bool = FlxG.keys.pressed.LEFT;
        var right:Bool = FlxG.keys.pressed.RIGHT;

        if (up && down) {
            up = down = false;
        }
        if (left && right) {
            left = right = false;
        }

        var dx = 0, dy = 0;
        dx = left ? -1 : dx;
        dx = right ? 1 : dx;
        dy = up ? -1 : dy;
        dy = down ? 1 : dy;

        var velocity = 250 * new FlxPoint(dx, dy).normalize();
        player.velocity.set(velocity.x, velocity.y);

        text.text = 'distanceTo: ${player.getPosition().distanceTo(enemy.getPosition())}
        distanceSquaredTo: ${player.getPosition().distanceSquaredTo(enemy.getPosition())}
        dist: ${player.getPosition().dist(enemy.getPosition())}
        distSquared: ${player.getPosition().distSquared(enemy.getPosition())}
        ';

        super.update(elapsed);
    }
}

Note how distanceTo/dist reports NaN or weird values depending on if the player is above or below the enemy and that distanceSquaredTo/distSquared can get negative values without the fix.

@oscarcederberg oscarcederberg changed the title Fix distanceSquaredTo Fix distanceTo regression in FlxPoint Feb 14, 2025
@Geokureli Geokureli added this to the Next Patch milestone Feb 14, 2025
@Geokureli Geokureli added the Bug label Feb 14, 2025
Fix regression and add tests
@Geokureli Geokureli merged commit 9875460 into HaxeFlixel:dev Feb 14, 2025
11 checks passed
@Geokureli
Copy link
Member

Thanks!

@oscarcederberg oscarcederberg deleted the distance-to-fix branch February 14, 2025 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants