Skip to content

Commit

Permalink
- Return back boundary pool for tree
Browse files Browse the repository at this point in the history
- fix segment boundary borders to use epsilon (avoid lost items)
  • Loading branch information
DrA1ex committed Oct 7, 2023
1 parent 2c2c300 commit 8e1579a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
29 changes: 23 additions & 6 deletions lib/physics/common/tree.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {BoundaryBox} from "./boundary.js";
import {ObjectStack} from "../../misc/stack.js";

const EPSILON = 1e-9;

/** @type{ObjectStack<BoundaryBox>} */
const BoundaryBoxPool = new ObjectStack(() => BoundaryBox.empty());

class SpatialLeaf {
/** @type{SpatialLeaf[]} */
leafs = [];
Expand Down Expand Up @@ -37,6 +41,7 @@ class SpatialLeaf {

export class SpatialTree {
#leafId = 0;
#savedStackPosition;

/** @type {SpatialLeaf} */
root = null;
Expand All @@ -52,14 +57,23 @@ export class SpatialTree {
this.divider = divider;
this.maxCount = maxCount;

const boundary = BoundaryBox.fromBodies(items);
this.#savedStackPosition = BoundaryBoxPool.save();

const boundary = BoundaryBox.fromBodies(items, BoundaryBoxPool);
const segmentBoundary = this.#createLevelBoundary(boundary);

this.root = new SpatialLeaf(this.#leafId++, items, BoundaryBox.fromBodies(items), segmentBoundary, true);
this.root = new SpatialLeaf(this.#leafId++, items, boundary, segmentBoundary, true);

this.#fillLeaf(this.root);
}

free() {
if (this.#savedStackPosition === null) return;

BoundaryBoxPool.restore(this.#savedStackPosition);
this.#savedStackPosition = null;
}

#fillLeaf(leaf) {
if (leaf.items.length <= this.maxCount) return;

Expand All @@ -78,7 +92,7 @@ export class SpatialTree {
const filteredItems = items.filter(b => !b.static && SpatialTree.#isContainedByBoundary(b, segmentBoundary));
if (filteredItems.length <= 0) return null;

const boundaryBox = BoundaryBox.fromBodies(filteredItems);
const boundaryBox = BoundaryBox.fromBodies(filteredItems, BoundaryBoxPool);
const containsActive = parent.containsActive && filteredItems.some(b => b.active);

return new SpatialLeaf(this.#leafId++, filteredItems, boundaryBox, segmentBoundary, containsActive);
Expand All @@ -89,11 +103,14 @@ export class SpatialTree {
if (step <= EPSILON) return;

for (let i = 0; i < this.divider; i++) {
const left = segmentBoundary.left + i * step
const right = (i < this.divider - 1 ? left + step : segmentBoundary.right + EPSILON);

for (let j = 0; j < this.divider; j++) {
const left = segmentBoundary.left + i * step;
const top = segmentBoundary.top + j * step
const top = segmentBoundary.top + j * step;
const bottom = (j < this.divider - 1 ? top + step : segmentBoundary.bottom + EPSILON);

yield new BoundaryBox(left, left + step, top, top + step);
yield BoundaryBoxPool.get().update(left, right, top, bottom);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions lib/physics/solver.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export class ImpulseBasedSolver {
#collectCollisions() {
let t = performance.now();

this.stepInfo.tree?.free();
this.stepInfo.tree = new SpatialTree(this.rigidBodies, this.treeDivider, this.treeMaxCount);
this.stepInfo.treeTime = performance.now() - t;

Expand Down

0 comments on commit 8e1579a

Please sign in to comment.