Skip to content

QuadTree collision detection with minimumDistance misses hitboxes that moved #4247

Backlog

Description

@Yasser-Ameur

What happened?

With HasQuadTreeCollisionDetection and minimumDistance set, a hitbox that was added far away and then moves next to another hitbox never collides with it.

What do you expect?

The same collisions as without minimumDistance: once the two hitboxes overlap, onCollisionStart fires on both.

How can we reproduce this?

class MyGame extends FlameGame with HasQuadTreeCollisionDetection {
  @override
  Future<void> onLoad() async {
    initializeCollisionDetection(
      mapDimensions: const Rect.fromLTWH(0, 0, 1000, 1000),
      minimumDistance: 50,
    );
  }
}

// player: active RectangleHitbox, position (0, 0), size 10
// platform: passive RectangleHitbox, position (500, 500), size 10
await game.ensureAddAll([player, platform]);
game.update(0);

platform.position = Vector2.all(5); // now overlaps the player
game.update(0);
// expected: player.onCollisionStart called once
// actual: never called, however long the two stay overlapped

If the platform is added in range and then moved, it works, so it depends on where the hitbox was when it was first added.

What steps should take to fix this?

QuadTreeBroadphase keeps a cached center per hitbox for the minimumDistance check. updateTransform calls _cacheCenterOfHitbox, which only writes when there is no entry yet, so the center from the first add is kept and the moved hitbox is still measured from its old position. Writing the new center in updateTransform fixes it. I have that change with tests and will open a PR.

Affected platforms

All

Other information

Checked on main at d185bf9.

Are you interested in working on a PR for this?

  • I want to work on this

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions