Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phaser - Arcade collision physics

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.

like image 678
OCastle Avatar asked Aug 24 '26 16:08

OCastle


1 Answers

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.


  • https://github.com/photonstorm/phaser/issues/1513

  • http://www.html5gamedevs.com/topic/8191-vertical-sprite-collision-overlapfallthrough/

  • http://www.html5gamedevs.com/topic/7810-how-to-increase-rigidity-of-arcade-sprite-bodies/

    Yes, this is a shortcoming in the separation code of Arcade Physics, which was not designed for this kind of multi-body contact. The more things are stacked on top of one another, the more weird it gets. @lewster32

like image 197
bfmags Avatar answered Aug 27 '26 05:08

bfmags