Skip to content

Commit

Permalink
Init the data structures relevant for collision calculations when the…
Browse files Browse the repository at this point in the history
… body is added, rather than on the fly

If a body is deleted before it can be the body0 of a collision, an
entry for it won't be created in the collisionKeys array. However, because
of the splice idiom used at removal, when a body that has no entry in
collisionKeys is deleted, an arbitrary entry will be deleted regardless.

Eventually, when further bodys are removed according to this pattern, there
may be no entry in collisionKeys at all, which will prevent the collisions map
from ever being emptied. This can completely prevent the firing of collisionstart
events.
  • Loading branch information
Antonio Pisano committed Dec 28, 2023
1 parent 98cf0b3 commit a9fc4e2
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/drivers/ammo-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ AmmoDriver.prototype.init = function(worldConfig) {
/* @param {Ammo.btCollisionObject} body */
AmmoDriver.prototype.addBody = function(body, group, mask) {
this.physicsWorld.addRigidBody(body, group, mask);
this.els.set(Ammo.getPointer(body), body.el);
const bodyptr = Ammo.getPointer(body);
this.els.set(bodyptr, body.el);
this.collisions.set(bodyptr, []);
this.collisionKeys.push(bodyptr);
this.currentCollisions.set(bodyptr, new Set());
};

/* @param {Ammo.btCollisionObject} body */
Expand Down Expand Up @@ -103,10 +107,6 @@ AmmoDriver.prototype.step = function(deltaTime) {
}

if (collided) {
if (!this.collisions.has(body0ptr)) {
this.collisions.set(body0ptr, []);
this.collisionKeys.push(body0ptr);
}
if (this.collisions.get(body0ptr).indexOf(body1ptr) === -1) {
this.collisions.get(body0ptr).push(body1ptr);
if (this.eventListeners.indexOf(body0ptr) !== -1) {
Expand All @@ -116,9 +116,6 @@ AmmoDriver.prototype.step = function(deltaTime) {
this.els.get(body1ptr).emit("collidestart", { targetEl: this.els.get(body0ptr) });
}
}
if (!this.currentCollisions.has(body0ptr)) {
this.currentCollisions.set(body0ptr, new Set());
}
this.currentCollisions.get(body0ptr).add(body1ptr);
}
}
Expand Down

0 comments on commit a9fc4e2

Please # to comment.