[SOLVED] P2 Collision events


I’m doing some tests with the P2.js - integration seems pretty straight forward, but the thing that is baffling me is how to get the collision events. When using Phaser I would register a callback on the body using onBeginContact.add… but that does not appear to be available??

I have a project here (I have deliberately left the crash in there to demonstrate the issue).


Any help would be greatly appreciated!


1 Like

I think those functions have been added by Phaser.js as shown by here: https://libraries.io/github/Weedshaker/phaser-p2Helper

GitHub has the ‘vanilla’ documentation: https://github.com/schteppe/p2.js

Check this tutorial, it explains how to listen for collisions using p2.js. It provides a sample project as well:

Basically you bind your event callbacks to the world object to get the two objects that collided.

Ah - thanks again. Yeah, the documentation on Github under collisions says ‘to do’. :wink:

It looks like you can get access to the world object (I found a demo in the repo that seems to do it this way) via the body - so something like:

this.entity.script.p2Body.body.world.on('beginContact', function () {
       console.log('Collision registered...'); 

…does in fact work.

Thanks again. :slight_smile:

Thank you very much for that - I will take a look. Looks like a great resource. :slight_smile:

1 Like