n5ro / aframe-physics-system

Physics system for A-Frame VR, built on CANNON.js.
https://n5ro.github.io/aframe-physics-system/
MIT License
507 stars 137 forks source link

Ammo: Not all entities/bodies remove on collision event (issue with remove entities) #137

Open smeybi opened 5 years ago

smeybi commented 5 years ago

Hi, I noticed something, I'm not sure if it's a issue, maybe I'll remove entities the wrong way.

I would like to remove entities at collision. The collide events work, but if i use this for removeChild, not all entities are removed. It's look like a rhythm, a few are skipped.

This is the collision event code: this.el.addEventListener('collidestart', function(e) { if (remove_e) { e.target.parentNode.removeChild(e.target); } else { e.target.setAttribute('color', "blue"); }

Here is a example (only collisions with static bodies) on glitch, when a button is pressed, it will change to remove entity or color change: https://glitch.com/edit/#!/aframe-physics-ammo2

The same example with dynamic bodies it works, but I think it works, because there are many collisions events, that finally remove the entity. https://glitch.com/edit/#!/aframe-physics-ammo1

Edit: The same issue is when removing entities direct, not on the collision event. The collisions events skip some entities.

ghost commented 4 years ago

When using AmmoDriver, removing objects with removeChild will prevent "collidestart" and "collideend" of the remaining objects. Is there any other way to delete the object with Ammo-body added?

(edit) post new issue https://github.com/donmccurdy/aframe-physics-system/issues/141

smeybi commented 3 years ago

Try after a long time, I found out, it has a problem with deleting the ammo body on remove the entity (with ammo physics). I recreated and combined it with three.js. It work until remove the entity. For example it works, remove the ammo body and the object3D separate, not problem document.querySelector('a-scene').systems.physics.driver.removeBody(e.target.body) e.target.object3D.parent.remove(e.target.object3D)

But remove the entity there were the dropouts again. e.target.parentNode.removeChild(e.target)

I figured out, if this Ammo.destroy(this.body) is exchanged with the this.system.driver.removeBody(this.body) in the script for the Ammo remove function, it works. I don't know if this causes problems anywhere else! I hope the information helps more to find the problem!

aframe-physics-system.js Line 14912:

remove: function() { if (this.triMesh) Ammo.destroy(this.triMesh); if (this.localScaling) Ammo.destroy(this.localScaling); if (this.compoundShape) Ammo.destroy(this.compoundShape); if (this.body) { this.system.driver.removeBody(this.body) //Ammo.destroy(this.body); delete this.body; } Ammo.destroy(this.rbInfo); Ammo.destroy(this.msTransform); Ammo.destroy(this.motionState); Ammo.destroy(this.localInertia); Ammo.destroy(this.rotation); },

aussieburger commented 3 years ago

Try after a long time, I found out, it has a problem with deleting the ammo body on remove the entity (with ammo physics). I recreated and combined it with three.js. It work until remove the entity. For example it works, remove the ammo body and the object3D separate, not problem document.querySelector('a-scene').systems.physics.driver.removeBody(e.target.body) e.target.object3D.parent.remove(e.target.object3D)

But remove the entity there were the dropouts again. e.target.parentNode.removeChild(e.target)

I figured out, if this Ammo.destroy(this.body) is exchanged with the this.system.driver.removeBody(this.body) in the script for the Ammo remove function, it works. I don't know if this causes problems anywhere else! I hope the information helps more to find the problem!

aframe-physics-system.js Line 14912:

remove: function() { if (this.triMesh) Ammo.destroy(this.triMesh); if (this.localScaling) Ammo.destroy(this.localScaling); if (this.compoundShape) Ammo.destroy(this.compoundShape); if (this.body) { this.system.driver.removeBody(this.body) //Ammo.destroy(this.body); delete this.body; } Ammo.destroy(this.rbInfo); Ammo.destroy(this.msTransform); Ammo.destroy(this.motionState); Ammo.destroy(this.localInertia); Ammo.destroy(this.rotation); },

Legend figuring that out and posting it! Was banging my head for days trying to figure out why that was happening :)

wujiiu commented 1 year ago

Is there any progress pls?