Hoffenheimer97 Posted May 29, 2017 Share Posted May 29, 2017 Hi all, I'm working on a little racing game and using this map: 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 More sharing options...
samid737 Posted May 31, 2017 Share Posted May 31, 2017 You can try to inspect your physics bodies to see what is going on by enabling debug: https://phaser.io/examples/v2/p2-physics/body-debug Link to comment Share on other sites More sharing options...
Hoffenheimer97 Posted June 2, 2017 Author Share Posted June 2, 2017 I fixed this issue: setPostBroadphaseCallback seems to be the wrong method for what I'm trying to do. Instead I'm now working with onBeginContact() and onEndContact(). Link to comment Share on other sites More sharing options...
Recommended Posts