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