Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

p2 groups not colliding. Pls check if this short code is right.


kriket
 Share

Recommended Posts




My p2 groups are not colliding. Player just passes through stationary platform, even though he shouldnt. Pls check if this short code is right.

 

 




  create: function() {
    this.game.physics.p2.setImpactEvents(true);
    this.game.physics.p2.restitution = 0.5;
    this.game.physics.p2.gravity.y = 300;

    this.playerCollisionGroup = this.game.physics.p2.createCollisionGroup();
    this.stationaryCollisionGroup = this.game.physics.p2.createCollisionGroup();

    //  Platforms that don't move
    this.stationary = this.game.add.group();
    this.stationary.enableBody = true;
    this.stationary.physicsBodyType = Phaser.Physics.P2JS;

    this.stationary.create(0, 96, 'platform');
    this.stationary.create(632, 200, 'platform');
    this.stationary.create(400, 400, 'platform');

    this.stationary.setAll('body.setRectangle(200, 200)', false);
    this.stationary.setAll('body.static', true);      
    this.stationary.setAll('body.setCollisionGroup(stationaryCollisionGroup)', true);
    this.stationary.setAll('body.collides([stationaryCollisionGroup, playerCollisionGroup])', true);


    this.player = this.game.add.sprite(this.game.width/2, this.game.height/2, 'player', 'front.png');    
    this.player.scale.set(0.35);
    this.player.smoothed = false;
    this.game.physics.p2.enable(this.player);
    this.player.body.setRectangle(50, 50);
    this.player.body.fixedRotation = true;

    this.player.body.setCollisionGroup(this.playerCollisionGroup);

    this.player.body.collides(this.stationaryCollisionGroup, this.hitFunc, this);

    this.player.body.gravity.y = 50;
    this.player.body.fixedRotation = true;


}


 




Link to comment
Share on other sites

So, you are creating a group and enabling the body on that group. Shouldn't you enable the body on each element form the group instead?

 

But in the official example, http://phaser.io/examples/v2/p2-physics/collision-groups     enabling physics on group worked fine. 

 

    var pandas = game.add.group();    pandas.enableBody = true;    pandas.physicsBodyType = Phaser.Physics.P2JS;
Link to comment
Share on other sites

Those variables are not defined: [stationaryCollisionGroup, playerCollisionGroup]

Don't you get any errors in the console? Either use this.stationaryCollisionGroup or also create the local variables using var.

 

no, didnt get any errors in console. Game runs fine. Just no collision between player and platform. Although, I thought you had caught my mistake, but even changing to this.stationaryCollisionGroup doesnt work. Now that is surprising. No errors still.

 

 

    this.stationary.setAll('body.setCollisionGroup(this.stationaryCollisionGroup)', true);    this.stationary.setAll('body.collides([this.stationaryCollisionGroup, this.playerCollisionGroup])', true);

 

 

PS - uploaded to dropbox so u can just run it locally using Brackets

https://www.dropbox.com/sh/ip4j2byxb8c96wl/AACo3qSvWOnxX5kyluoerk3Ya?dl=0

Link to comment
Share on other sites

Because you lose this context.

var _this = this;this.stationary.setAll('body.setCollisionGroup(_this.stationaryCollisionGroup)', true);this.stationary.setAll('body.collides([this.stationaryCollisionGroup, _this.playerCollisionGroup])', true);

What about doing this? Or you can still try making a local reference of the groups using var.

Link to comment
Share on other sites

Because you lose this context.

var _this = this;this.stationary.setAll('body.setCollisionGroup(_this.stationaryCollisionGroup)', true);this.stationary.setAll('body.collides([this.stationaryCollisionGroup, _this.playerCollisionGroup])', true);

What about doing this? Or you can still try making a local reference of the groups using var.

 

 

Didnt work with _this. Still no collision. No error either. Yeah, will try and do it using var and see what happens. I suppose, I just wanted to see why it wasnt working with this

Link to comment
Share on other sites

WOW, this is weird. Even with local vars, there's no collision. No errors. 

 

 

    var playerCollisionGroup = this.game.physics.p2.createCollisionGroup();    var stationaryCollisionGroup = this.game.physics.p2.createCollisionGroup();    this.stationary = this.game.add.group();    this.stationary.enableBody = true;    this.stationary.physicsBodyType = Phaser.Physics.P2JS;    this.stationary.create(0, 96, 'platform');    this.stationary.create(632, 200, 'platform');    this.stationary.create(400, 400, 'platform');    this.stationary.setAll('body.setRectangle(200, 200)', false);    this.stationary.setAll('body.static', true);          this.stationary.setAll('body.setCollisionGroup(stationaryCollisionGroup)', true);    this.stationary.setAll('body.collides([stationaryCollisionGroup, playerCollisionGroup])', true);          this.player = this.game.add.sprite(this.game.width/2, this.game.height/2, 'player', 'front.png');          this.player.scale.set(0.35);    this.player.smoothed = false;    this.game.physics.p2.enable(this.player);    this.player.body.setRectangle(50, 50);    this.player.body.fixedRotation = true;    this.player.body.setCollisionGroup(playerCollisionGroup);
    this.player.body.collides(stationaryCollisionGroup);
Link to comment
Share on other sites

 

Add these lines:

this.game.physics.startSystem(Phaser.Physics.P2JS);this.game.physics.p2.setImpactEvents(true);

Hi, how are you mate? Both lines are already in there. Second line in create function as you can see in OP. First line, is in the boot state. But I also just tried adding it in create func and still no collision

Link to comment
Share on other sites

 

My p2 groups are not colliding. Player just passes through stationary platform, even though he shouldnt. Pls check if this short code is right.
 
 
  create: function() {    this.game.physics.p2.setImpactEvents(true);    this.game.physics.p2.restitution = 0.5;    this.game.physics.p2.gravity.y = 300;    this.playerCollisionGroup = this.game.physics.p2.createCollisionGroup();    this.stationaryCollisionGroup = this.game.physics.p2.createCollisionGroup();    //  Platforms that don't move    this.stationary = this.game.add.group();    this.stationary.enableBody = true;    this.stationary.physicsBodyType = Phaser.Physics.P2JS;    this.stationary.create(0, 96, 'platform');    this.stationary.create(632, 200, 'platform');    this.stationary.create(400, 400, 'platform');    this.stationary.setAll('body.setRectangle(200, 200)', false);    this.stationary.setAll('body.static', true);          this.stationary.setAll('body.setCollisionGroup(this.stationaryCollisionGroup)', true);    this.stationary.setAll('body.collides([this.stationaryCollisionGroup, this.playerCollisionGroup])', true);    this.player = this.game.add.sprite(this.game.width/2, this.game.height/2, 'player', 'front.png');        this.player.scale.set(0.35);    this.player.smoothed = false;    this.game.physics.p2.enable(this.player);    this.player.body.setRectangle(50, 50);    this.player.body.fixedRotation = true;    this.player.body.setCollisionGroup(this.playerCollisionGroup);    this.player.body.collides(this.stationaryCollisionGroup, this.hitFunc, this);    this.player.body.gravity.y = 50;    this.player.body.fixedRotation = true;}
 

 

Try now. You did not add 'this.' in the key string.

Link to comment
Share on other sites

I found the problem. You are using .setAll which is for properties but .setCollisionGroup(this.stationaryCollisionGroup) is a method.

Here is a approach for your problem :

var SideScroller = SideScroller || {};SideScroller.Game = function(){};SideScroller.Game.prototype = {  preload: function() {      this.game.time.advancedTiming = true;    },  create: function() {    this.background = this.add.image(0, 0, 'pic1');    this.background2 = this.add.image(3000, 0, 'pic1');    this.background3 = this.add.image(6000, 0, 'pic1');    this.game.physics.p2.setImpactEvents(true);    this.game.physics.p2.restitution = 0.5;    this.game.physics.p2.gravity.y = 300;    this.playerCollisionGroup = this.game.physics.p2.createCollisionGroup();    this.stationaryCollisionGroup = this.game.physics.p2.createCollisionGroup();    //  Platforms that don't move    this.stationary = this.game.add.group();    this.stationary.enableBody = true;    this.stationary.physicsBodyType = Phaser.Physics.P2JS;      var array = [];    for(var i = 0; i < 3 ; i++){            array[i] = this.stationary.create(this.game.width*0.5, 300+100*i, 'platform');            array[i].body.setRectangle(100, 100);            array[i].body.kinematic = true;            array[i].body.setCollisionGroup(this.stationaryCollisionGroup);            array[i].body.collides([this.stationaryCollisionGroup, this.playerCollisionGroup]);        console.log(this.stationary);      }          this.player = this.game.add.sprite(this.game.width/2, this.game.height/2, 'player', 'front.png');        this.player.scale.set(0.35);    this.player.smoothed = false;//    this.player.animations.add('fly', [0,1,2,3,4,5], 10, true);//    this.player.play('fly');//    this.player.anchor.setTo(0.5, 1);    this.game.physics.p2.enable(this.player);//    this.player.body.setCircle(28);    this.player.body.setRectangle(50, 50);    this.player.body.fixedRotation = true;      this.player.body.dynamic = true;    this.player.body.setCollisionGroup(this.playerCollisionGroup);          this.player.body.collides(this.stationaryCollisionGroup, this.hitFunc, this);//    this.player.body.collides(stationaryCollisionGroup);    this.player.body.gravity.y = 50;    this.player.body.fixedRotation = true;      this.game.physics.p2.updateBoundsCollisionGroup();    this.player.animations.add('left', [        'L1.png',        'L2.png',    ], 15, true, false);    this.player.animations.add('right', [        'R1.png',        'R2.png',    ], 15, true, false);          this.player.animations.add('jump', [        'front.png',    ], 15, true, false);  //      CAMERA          //the camera will follow the player in the world//    this.game.camera.follow(this.player);              var edge = 100;      this.cameraDeadzone = new Phaser.Rectangle(edge, edge, this.game.camera.width - (edge * 2), this.game.camera.height - (edge * 2));      this.game.camera.focusOn(this.player);                this.cursors = this.game.input.keyboard.createCursorKeys();    this.facing = 'left';    this.jumpTimer = 0;    this.jumpButton = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);                this.game.renderer.renderSession.roundPixels = true;    //    this.player.body.collideWorldBounds = true;  },              update: function() {                      if (true) {	   var cam = this.game.camera;	   var player = this.player;	   var hEdge = player.x - cam.x;	   var vEdge = player.y - cam.y;//	   if (hEdge < this.cameraDeadzone.left || hEdge > this.cameraDeadzone.right || vEdge < this.cameraDeadzone.top || vEdge > this.cameraDeadzone.bottom) {	   if (hEdge < this.cameraDeadzone.left || hEdge > this.cameraDeadzone.right) {		  var camCenter = { x: cam.x + (cam.width / 2), y: cam.y + (cam.height / 2) };		  var diff = Phaser.Point.subtract(player, camCenter);		  cam.x += diff.x * 1.8;		  cam.y += diff.y * 1.8;        }    }      /*      if(this.cursors.left.isDown)       {        this.player.body.moveLeft(200);        if (this.facing != 'left')        {            this.facing = 'left';        }      }      else if (this.cursors.right.isDown)      {        this.player.body.moveRight(200);        if (this.facing != 'right')        {            this.facing = 'right';        }      }      else      {        this.player.body.velocity.x = 0;      }          if (this.jumpButton.isDown && this.game.time.now > this.jumpTimer && this.checkIfCanJump())    {        this.player.body.moveUp(300);        this.jumpTimer = this.game.time.now + 750;    }*/      this.input.onDown.add(this.inputControls, this);  },        checkIfCanJump: function() {        var yAxis = p2.vec2.fromValues(0, 1);        var result = false;        for (var i = 0; i < this.game.physics.p2.world.narrowphase.contactEquations.length; i++)        {            var c = this.game.physics.p2.world.narrowphase.contactEquations[i];            if (c.bodyA === this.player.body.data || c.bodyB === this.player.body.data)            {                var d = p2.vec2.dot(c.normalA, yAxis); // Normal dot Y-axis                if (c.bodyA === this.player.body.data) d *= -1;                if (d > 0.5) result = true;            }        }            return result;    },      inputControls: function(pointer) {        if(pointer.x < this.game.width/2 && pointer.isDown)        {                this.player.body.moveLeft(200);                if (this.facing != 'left')                {                    this.facing = 'left';                }        }        else if(pointer.x >= this.game.width/2 && pointer.isDown)        {                this.player.body.moveRight(200);                if (this.facing != 'right')                {                    this.facing = 'right';                }        }        else        {            this.player.body.velocity.x = 0;        }      },            hitFunc: function() {//        this.game.debug.body(this.player);    },        render: function() {//        this.game.debug.body(this.player);    },        };
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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