Arcade Physics Friction not Working as Expected

Hello! I am creating a platformer in Phaser 3 and am currently working on implementing moving platforms. Reading the documentations/tutorials, it seems like setting the body.friction values to 1 -should- allow my player to ride the platforms as they move. Unfortunately, they seem to slide right by and my player remains static. Is this intended, or do I have a setting incorrect somewhere?

Relevant code:

//The moving platform
m_moving = this.physics.add.group();

movingPhysics = m_moving.create(platform.x, platform.y, CONST_MOVING_KEY).setOrigin(0, 0).setScale(platform.width, platform.height);
movingPhysics.body.allowGravity = false;
movingPhysics.body.immovable = true;
movingPhysics.body.friction.x = 1;
movingPhysics.body.friction.y = 1;

//The player
playerPhysics = this.physics.add.sprite(0, 0, CONST_PLAYER_KEY);
this.physics.add.collider(playerPhysics, m_moving)

//Player Movement (Inside a custom player class, so gameObject is the Phaser Sprite
	if(this.gameObject.body.touching.down)
	{

		this.gameObject.setVelocityX(0);

	}

	if(this.control.Left() && this.gameObject.body.touching.down)
	{

		this.gameObject.setVelocityX(-this.horizontalVelocity);

	}

	if(this.control.Right() && this.gameObject.body.touching.down)
	{

		this.gameObject.setVelocityX(this.horizontalVelocity);

	}


	if(this.control.Jump() && this.gameObject.body.touching.down)
	{

		this.gameObject.setVelocityY(this.jumpVelocity);

	}

//Moving Platform movement (Also inside a custom object)
MovingPlatform.prototype.Update = function()
{
    
    var time = 0.001 * MovingPlatform.timer.RunningMilliseconds();
    
    this.gameObject.x = this.xParam(time); //Custom parametrization functions which move the platform
    this.gameObject.y = this.yParam(time);
    
}

Hi @basstabs ,

I think the horizontal velocity of the player should be equal to the platform velocity, but that work should be done by the friction itself.

Don’t change the position of the platforms directly, modify their velocities to move them if you are going to use the physics engine with them (this is valid for any game object).

Greetings.

1 Like

Thanks for the quick response!

Is it possible that by setting the velocity to 0, I am overriding whatever velocity is set as a result of the friction? I wouldn’t think that would be the case, but I am new to how Phaser works.

Okay, thank you for that! I would like to have very fine control over where platforms are and when, which is why I was setting the positions directly. If I set velocity using derivatives, I am worried that timing/positioning could come unraveled due to rounding issues and frames not aligning exactly on full seconds. Is there a way to properly set the positions so that physics works properly, or is velocity the only way?

Edit: I forgot to mention that changing this to velocity does lead to the friction working as expected, so I’ll move forward with that implementation and just watch to make sure that platforms don’t get too far off course. Thanks a million!

I think this is probably preventing the friction from working.

You can try calculating the position delta in MovingPlatform.prototype.Update, saving it, then adding it to the sprite in a collision callback.

1 Like

Yes, I changed it to work based off of velocity rather than position and it works perfectly. I had briefly messed around with collision callbacks and locking the player to platforms and the like based on a few tutorials floating around, but I’d really like all of the collision behavior to be handled natively by Phaser as much as possible.

You can use velocity and also correct the sprite’s position during postupdate: