Jump to content

P2 collision groups exceptions


flow
 Share

Recommended Posts

Hi everybody, 

I'm working on my first Phaser based game and so far it's really a lot of fun :) 

One thing that keeps giving me some headaches from time to time though is that P2 physics is mostly incompatible with arcade and many tutorials only work with arcade. That seems to start with the sprite bitmaps where everything is rotated 90 degrees :wacko:

Usually I find some workarounds or I've replaced stuff completely like the Weapon plugin, but now I'm facing an issue with collisions where I'm not quite sure what's the best solution:

Let's assume we have 2 player sprites and 2 enemy sprites and each of them has some bullets to fire. So we create 2 collision groups for the sprites and 2 for the bullets:

var playerCG = game.physics.p2.createCollisionGroup();
var enemyCG = game.physics.p2.createCollisionGroup();
var playerBulletsCG = game.physics.p2.createCollisionGroup();
var enemyBulletsCG = game.physics.p2.createCollisionGroup();

player1.body.setCollisionGroup(playerCG);
player1.body.collides([enemyCG, enemyBulletsCG]);
p1Bullets.body.setCollisionGroup(playerBulletsCG);
p1Bullets.body.collides([enemyCG]);

player2.body.setCollisionGroup(playerCG);
player2.body.collides([enemyCG, enemyBulletsCG]);
p2Bullets.body.setCollisionGroup(playerBulletsCG);
p2Bullets.body.collides([enemyCG]);

enemy1.body.setCollisionGroup(enemyCG);
enemy1.body.collides([playerCG, playerBulletsCG]);
e1Bullets.body.setCollisionGroup(enemyBulletsCG);
e1Bullets.body.collides([playerCG]);

enemy2.body.setCollisionGroup(enemyCG);
enemy2.body.collides([playerCG, playerBulletsCG]);
e2Bullets.body.setCollisionGroup(enemyBulletsCG);
e2Bullets.body.collides([playerCG]);

So far so good, that works fine.

But now we want to change the play-mode and want to activate "friendly fire" and "friendly collisions".

If we add the playerBulletsCG to player1.body.collides(...) and playerCG to p1Bullets.body.collides(...) we get a problem when the bullets are fired from withing the collision box of a player because the players own bullets collide now with his sprite :-(
Obviously we could create additional groups like player1CG, player2CG and player1BulletsCG, player2BulletsCG, but then we also have to add all these groups to the enemies and we need to come up with a whole new structure of initializing all these groups in case we want to support 4 players or more.

So I was wondering if there is any method that says "collide with anything but your own bullets"?

I'm open for any suggestions :-)

Link to comment
Share on other sites

[UPDATE]

Something I found in one of the P2 examples that might be the solution I was looking for :)

//apply a filter before the narrow collision check starts
game.physics.p2.setPostBroadphaseCallback(filterCollisions, this);

//use a custom "ownerId" value to check if both come from the same entity (player/npc)
function filterCollisions(p2BodyA, p2BodyB) {
    if (p2BodyA && p2BodyB && p2BodyA.sprite.ownerId && p2BodyB.sprite.ownerId){
        if (p2BodyA.sprite.ownerId == p2BodyB.sprite.ownerId){
                return false;
        }
    }
    return true;
}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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