Skip to content

Commit

Permalink
Merge pull request #6122 from mozilla/bug/physics-bodies
Browse files Browse the repository at this point in the history
Remove bodies that are disconnected from the scene graph
  • Loading branch information
johnshaughnessy authored Jun 20, 2023
2 parents d64fec9 + c816a09 commit 40dce4d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/systems/physics-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class PhysicsSystem {
this.workerHelpers = new WorkerHelpers(this.ammoWorker);

this.bodyUuids = [];
this.bodiesToRemove = [];
this.indexToUuid = {};
this.bodyUuidToData = new Map();

Expand Down Expand Up @@ -146,7 +147,7 @@ export class PhysicsSystem {
* 17 Angular Velocity (float)
* 18-25 first 8 Collisions (ints)
*/

this.bodiesToRemove.length = 0;
if (this.objectMatricesFloatArray.buffer.byteLength !== 0) {
for (let i = 0; i < this.bodyUuids.length; i++) {
const uuid = this.bodyUuids[i];
Expand All @@ -155,8 +156,8 @@ export class PhysicsSystem {
const type = body.options.type ? body.options.type : TYPE.DYNAMIC;
const object3D = body.object3D;
if (!object3D.parent) {
// TODO: Fix me
console.error("Physics body exists but object3D has no parent.");
console.warn("Physics body exists but object3D had no parent; removing the body.");
this.bodiesToRemove.push(uuid);
continue;
}
if (type === TYPE.DYNAMIC) {
Expand Down Expand Up @@ -208,6 +209,10 @@ export class PhysicsSystem {
);
}

for (let i = this.bodiesToRemove.length - 1; i >= 0; i--) {
this.removeBody(this.bodiesToRemove[i]);
}

/* DEBUG RENDERING */
if (this.debugEnabled) {
const index = window.Atomics.load(this.debugIndex, 0);
Expand Down

0 comments on commit 40dce4d

Please sign in to comment.