Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Dear maintainers,
I was struggling with some misbehavior in my application, without realizing this may very well be related with issues #47 #52 and #54
Similar to the use cases reported originally by @smeybi, my application also may remove ammo bodies during its lifecycle, as they represent assets that are spawned/despawned on the scene.
After some long debugging I have realized there existed a problem with the way ammo bodies were deleted. If we look at
aframe-physics-system/src/drivers/ammo-driver.js
Lines 67 to 76 in 98cf0b3
At line 74 we see that when an ammo body is deleted, its supposed entry in the collisionKeys array is removed. However, this entry will be there only if the body ever participated in a collision in the role of body0
aframe-physics-system/src/drivers/ammo-driver.js
Lines 106 to 109 in 98cf0b3
If this never happened at the time the body is removed, the resulting index -1 will de-facto make so that splice will delete an arbitrary entry in collisionKeys. If this pattern repeats often enough, it will de-facto disable event generation for any body on the scene, as only in the collisionKeys loop we do cleanup the collisions array, which is checked before we trigger the event
aframe-physics-system/src/drivers/ammo-driver.js
Lines 110 to 114 in 98cf0b3
aframe-physics-system/src/drivers/ammo-driver.js
Lines 126 to 140 in 98cf0b3
In my proposal I revert the previous fix, which came at the cost of a memory leak, and refactor the code so that we do not instantiate the data structures relevant for collisions on the fly, but at body creation. This ensures that a collisionKeys entry is there for every body and also makes the code more orderly IMO.
Please have a look if this also solves the issue reported originally.
All the best