Jump to content

P2 Physics : How to check collision with a tile layer in setPostBroadphaseCallback?


Recommended Posts


I was using Arcade physics and the collisions were working perfectly fine when defined as physics.arcade.collide. In my game, I have an AI character moving in a straight line, flipping direction when colliding with walls. Walls are defined in a layer, with a custom property 'collision' set to true. (defined in Tiled). 

I had to switch to P2 physics as I needed more sophisticated collisions for some objects that had polygon shapes, and also had to use something that P2.Spring defines.

With P2, when my AI collides with the walls it bounces back in XY direction - while the ideal behaviour is that with - this.body.velocity.x *= -1; OR this.body.velocity.y *= -1; - It should only remain on its axis.

I thought I would solve it by checking collision bodies in setPostBroadphaseCallback but that does not seem to work as desired or may be I am missing something very obvious.

 if(body1 != null && body1.sprite != null && body1.sprite.key === 'enemy'){

   // I would want to check body2 as I do body1 but for my tile body2.sprite is NULL.


I even tried comparing with each wall body gotten from the below method -

 this.walls = this.game.physics.p2.convertTilemap(this.map, this.layers[layer.name]);

The walls collision is defined as below -
 for(var i=0; i<this.walls.length; i++){

Is there a way that I would know the collision group of body2? OR compare collision groups and not body1 and body2 in setPostBroadphaseCallback?

Please help. It's urgent.

Thanks in advance!



Link to comment
Share on other sites


  • Recently Browsing   0 members

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