physicsBody collision event listeners issues (probably bugs?)

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.

  1. bullet.physicsImpostor.physicsBody.removeEventListener doesn’t actually remove event listener.
    Babylon.js Playground

  2. Inside event listener there is no way to get a mesh to which physicsBody was assigned to. I invented some hack for this, but why it is not inside Babylon already?

  3. 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.

1 Like

Hi guys. Hey forum helpers / @Deltakosh … the first post in this thread is still unanswered.

I did a poor/mistaken reply yesterday, and later deleted it. Sorry about that. Consider this thread 0-replies.

Stay tuned.

Pinging @trevordev for help

  1. 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.

  2. 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.

  3. This does seem like an issue, I’m investigating a PR to fix this. (remove impostor from world after step to avoid cannon exception by TrevorDev · Pull Request #6081 · BabylonJS/Babylon.js · GitHub)

1 Like

These things are working good now on latest master. Thanks!