Sprite push each other through TilemapLayer

I’m making a top-down game with a simple tilemap for the player to walk around using the keyboard. The player is followed by enemies. Collisions usually work as expected - neither enemies nor the player can pass the walls of the tilemap, nor can they pass through each other. However, when an enemy pushes the player into a wall (or vice versa), no collision is detected and the sprite being pushed goes inside the wall. How can I avoid sprites doing this to each other and breaking the tilemap collision detection?

Further details:

  • Phaser CE 2.10
  • Arcade physics. Body is enabled on player and enemies
  • Collision detection between player and tilemap layer: this.game.physics.arcade.collide(this.player, this.layer) The same approach is taken between enemies/layer, enemies/enemies and enemies/player
  • Enemy movement: this.game.physics.arcade.moveToObject(this.enemy, this.player, this.enemy.speed)

I think that may be unavoidable unless you stop the pushing somehow.

Using a nonzero body.bounce.x might solve it.

If you’re calling moveToObject() every update, do it before checking collisions.

Update player movement before checking collisions.

In the enemies/player collision callback, look at the body.blocked.* values for each sprite. That shows where it’s touching a tile, if any. I think you need to zero the velocity of the other sprite (the pushing sprite) on that axis (x for left/right, y for up/down).

Another crazy thing to try is

// After checking collisions
// For each sprite:
sprite.body.immovable = !sprite.body.blocked.none;
1 Like

You provide some creative solutions. But it turns out all I needed to do was follow your suggestion of moving sprites before checking collisions. Many thanks!

1 Like