Jump to content

[Solved]Collision detection problem for P2 plugin


khleug35
 Share

Recommended Posts

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;    
        }
    }
});   

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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);
  }
});

 

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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