Platformer > moving platforms > body follow path vs friction

Hi,

Background - my need:

  • implement moving platforms that follow a path (from Tiled)
  • a player must move along the platform with no hackish code (ie. assigning a velocity matching that of the plaform).

Doing some research, I understand that:

  • Based on other topics (see other forum topic) the player moves with a platform only if the platform has velocity (and friction of 1 to be in sync)
  • it’s easy for a body to follow a path (as per lab example) using “set position” and a vector path.

Before I start re-implementing the wheel: do you guys know of a clean built-in way to tell a body to move to a point (and stop there) using velocity?


In less advanced engines, it was obvious to have to implement it ourselves with pseudo code like:

  • identify next point.
  • compute and apply x & y velocities.
  • test platform position every step()
  • point reached? move/stop/rewind as per config.

Seems like elementary needs for a 2D game, hence my supposing this kind of mechanism is built-in.

The following seems to basically allow the platform’s motion to be applied to the player, provided the platform’s body.friction is 1 :

called by the update method of the platform:

this.pathIndex += this._direction * this.pathSpeed * delta;
this.path.getPoint(this.pathIndex, this.pathVector);

this.scene.physics.moveTo(this, this.pathVector.x, this.pathVector.y, this._speedPxPerSecond);

This, keeping in mind to do the following when done looping/rewinding/moving:

this.setVelocity(0, 0);
this.setPosition(this.pathVector.x, this.pathVector.y);

Seems to solve the need to transfer the motion to the object standing on that platform.

Unless this approach seems wrong to you guys, I’ll mark the topic as complete.


I’m now looking into how the motion “transfers”. But it seems that the delta position is applied to the player instead of the velocity. This causes ghosting issues, but that’s another topic.

1 Like