I faced several issues related with physicsBody collision event listeners. I prepared 3 playgrounds with detailed comments and console logs. Each playground shows exactly 1 issue.
I assume that these are bugs. But probably I am just don’t understand how it should be in proper way. That’s why I am creating topic in ‘Questions’ section.
When we want to destoy mesh and it’s physicsImpostor after hit, the engine crashes with error Cannot read property '_wakeUpAfterNarrowphase' of undefined. Probably I am missing something before calling dispose(), but I don’t know. “Sorry, new users can only put 2 links in a post.”
[same as above]ZDTTQQ#2
But physicsBody actually has such method, because it is the only way to make handler to fire. I already tried mesh.onCollide and physicsImpostor.onCollide. It doesn’t fire. We discussed this issue before here:
Then Raggar advised to use .addEventListener on physicsBody:
This looked like working solution, but when I start to dig into it I found those three new issues that I mentioned above.
I think the issue here is that you are adding one event listener and removing a different one because bind(this) is creating a different instance of the function. If bind is only called once it should work eg. https://playground.babylonjs.com/indexStable.html#ZDTTQQ#3
If you are using methods on the physicsBody directly you will be calling methods outside of babylon contained in the physics library the plugin uses. In your case cannon is being used which does not provide this functionality. Instead of using methods directly on the physics body you could use methods on the physics impostor instead such as registerOnPhysicsCollide (Babylon.js Playground) however for this you must specify the other other impostors to get the callback for but if theres missing functionality you need let me know.