Skip to content

Commit

Permalink
Band-aid for physics-system crash
Browse files Browse the repository at this point in the history
  • Loading branch information
johnshaughnessy committed Aug 3, 2022
1 parent cd8e2a1 commit b8e7469
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 66 deletions.
15 changes: 4 additions & 11 deletions src/components/body-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ AFRAME.registerComponent("body-helper", {
init: function() {
this.system = this.el.sceneEl.systems["hubs-systems"].physicsSystem;
this.alive = true;
this.uuid = -1;
this.system.registerBodyHelper(this);
},

init2: function() {
this.el.object3D.updateMatrices();
this.uuid = this.system.addBody(this.el.object3D, this.data);
const eid = this.el.object3D.eid;
Expand All @@ -50,17 +45,15 @@ AFRAME.registerComponent("body-helper", {
},

update: function(prevData) {
if (prevData !== null && this.uuid !== -1) {
if (prevData) {
this.system.updateBody(this.uuid, this.data);
}
},

remove: function() {
if (this.uuid !== -1) {
this.system.removeBody(this.uuid);
const eid = this.el.object3D.eid;
removeComponent(APP.world, Rigidbody, eid);
}
this.system.removeBody(this.uuid);
const eid = this.el.object3D.eid;
removeComponent(APP.world, Rigidbody, eid);
this.alive = false;
}
});
8 changes: 1 addition & 7 deletions src/components/shape-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,7 @@ AFRAME.registerComponent("shape-helper", {

init: function() {
this.system = this.el.sceneEl.systems["hubs-systems"].physicsSystem;
this.alive = true;
this.uuid = -1;
this.system.registerShapeHelper(this);
},

init2: function() {
this.mesh = null;

let bodyEl = this.el;
Expand All @@ -52,7 +47,7 @@ AFRAME.registerComponent("shape-helper", {
}
}
if (!this.bodyHelper || this.bodyHelper.uuid === null || this.bodyHelper.uuid === undefined) {
console.warn("body not found");
console.error("body not found");
return;
}
if (this.data.fit === FIT.ALL) {
Expand All @@ -72,6 +67,5 @@ AFRAME.registerComponent("shape-helper", {
if (this.uuid !== -1 && this.bodyHelper.alive) {
this.system.removeShapes(this.bodyHelper.uuid, this.uuid);
}
this.alive = false;
}
});
90 changes: 42 additions & 48 deletions src/systems/physics-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export class PhysicsSystem {
this.ammoWorker = new AmmoWorker();
this.workerHelpers = new WorkerHelpers(this.ammoWorker);

this.bodyHelpers = [];
this.shapeHelpers = [];
this.bodyUuids = [];
this.indexToUuid = {};
this.bodyUuidToData = new Map();
Expand Down Expand Up @@ -53,31 +51,24 @@ export class PhysicsSystem {
this.ammoWorker.onmessage = async event => {
if (event.data.type === MESSAGE_TYPES.READY) {
this.ready = true;
for (const bodyHelper of this.bodyHelpers) {
if (bodyHelper.alive) bodyHelper.init2();
}
for (const shapeHelper of this.shapeHelpers) {
if (shapeHelper.alive) shapeHelper.init2();
}
this.shapeHelpers.length = 0;
this.bodyHelpers.length = 0;
} else if (event.data.type === MESSAGE_TYPES.BODY_READY) {
const uuid = event.data.uuid;
const index = event.data.index;
if (this.bodyUuidToData.has(uuid)) {
const { uuid, index } = event.data;
const bodyData = this.bodyUuidToData.get(uuid);
bodyData.index = index;
bodyData.isInitialized = true;
if (bodyData.removeBodyMessageSent) {
this.bodyUuidToData.delete(uuid);
} else {
this.bodyUuids.push(uuid);
this.bodyUuidToData.get(uuid).index = index;
this.indexToUuid[index] = uuid;
} else {
console.warn(`Body initialized for uuid: ${uuid} but body missing.`);
}
} else if (event.data.type === MESSAGE_TYPES.SHAPES_READY) {
const bodyUuid = event.data.bodyUuid;
const shapesUuid = event.data.shapesUuid;
if (this.bodyUuidToData.has(bodyUuid)) {
this.bodyUuidToData.get(bodyUuid).shapes.push(shapesUuid);
} else {
console.warn(`Shape initialized but body with uuid: ${bodyUuid} missing.`);
console.warn(`Shape initialized on worker but body is missing.`);
}
} else if (event.data.type === MESSAGE_TYPES.TRANSFER_DATA) {
this.objectMatricesFloatArray = event.data.objectMatricesFloatArray;
Expand Down Expand Up @@ -161,6 +152,11 @@ export class PhysicsSystem {
const index = body.index;
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.");
continue;
}
if (type === TYPE.DYNAMIC) {
matrix.fromArray(
this.objectMatricesFloatArray,
Expand Down Expand Up @@ -225,19 +221,24 @@ export class PhysicsSystem {
})();

addBody(object3D, options) {
this.workerHelpers.addBody(this.nextBodyUuid, object3D, options);
const bodyId = this.nextBodyUuid;
this.nextBodyUuid += 1;

this.workerHelpers.addBody(bodyId, object3D, options);

this.bodyUuidToData.set(this.nextBodyUuid, {
this.bodyUuidToData.set(bodyId, {
object3D: object3D,
options: options,
collisions: [],
linearVelocity: 0,
angularVelocity: 0,
index: -1,
shapes: []
shapes: [],
isInitialized: false,
removeBodyMessageSent: false
});

return this.nextBodyUuid++;
return bodyId;
}

updateBody(uuid, options) {
Expand All @@ -251,25 +252,34 @@ export class PhysicsSystem {

// TODO inline updateBody
updateBodyOptions(bodyId, options) {
const bodyData = this.bodyUuidToData.get(bodyId);
if (!bodyData) {
// TODO: Fix me.
console.warn("updateBodyOptions called for invalid bodyId");
return;
}
this.workerHelpers.updateBody(bodyId, Object.assign(this.bodyUuidToData.get(bodyId).options, options));
}

removeBody(uuid) {
const idx = this.bodyUuids.indexOf(uuid);
if (this.bodyUuidToData.has(uuid) && idx !== -1) {
delete this.indexToUuid[this.bodyUuidToData.get(uuid).index];
const bodyData = this.bodyUuidToData.get(uuid);
if (!bodyData) {
// TODO: REMOVE ME. We should not ever see this!
console.error(`removeBody called for unknown body id`);
return;
}

this.workerHelpers.removeBody(uuid);
bodyData.removeBodyMessageSent = true;

const collisions = this.bodyUuidToData.get(uuid).collisions;
collisions.forEach(otherId => {
if (bodyData.isInitialized) {
delete this.indexToUuid[bodyData.index];
bodyData.collisions.forEach(otherId => {
const otherData = this.bodyUuidToData.get(otherId).collisions;
otherData.splice(otherData.indexOf(uuid), 1);
});

this.bodyUuids.splice(this.bodyUuids.indexOf(uuid), 1);
this.bodyUuidToData.delete(uuid);
this.bodyUuids.splice(idx, 1);
this.workerHelpers.removeBody(uuid);
} else {
console.warn(`removeBody called for uuid: ${uuid} but body missing.`);
}
}

Expand All @@ -294,7 +304,7 @@ export class PhysicsSystem {
console.warn(`removeShapes called for shapesUuid: ${shapesUuid} on bodyUuid: ${bodyUuid} but shapes missing.`);
}
} else {
console.warn(`Tried to remove shape for unknown body ${bodyUuid}`);
console.error(`Tried to remove shape for unknown body ${bodyUuid}`);
}
}

Expand All @@ -306,22 +316,6 @@ export class PhysicsSystem {
this.workerHelpers.removeConstraint(constraintId);
}

registerBodyHelper(bodyHelper) {
if (this.ready) {
bodyHelper.init2();
} else {
this.bodyHelpers.push(bodyHelper);
}
}

registerShapeHelper(shapeHelper) {
if (this.ready) {
shapeHelper.init2();
} else {
this.shapeHelpers.push(shapeHelper);
}
}

bodyInitialized(uuid) {
return this.bodyUuidToData.has(uuid) && this.bodyUuidToData.get(uuid).index !== -1;
}
Expand Down

0 comments on commit b8e7469

Please sign in to comment.