cannon.js: Collision exit event

We can detect collisions using…

body.addEventListener('collide', onCollide);

…Is there a way to detect when a collision exits?

body.addEventListener('collideExit', onCollideExit);

About this issue

  • Original URL
  • State: open
  • Created 9 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

That would be nice to have, but it’s not currently implemented. In p2.js I’ve implemented the beginContact and endContact events, which should be quite easy to remake in cannon.js. The contact tracking should be refactored though, right now it’s a bit messy…

If you want to track if two bodies are in contact, run through the world.contacts array.

function bodiesAreInContact(bodyA, bodyB){
    for(var i=0; i<world.contacts.length; i++){
        var c = world.contacts[i];
        if((c.bi === bodyA && c.bj === bodyB) || (c.bi === bodyB && c.bj === bodyA)){
            return true;
        }
    }
    return false;
}

These are available. You listen to the events on the world.

world.addEventListener('beginContact', () => {
            console.log('contact!!!')
        })

        world.addEventListener('endContact', () => {
            console.log('end contact!!!')
        })

Note: Have only verified this on the maintained fork that is cannon-es https://github.com/pmndrs/cannon-es

I can’t seem to find this anywhere in the latest build… is there a collideExit event?