Foniks
November 28, 2017, 2:27pm
#1
Hi,
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).
https://playcanvas.com/project/519676/overview/p2jsexample
Any help would be greatly appreciated!
Cheers.
1 Like
yaustar
November 28, 2017, 2:46pm
#2
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.
Foniks
November 28, 2017, 3:18pm
#4
Ah - thanks again. Yeah, the documentation on Github under collisions says ‘to do’.
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.
Foniks
November 28, 2017, 3:21pm
#5
Thank you very much for that - I will take a look. Looks like a great resource.
1 Like