Sprite collisions from different directions

I am having trouble figuring out how to handle collisions from different directions. I have a player sprite, platform spite and ground tiles.

I want the player sprite to collide with only the top of the platform sprite and when the platform sprite falls I want it to call its destroy function when it collides with the ground tiles.

This setup allows the player sprite to collide with the top and bottom of the platforms and the platforms to collide with the ground when they fall. However I was the player to jump up through the platforms.

Is there a way I can dynamically adjust the collision behavior so that when the player is jumping up it doesn’t collide with the platform?

class Platform() {
...
this.sprite.body.checkCollision.up= true;
this.sprite.body.checkCollision.down = true;
this.platform.body.checkCollision.left = false;
this.platform.body.checkCollision.right = false;
..
}

create() {
...
this.player = new Player(...)
var platform = new Platform(...)

this.physics.add.collider(this.player.sprite, platform.sprite)
this.physics.add.collider(platform.sprite, this.groundLayer, this.platGroundCollision, undefined, this);

...
}

private platGroundCollision(obj1: Phaser.GameObjects.GameObject, obj2: Phaser.GameObjects.GameObject){
obj1.destroy()
}

You have to take out the collision from the bottom platform sprite to jump through.

this.physics.add.collider(
  this.player.sprite,
  platform.sprite,
  null,
  (playerSprite) => playerSprite.body.velocity.y > 0
);
1 Like

Okay great. So I needed to use the process callback. I clearly missed that when I was looking at the method.

I am a little unclear how the playerSprite reference works in your arrow function. It seems like it is referencing the first object in the collider. Is that a keyword phaser uses to reference the first object in colliders?

Right, but when I do that the platforms can’t collide with the ground layer when they fall. I still need to check for collisions on the bottom of the platforms I just need to add some conditions on when to check for collision.

The arguments to collideCallback and processCallback are the two colliding game objects. You don’t have to use them here, though, you could use

this.physics.add.collider(
  this.player.sprite,
  platform.sprite,
  null,
  () => this.player.sprite.body.velocity.y > 0
);
1 Like