Skip to content

Commit

Permalink
Optimize collision tree (skip checks for leafs without active bodies)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrA1ex committed Oct 6, 2023
1 parent 4fde7c6 commit c5ca03e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
10 changes: 7 additions & 3 deletions lib/physics/common/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@ class SpatialLeaf {
segmentBoundary;
/** @type{Body[]} */
items;
containsActive = true;

id = 0;

/**
* @param {number} id
* @param {Body[]} items
* @param {BoundaryBox} boundary
* @param {BoundaryBox} segmentBoundary
* @param {BoundaryBox} [segmentBoundary=null]
* @param {Boolean} [containsActive=true]
*/
constructor(id, items, boundary, segmentBoundary = null) {
constructor(id, items, boundary, segmentBoundary = null, containsActive = true) {
this.id = id;
this.items = items;
this.boundary = boundary;
this.segmentBoundary = segmentBoundary ?? boundary;
this.containsActive = containsActive;
}

addLeaf(leaf) {
Expand Down Expand Up @@ -86,7 +89,8 @@ export class SpatialTree {

if (filteredItems.length > 0) {
const boundaryBox = BoundaryBox.fromBodies(filteredItems, BoundaryBoxPool);
const leaf = new SpatialLeaf(this.#leafId++, filteredItems, boundaryBox, segmentBoundary);
const containsActive = current.containsActive && filteredItems.some(b => b.active);
const leaf = new SpatialLeaf(this.#leafId++, filteredItems, boundaryBox, segmentBoundary, containsActive);
current.addLeaf(leaf);
}
}
Expand Down
13 changes: 9 additions & 4 deletions lib/physics/solver.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,16 @@ export class ImpulseBasedSolver {
}

#processTreeLeaf(current) {
if (!current.containsActive) return;

if (current.leafs.length > 0) {
for (let i = 0; i < current.leafs.length; i++) {
const l1 = current.leafs[i];
for (let j = i + 1; j < current.leafs.length; j++) {
const l2 = current.leafs[j];

if (GeomUtils.isBoundaryCollide(l1.boundary, l2.boundary)) {
const containsActive = l1.containsActive || l2.containsActive;
if (containsActive && GeomUtils.isBoundaryCollide(l1.boundary, l2.boundary)) {
this.#processLeafOuterCollisions(l1, l2);
}
}
Expand All @@ -217,7 +220,7 @@ export class ImpulseBasedSolver {
}

this.stepInfo.checkCount += current.leafs.length * (current.leafs.length + 1) / 2;
} else {
} else if (current.containsActive) {
this.#processLeafInnerCollisions(current);
}
}
Expand All @@ -238,15 +241,17 @@ export class ImpulseBasedSolver {
const collidingL1 = new Array(leaf1.leafs.length);
let i = 0
for (const l1 of leaf1.leafs) {
if (GeomUtils.isBoundaryCollide(l1.boundary, leaf2.boundary)) {
const containActive = l1.containsActive || leaf2.containsActive;
if (containActive && GeomUtils.isBoundaryCollide(l1.boundary, leaf2.boundary)) {
collidingL1[i++] = l1;
}
}

const collidingL2 = new Array(leaf2.leafs.length);
let j = 0;
for (const l2 of leaf2.leafs) {
if (GeomUtils.isBoundaryCollide(l2.boundary, leaf1.boundary)) {
const containActive = l2.containsActive || leaf1.containsActive;
if (containActive && GeomUtils.isBoundaryCollide(l2.boundary, leaf1.boundary)) {
collidingL2[j++] = l2;
}
}
Expand Down

0 comments on commit c5ca03e

Please sign in to comment.