@@ -83,6 +83,27 @@ extension type QuadTree$QueryResult._(Float32List _bytes) {
8383/// The QuadTree can store objects with a width, height, and position.
8484/// Positions are represented as a point (x, y) in the 2D space at the top-left
8585/// corner of the object.
86+ ///
87+ /// The QuadTree is a tree data structure in which each internal node has
88+ /// exactly four children: North-West, North-East, South-West, and South-East.
89+ /// Each node represents a rectangular region of the space.
90+ ///
91+ /// The QuadTree is a spatial partitioning algorithm that is used to subdivide
92+ /// a two-dimensional space into smaller regions for efficient collision
93+ /// detection and spatial queries.
94+ ///
95+ /// [boundary] is the boundary of the QuadTree, usually the size of the game
96+ /// world coordinates or the screen size.
97+ ///
98+ /// [capacity] is the maximum number of objects that can be stored in a node
99+ /// before it subdivides.
100+ /// Suitable values for the capacity are usually between 18 and 24.
101+ /// Should be always greater or equal than 6.
102+ ///
103+ /// [depth] is the maximum depth of the QuadTree. If the depth is reached,
104+ /// the QuadTree will not subdivide further and [capacity] will be ignored.
105+ /// Suitable values for the depth are usually between 8 and 16.
106+ /// Should be always greater or equal than 1.
86107/// {@endtemplate}
87108final class QuadTree {
88109 /// Creates a new Quadtree with [boundary] and a [capacity] .
@@ -92,9 +113,9 @@ final class QuadTree {
92113 // Boundary of the QuadTree.
93114 required ui.Rect boundary,
94115 // Capacity of the each QuadTree node.
95- int capacity = 18 ,
116+ int capacity = 24 ,
96117 // Maximum depth of the QuadTree.
97- int depth = 8 ,
118+ int depth = 12 ,
98119 }) {
99120 assert (boundary.isFinite, 'The boundary must be finite.' );
100121 assert (! boundary.isEmpty, 'The boundary must not be empty.' );
@@ -271,7 +292,7 @@ final class QuadTree {
271292 /// Get rectangle bounds of the object with the given [objectId] .
272293 ui.Rect get (int objectId) {
273294 final objects = _objects;
274- if (objectId < 0 || objectId >= _nextObjectId || objectId >= objects.length)
295+ if (objectId < 0 || objectId >= objects.length)
275296 throw ArgumentError ('Object with id $objectId not found.' );
276297 final offset = objectId * _objectSize;
277298 return ui.Rect .fromLTWH (
@@ -318,6 +339,14 @@ final class QuadTree {
318339 // Do not change the object's id.
319340 node._remove (objectId);
320341
342+ // Mark the node and all its parents as dirty
343+ // and possibly needs optimization.
344+ // Also decrease the length of the node and all its parents.
345+ for (QuadTree$Node ? n = node; n != null ; n = n.parent) {
346+ n._dirty = true ;
347+ n._length-- ;
348+ }
349+
321350 // Insert the object back into the QuadTree at the new position
322351 // with the same id.
323352 final nodeId = root._insert (objectId, left, top, width, height);
@@ -328,9 +357,7 @@ final class QuadTree {
328357 /// Removes [objectId] from the Quadtree if it exists.
329358 /// After removal, tries merging nodes upward if possible.
330359 bool remove (int objectId) {
331- if (objectId < 0 ||
332- objectId >= _nextObjectId ||
333- objectId >= _id2node.length) return false ; // Invalid id
360+ if (objectId < 0 || objectId >= _id2node.length) return false ; // Invalid id
334361 final node = _nodes[_id2node[objectId]];
335362 if (node == null ) return false ; // Node not found
336363
0 commit comments