rorymcg

P2 sprite collision group pain

Recommended Posts

Hi all,

Been banging my head against the wall for ages with this.

Default P2 collision works, I clearShapes() on each sprite to get rid of these boundary boxes and loadPolygon() to get my custom boundaries (defined in sprite_physics.json). However the collision groups appear not to be working as my player sprite goes straight through/under the enemy.

Can anyone see where I'm going wrong?

 

// in index.html
(function() {
    var game = new Phaser.Game(1024, 768, Phaser.AUTO, null)
    game.state.add('Game', Game);
    game.state.start('Game');
})();



// in game.js
var Game = function (game) {
};

Game.prototype = {

    preload: function () {
        this.game.load.image('player', 'assets/player.png');
        this.game.load.image('enemy', 'assets/enemy.png');
        this.game.load.physics('sprite_physics', 'assets/sprite_physics.json');
    },

    create: function () {
        this.game.physics.startSystem(Phaser.Physics.P2JS);

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

        this.createPlayer();
        this.createEnemy();
    },

    update: function () {
        // <snip> do some steering stuff </snip>
    },

    createPlayer: function () {
        this.player = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY + 300, 'player');

        this.game.physics.p2.enable(this.player, true); // so I can see the polygon's boundaries

        // Gets rid of current bounding box
        this.player.body.clearShapes();
        // BUT THEN need to add collision cos default p2 collision is wiped with clearShapes()

        // Add boundary shape from PhysicsEditor
        this.player.body.loadPolygon('sprite_physics', 'player');

        // Seems to do nothing :(
        this.player.body.setCollisionGroup(this.playerCollisionGroup);
        this.player.body.collides([
           this.enemyCollisionGroup
        ]);
    },

    createEnemy: function () {
        this.enemy = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'enemy');

        this.game.physics.p2.enable(this.enemy, true);

        this.enemy.body.clearShapes();

        this.enemy.body.loadPolygon('sprite_physics', 'enemy');

        this.enemy.body.setCollisionGroup(this.enemyCollisionGroup);
        this.enemy.body.collides([
           this.playerCollisionGroup
        ]);
    }
}

 

Share this post


Link to post
Share on other sites

THANK YOU.

I just downloaded phaser.min.js 2.9.1 and it fixed everything.

I'm kind of pissed off I spent that much time trying to figure something out when it was a massive live defect but I'm mostly grateful for this community (you, Samid737) and phaser's library working with a patch. I would recommend the Phaser team to be more vocal with their fundamental hotfixes....

Or am I missing something in terms of their release exposure?

 

 

 

Share this post


Link to post
Share on other sites

Ironically, I introduced the bug. The complete story #279, The underlying problem is yet to be resolved, but its not A critical one. This was an unexpected bug as you can see. The Phaser-CE community does its best to foresee and are pretty busy maintaining, but quality control is definitely an added challenge with community driven projects.

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.