Matter Physics in Phaser v3.60

I don’t really use Matter and I don’t have a 144Hz monitor :smiley: but here are my impressions.

By default Matter makes a variable-sized step using the minimum delta from the last 60 frames, clamped within 16–33ms:

These values are fine for browser animation rates of 30–60fps but above or below that Matter will be running ahead or behind the game step rate.

You could limit Phaser’s frame rate to 60fps also:

new Phaser.Game({
  fps: {
    forceSetTimeOut: true,
    target: 60
  },
  physics: {
    default: 'matter',
    matter: {
      runner: {
        fps: 60
      }
    }
  }
});

Or you could have Matter use Phaser’s delta:

new Phaser.Game({
  physics: {
    default: 'matter',
    matter: {
      getDelta: function () {
        return this.scene.game.loop.delta;
      },
      runner: {
        // `isFixed` means use `getDelta()`
        isFixed: true
      }
    }
  }
});

You probably don’t want to enable matter.runner.isFixed by itself, because that will make Matter run a fixed-size step (60Hz) no matter what Phaser’s delta is. Likewise with set60Hz() and set30Hz().

A good solution might be to turn off Matter’s autoUpdate and step through the game loop delta manually at a fixed rate:

:warning: Beware that applyForce() and thrust() are still timestep dependent.

1 Like

Here’s a small Scene Plugin that runs a fixed-step Matter loop:

1 Like

Thanks for the insightful suggestions. I don’t have a 144Hz monitor myself, but I’m encountering issues with MatterJs running twice as fast on some monitors. I have a few questions, I hope you can help clarify.

  1. :warning: Beware that applyForce() and thrust() are still timestep dependent. Does it mean that setting a fixed framerate to 60fps won’t solve the issue with MatterJs running faster on >= 120Hz monitors if we are using applyForce() or thrust() in the game?
  2. Do we have to set forceSetTimeOut:true, to run Phaser at target 60fps?

I think the problem with applyForce() and thrust() is that the same values can have different effects depending on the size of Matter’s timestep. But the problem of Matter running “too fast” isn’t that; that’s from the delta error when Matter clamps deltas below 16ms to 16ms (60 fps).

{ forceSetTimeOut: true } is the best way to limit Phaser’s loop to 60 fps. But you don’t have to do that. You can use the third solution above.

1 Like

Thanks for the additional explanation.
I tried the scene plugin fix, and it worked.