Jump to content

Phaser P2 Collision Problem


Hoffenheimer97
 Share

Recommended Posts

Hi all,

I'm working on a little racing game and using this map:strecke.thumb.jpg.41af90cc71b547bba9a4903c80bbc39d.jpg

 

I'm using p2 physics. Whenever the car touches the gras it should become slower. In order to achieve that I cut out the inner gras and created a polygon shape for it.

/*Adding car*/
    car = game.add.sprite(1100,300,'car');
    car.name="car";
    car.width=100;
    car.height=175;
    game.physics.p2.enable(car);
    car.body.angle = 90;
    game.camera.follow(car);

 inner_gras = game.add.sprite(1320, 940, 'gras');
    game.physics.p2.enable(inner_gras);
    inner_gras.body.kinematic = true;
    inner_gras.body.clearShapes();
    inner_gras.body.loadPolygon('collision', 'inner_gras');
    inner_gras.name="inner_gras";

For the collision detection I use game.physics.p2.setPostBroadphaseCallback(): 

game.physics.p2.setPostBroadphaseCallback(hitGras, this);

function hitGras(body1,body2){
    if((body1.sprite.name === "inner_gras" && body2.sprite.name === "car") ||(body1.sprite.name === "car" && body2.sprite.name === "inner_gras")){
        console.log(body1);//console.log(body2.sprite);
        return true;
    }
    else{
        return false;
    }

}

The collision detection works fine, but whenever I try to do something else than returning true, for example changing the speed or something else, a contact between the car and the inner_gras sprite is detected, where they shouldn't have any contact.

It seems like the inner_gras sprite does still have the rectangular shape, although applying clearShapes().

 

Any ideas or suggestions on how to solve this problem, or what to use instead?

 

Thanks in advance!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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