Phaser - Arcade collision physics

719 views Asked by At

I'm working on a simple tile game using the Phaser framework, but unfortunately I've stumbled upon a "bug" when using the Arcade collision method. I want all the tiles to perfectly stack on top of each other, but the tiles on top always go through the tiles below them.

This is the code:

var game = new Phaser.Game(700, 700, Phaser.AUTO, 'phaser-demo', {
  create: create,
  update: update
});

var tiles, textureRegistry = {};

function create() {
  game.physics.startSystem(Phaser.Physics.ARCADE);
  game.physics.arcade.gravity.y = 500;

  tiles = game.add.group();
  tiles.physicsBodyType = Phaser.Physics.ARCADE;
  tiles.enableBody = true;

  for (var i = 0; i < 10; i++) {
    for (var j = 0; j < 10; j++) {
      tiles.add(game.add.sprite(i * 70, j * 70, createBlock(64, 'red')));
    }
  }
  tiles.setAll('body.collideWorldBounds', true);
  tiles.setAll('body.bounce', new Phaser.Point(0.5, 0.5));

}

function update() {
  game.physics.arcade.collide(tiles);
}

function createBlock(size, color) {
  var name = size + '_' + color;
  if (textureRegistry[name]) {
    return textureRegistry[name];
  }

  var bmd = game.add.bitmapData(size, size);
  bmd.ctx.fillStyle = color;
  bmd.ctx.fillRect(0, 0, size, size);
  textureRegistry[name] = bmd;
  return bmd;
}
<script src="https://github.com/photonstorm/phaser/releases/download/v2.6.2/phaser.min.js"></script>

It seems to look even worse on Chrome. It's important to note that the problem only occurs when 4x4 or more tiles are used.

1

There are 1 answers

0
bfmags On BEST ANSWER

Phaser Arcade Physics doesn't handle multi-body contact very well, due to limitations on current version

Alternatively consider using P2 physics instead and/or see discussions below.