Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ class QuadTree<T extends Hitbox<T>>({
if (child == null) {
throw StateError('Child must be not null');
}
for (final hitbox in child.hitboxes) {
_hitboxAtNode[hitbox as ShapeHitbox] = node;
}
node.hitboxes.addAll(child.hitboxes);
child.hitboxes.clear();
node.children[i] = null;
Expand Down
41 changes: 41 additions & 0 deletions packages/flame/test/collisions/quadtree_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:flame/image_composition.dart';
import 'package:flame_test/flame_test.dart';
import 'package:flutter_test/flutter_test.dart';

import 'collision_test_helpers.dart';

void main() {
group('QuadTree', () {
testWithGame<HasQuadTreeCollidablesGame>(
'keeps track of hitboxes that optimize moved to another node',
() {
final game = HasQuadTreeCollidablesGame();
game.initializeCollisionDetection(
mapDimensions: const Rect.fromLTWH(0, 0, 1000, 1000),
maxObjects: 1,
);
return game;
},
(game) async {
final tree = game.collisionDetection.broadphase.tree;
final a = TestBlock(Vector2.all(10), Vector2.all(10));
final b = TestBlock(Vector2.all(600), Vector2.all(10));
await game.ensureAddAll([a, b]);
game.update(0);

b.removeFromParent();
game.update(0);
// Merges the quadrants, which moves the hitbox of a to the root node.
tree.optimize();

a.position = Vector2.all(20);
game.update(0);
expect(tree.hitboxes, [a.hitbox]);

a.removeFromParent();
game.update(0);
expect(tree.hitboxes, isEmpty);
},
);
});
}
Loading