Skip to content

fix: Remove and move hitboxes correctly after QuadTree.optimize - #4287

Open
turalbabayev wants to merge 1 commit into
flame-engine:mainfrom
turalbabayev:fix/quadtree-optimize-hitbox-node
Open

turalbabayev wants to merge 1 commit into
flame-engine:mainfrom
turalbabayev:fix/quadtree-optimize-hitbox-node

Conversation

@turalbabayev

Copy link
Copy Markdown
Contributor

Description

QuadTree keeps a map from each hitbox to the node that holds it, and remove uses that map to find the hitbox. When optimize() merges the children of a node, it moves their hitboxes into the parent but doesn't update the map, so those hitboxes still point to the child nodes that were dropped. After that:

  • Removing one of those hitboxes removes it from the dropped child and leaves it in the tree. Its component is gone, but the hitbox still comes back from query and hitboxes.
  • Moving one of those hitboxes (updateTransform calls remove and then add) adds it a second time, so the tree holds it twice.

optimize() now points the moved hitboxes to the node they were merged into.

New test in test/collisions/quadtree_test.dart: with maxObjects: 1, two blocks split the root. The test removes one block, calls optimize(), then moves and removes the other one, and checks tree.hitboxes after each step. On main the moved hitbox is listed twice. With this change the test passes.

Checked locally on macOS with Flutter 3.50.1: dart format reports no changes, flutter analyze reports no issues, cspell finds nothing, and flutter test in packages/flame passes except for the same 8 golden tests that also fail on main on this machine.

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • [-] I have updated/added relevant documentation in docs and added dartdoc comments with ///.
  • [-] I have updated/added relevant examples in examples or docs.

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

None.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant