Sprite Collision from top only

I’m new to phaser and just trying to figure out how I can modify a collider to allow sprites to collide from one direction but not others.

I have a player sprite and a platform sprite. Currently they collide normally, the player can’t pass through the platform in any direction. What I would like is for the the player to collide with the platform when they are coming down onto it. Not when they are jumping up through it.

Looking through the documentation I see that tiles have different collide members such as collideUp which could accomplish what I want but I want my platforms to be sprites not tiles so I can animate them.

I set up a collider callback between my player sprite and my platform sprite group but I don’t know how I can dynamically change the collision behavior. Any thoughts would be apricated.

create() {

this.platformGroup = this.physics.add.group();
this.platformGroup.defaults.setAllowGravity = false;
this.platformGroup.defaults.setImmovable = true;

this.player = new Player(this, spawnPoint.x, spawnPoint.y);

this.physics.add.collider(this.player.sprite, this.platformGroup, this.handlePlayerPlatformCollision, undefined, this);

}

private handlePlayerPlatformCollision(obj1: Phaser.GameObjects.GameObject, obj2: Phaser.GameObjects.GameObject)

{
// Some kind of logic to turn on and off collision
}

:wave:

One way is

platform.body.checkCollision.down = false;

Another is

// Pass as `processCallback` in `this.physics.add.collider()`
function processPlayerPlatformCollision
(
  player: Phaser.Types.Physics.Arcade.GameObjectWithBody,
  platform: Phaser.Types.Physics.Arcade.GameObjectWithBody
)
{
  return (player.body.velocity.y > 0);
}
2 Likes

Great, thanks for the help. This worked exactly like I wanted. I didn’t even see that property in the body.