How to create precise platformer movement using MatterJS?

61 Views Asked by At

I'm using MatterJS to create a simple tile-based platformer, but running into issues getting both gravity and movement to work together.

My goal is to have it so the player is only partially physics driven, where they are affected by gravity, collide with walls, and push other physics objects around, but at the same time have "snappy," left/right movement, where the instant the direction isn't being held, the player stops moving in that direction.

Things I've tried and the results:

  • Using Matter.Body.setVelocity() resulted in the object moving long after I released the button.
  • Using Matter.Body.setVelocity() and then trying to clear the velocity in afterUpdate means gravity stops being applied correctly.
  • Using Matter.Body.translate() for L/R movement resulted in almost exactly what I wanted, except the player would always lightly clip into walls, causing them to get stuck instead of sliding down.
  • Using Matter.Body.applyForce() in beforeUpdate() to apply the direction vector resulted in the object still moving after I released the button, and moving at different speeds depending on if it was on the ground or not.
  • Using Matter.Body.applyForce() in beforeUpdate() and attempting to apply an equal but opposite force in afterUpdate() resulted in some really strange behavior.

Any idea how I would go about implementing this?

0

There are 0 best solutions below