khleug35 Posted July 6, 2019 Share Posted July 6, 2019 How to detect collision between two objects in P2 Plugin?? I try to use the following code, but not work When collisionGroup 0 collide collisionGroup 1, It will trigger events (eg: hurt, tint, heart HP etc......) this.body.collisionGroup = 0; this.body.collideAgainst = [1]; this.body.collide = this.collide.bind(this); collide: function(body){ if (body.collisionGroup === 1) { console.log("You touch something"); } }, Anyidea check collision between two objects or check overlap in P2 Physics??? Thank you very much My full code of player class: game.createClass('Player', { init: function(x, y) { this.classname = 'playerclass'; this.sprite = new game.Sprite('panda.png'); this.sprite.position.set(x, y); this.sprite.anchorCenter(); this.sprite.addTo(game.scene.stage); this.body = new game.Body({ mass: 1, fixedRotation: false, position: [ this.sprite.position.x / game.scene.world.ratio, this.sprite.position.y / game.scene.world.ratio ], }); var shape = new p2.Circle({ radius: this.sprite.width / 2 / game.scene.world.ratio }); /* For Box var shape = new p2.Box({ width: this.sprite.width/ game.scene.world.ratio, height: this.sprite.height/ game.scene.world.ratio }); */ this.body.addShape(shape); this.body.collisionGroup = 0; this.body.collideAgainst = [1]; this.body.addTo(game.scene.world); this.body.collide = this.collide.bind(this); }, collide: function(body){ if (body.collisionGroup === 1) { console.log("You touch something"); } }, update: function(){ this.sprite.position.x = this.body.position[0] * game.scene.world.ratio; this.sprite.position.y = this.body.position[1] * game.scene.world.ratio; this.sprite.rotation = this.body.angle; if (game.keyboard.down('W')) { this.body.velocity[1] = -2; } else if (game.keyboard.down('S')) { this.body.velocity[1] = 2; }else{ } if (game.keyboard.down('A')) { this.body.velocity[0] = -1; } else if (game.keyboard.down('D')) { this.body.velocity[0] = 1; } else { this.body.velocity[0] = 0; } } }); Wolfsbane 1 Quote Link to comment Share on other sites More sharing options...
khleug35 Posted July 6, 2019 Author Share Posted July 6, 2019 I find the p2 example and try to type the following code to set player to Player collisionGroup and enemy to Enemy collisionGroup But I still no idea to check two object overlap and collision this.groups = { Player: Math.pow(2, 0), Enemy: Math.pow(2, 1), GROUND: Math.pow(2, 2) }; this.shape.collisionGroup= game.scene.groups.Player; this.shape.collisionMask= game.scene.groups.Player | game.scene.groups.Enemy; From Panda2 in Arcade Physics , I can try this method game.createClass('Player', { init: function() { ... this.body.collideAgainst = [game.Body.Enemy]; this.body.collide = this.collide.bind(this); }, collide: function(other) { if (other.collisionGroup === game.Body.Enemy) { //doSomething...... } }, ..... }); From phaser 2 in P2 Physics , I can refer to this link. http://www.phaser.io/examples/v2/p2-physics/collision-groups How about Panda2 in P2 Physics , Thank you very much Quote Link to comment Share on other sites More sharing options...
Stephan Posted July 6, 2019 Share Posted July 6, 2019 hi, Have you set up the code in Mainscene correctly? It should look something like this: add world this.world = new game.Physics({ gravity: [0, 1], }); //add eventhandler collision this.world.on("beginContact",function(event){ if(typeof event.bodyA.collide !== 'undefined'){ event.bodyA.collide(event.bodyB); } if(typeof event.bodyB.collide !== 'undefined'){ event.bodyB.collide(event.bodyA); } }); khleug35 and Wolfsbane 2 Quote Link to comment Share on other sites More sharing options...
khleug35 Posted July 6, 2019 Author Share Posted July 6, 2019 @Stephan Hello, Thank you very much It work for me, I am Newbie for P2.js , I haven't set this code in Main scene Thank again!!!!!!!! Have a nice day Quote Link to comment Share on other sites More sharing options...
Stephan Posted July 6, 2019 Share Posted July 6, 2019 Great! Glad I could help you. ? khleug35 1 Quote Link to comment Share on other sites More sharing options...
khleug35 Posted July 8, 2019 Author Share Posted July 8, 2019 On 7/7/2019 at 12:47 AM, Stephan said: Great! Glad I could help you. ? Thanks , One more question Is p2 plugin in Panda2 is not support direction of collision?? eg collide(body, dir) ?? collide: function(body, dir) { if (dir === 'UP' || dir === 'DOWN') { dosomething.... } } https://www.panda2.io/docs/api/Body Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.