Jump to content

[solved] How dispose mesh in onCollideEvent


Recommended Posts

Hi @all again,

I have a question: I want to remove a bulletMesh (bullet) with the dispose function if it collide with something like

          bullet.physicsImpostor.onCollideEvent = function (self, other) {
                console.log(self, other, self.position);

Got an Error:

Uncaught TypeError: Cannot read property '_wakeUpAfterNarrowphase' of undefined
    at World.internalStep (cannon.js:13514)
    at World.step (cannon.js:13211)
    at CannonJSPlugin.executeStep (babylon.max.js:45178)
    at PhysicsEngine._step (babylon.max.js:36315)
    at Scene.render (babylon.max.js:21270)
    at main.js:847
    at Engine._renderLoop (babylon.max.js:8509)

What do I wrong? Ho can I do this?


Link to comment
Share on other sites

@RaananW It works with a setTimeout : http://www.babylonjs-playground.com/#1GPEKY#2

The exception seems to be in cannon though... Below the code where the exception happen N=2 and bodies.length=1 (here as well: https://github.com/BabylonJS/Babylon.js/blob/master/dist/cannon.js#L13512)

    // Wake up bodies
    for(i=0; i!==N; i++){
        var bi = bodies[i];
            bi._wakeUpAfterNarrowphase = false;


Link to comment
Share on other sites


Seems like we need to play with cannon a bit :)

An array is being manipulated while it is being changed, so it seems. Again, the quickest solution would be to wrap this in a setTimeout (thanks @Temechon :) ). The problem is the cannon loop which doesn't update this N variable (the number of objects). So, disposing on the beginning of the next frame works. And without manipulating cannon, this seems to be the only solution possible.

I can add a setTimeout for the dispose function, that however might cause an interesting issue with the current frame - the physics body will still be there till the end of the frame, until you call dispose.

If this is an accepted solution (again, seems to be the only one at the moment), I will add that.

Link to comment
Share on other sites

A late dispose works, but is not fashionable. In my case I can see how the bullet continue fly a bit longer after it collides. One scond is also realy long. I want the possibility to choose my own setTimeout value. So I think dont change the state.

11 hours ago, RaananW said:

And without manipulating cannon, this seems to be the only solution possible

Why we can't change cannon? Maybe it is a nice idea to contact the cannon developer or/and make a fork or something like this. I Think an own cannon developed by the babylon.js community will be nice. This prevents that the physics becomes a development bottleneck. Besides the cannon developer seems inactive for more than one year. I just brainstorming.:)

Link to comment
Share on other sites

I have already contributed to cannon, this is of course possible. The problem is - how long will it take, and whats need to be done. In this case - the N variable was implemented like that for performance reasons. 

setTimeout without time value will execute before the next frame starts. it won't be a second, and not half a second. it will be one frame after. I do agree it is not the best solution but it is A solution, and it works, without making major changes.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...