Skip to content

Commit c15fb05

Browse files
committed
Change size of object
1 parent f29ae43 commit c15fb05

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

lib/src/collisions/quadtree.dart

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,28 @@ final class QuadTree {
336336
);
337337
}
338338

339+
/// Change the size of the object with the given [objectId] to the new
340+
/// [width] and [height].
341+
void changeSize(int objectId, {double? width, double? height}) {
342+
if (width == null && height == null) return;
343+
final objects = _objects;
344+
if (objectId < 0 || objectId >= objects.length) return;
345+
final offset = objectId * _objectSize;
346+
if (width != null) objects[offset + 2] = width;
347+
if (height != null) objects[offset + 3] = height;
348+
}
349+
339350
/// Move the object with the given [objectId] to the new position
340351
/// [left] (x), [top] (y).
341-
void move(int objectId, double left, double top) {
352+
///
353+
/// Optionally, you can also change the [width] and [height] of the object.
354+
void move(
355+
int objectId,
356+
double left,
357+
double top, {
358+
double? width,
359+
double? height,
360+
}) {
342361
final root = _root;
343362
if (root == null) return;
344363
if (objectId < 0 || objectId >= _id2node.length) return;
@@ -353,17 +372,20 @@ final class QuadTree {
353372
objects[offset + 0] = left;
354373
objects[offset + 1] = top;
355374

375+
if (width != null) objects[offset + 2] = width;
376+
if (height != null) objects[offset + 3] = height;
377+
356378
// Get the object's width and height.
357-
final width = objects[offset + 2];
358-
final height = objects[offset + 3];
379+
final w = width ?? objects[offset + 2];
380+
final h = height ?? objects[offset + 3];
359381

360382
// Check if the object still fits in the same node's boundary.
361383
if (node == null) {
362384
assert(false, 'Current node not found for object with id $objectId.');
363385
// Insert the object to the QuadTree at the new position.
364-
final nodeId = root._insert(objectId, left, top, width, height);
386+
final nodeId = root._insert(objectId, left, top, w, h);
365387
_id2node[objectId] = nodeId;
366-
} else if (_overlapsLTWH(node.boundary, left, top, width, height)) {
388+
} else if (_overlapsLTWH(node.boundary, left, top, w, h)) {
367389
// The object still fits in the same node's boundary.
368390
// Coordinate already updated - nothing to do.
369391
} else {
@@ -382,7 +404,7 @@ final class QuadTree {
382404

383405
// Insert the object back into the QuadTree at the new position
384406
// with the same id.
385-
final nodeId = root._insert(objectId, left, top, width, height);
407+
final nodeId = root._insert(objectId, left, top, w, h);
386408
_id2node[objectId] = nodeId;
387409
}
388410
}

0 commit comments

Comments
 (0)