Jump to content

P2 Physics onBeginContact without executing collision?


PansenDarmVirus
 Share

Recommended Posts

Hello there,

I started using phaser a week ago and for my starter project i need accurate collision detection - that means P2 engine yeaah!

But i just dont need the default collisions with bouncing everything away.

Basically all sprites/bodies should be static and on contact i want a custom function to do the job.

The problem for me ist just, that as soon as both objects are static, no onBeginContact nor any similar event is triggered, as far as i know.

Working against the physics movement and rotation following a collision is no option - so buggy

So how can i receive a contact event without executing the default collision for at least one of the 2 objects? Why is this simple task so complicated?:blink: am i just stupid?:ph34r:

Link to comment
Share on other sites

After struggling with this problem for quite some time i found a solution with the shape.sensor=true property. The tricky part here was, that Phaser splits a polygon into multiple shapes, which all require their personal sensor=true. So finally my code looks like:

gemuese = this.add.sprite(42,42,"gurke");
this.game.physics.p2.enable(gemuese, true);
gemuese.body.clearShapes();
gemuese.body.loadPolygon("collision_masks", "gurke");
for(var i=0; i<gemuese.body.data.shapes.length;i++)gemuese.body.data.shapes[i].sensor=true;
gemuese.body.onBeginContact.add(fischfutter, this);
fischfutter = function(body, bodyB, shapeA, shapeB, equation) {
   console.log("Heureka!");
   }

I suggest to include the polygon-split-up (or generally the contact without collision thing)  into the p2 examples in some way at least, because-  well, I wasn't the first with this kind of problem and probably won't be the last. See you guys :lol:

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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