phaser icon indicating copy to clipboard operation
phaser copied to clipboard

Unable to make Matter's setAngularVelocity framerate-independent

Open dreasgrech opened this issue 1 year ago • 6 comments

Version

  • Phaser Version: Phaser 3.6.0
  • Operating system: Windows 10
  • Browser: Google Chrome v.112.0.5615.138 (64bit)

Description

Encountered an issue while trying to make Matter's setAngularVelocity framerate-independent.

Example Test Code

I made two examples with calling setAngularVelocity on a Matter image but I'm noticing the example with the lower framerate rotates faster than the one with the higher framerate and I'm having a hard time to get them to take the same amount of time for one full revolution.

This example is capped at 144fps: https://jsfiddle.net/hgm25qrx/

and this one is capped at 30fps: https://jsfiddle.net/puhL7s9n/

On my machines, the 30fps example rotates the image faster than the 144fps example.


I also created two other examples, same 30fps and 144fps, which do not use the delta time (hence tied to the framerate) to check if setAngularVelocity is already framerate-independent but that does not seem to be the case either:

Examples with no deltaTime: 144fps: https://jsfiddle.net/9nswp3fr/ 30fps: https://jsfiddle.net/adk9gue6/

dreasgrech avatar May 04 '23 16:05 dreasgrech

From my experience, Matter seems to ignore FPS limits altogether. In my game, I made sure it follows delta time this way:

In my create() function: this.matter.world.autoUpdate = false;

And then my update function:

update (time,delta) {
	this.matter.world.step(delta);
}

Baegus avatar May 24 '23 07:05 Baegus

From my experience, Matter seems to ignore FPS limits altogether. In my game, I made sure it follows delta time this way:

In my create() function: this.matter.world.autoUpdate = false;

And then my update function:

update (time,delta) {
	this.matter.world.step(delta);
}

Thanks for your reply and it seems like you're right.

I've created another two examples, now with manual stepping and they seem to be much more consistent with each other: 144fps: https://jsfiddle.net/wetb2gda/ 30fps: https://jsfiddle.net/9uc7apgz/

One thing though is that I when I tried feeding in the delta time like in your example, things didn't work as expected. But it does seem to work alright when using the engine defaults (https://newdocs.phaser.io/docs/3.52.0/Phaser.Physics.Matter.World#step)

dreasgrech avatar May 27 '23 01:05 dreasgrech

I think this can be done with

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

samme avatar Jun 15 '23 20:06 samme

One thing though is that I when I tried feeding in the delta time like in your example, things didn't work as expected. But it does seem to work alright when using the engine defaults (https://newdocs.phaser.io/docs/3.52.0/Phaser.Physics.Matter.World#step)

Interesting, I've tried this and it acts just as if I didn't do anything (simulation is too fast on higher refresh rates). When I pass the delta time, the speed is consistent.

I think this can be done with

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

This seems to work fine.

Baegus avatar Jun 27 '23 11:06 Baegus

I just came across another related bug - even if the physics are tied to delta using one of the methods mentioned above, the thrust() functions are still tied to the framerate and you have to adjust the thrust value accordingly (factoring in scene.game.loop.delta).

Baegus avatar Jul 18 '23 10:07 Baegus

thrust() and applyForce() are still timestep dependent.

samme avatar Jul 18 '23 18:07 samme

Going to close this issue as it's just the way Matter works. If they resolve it upstream, we'll integrate the fix here too. I wouldn't hold your breath, though.

photonstorm avatar Feb 20 '24 15:02 photonstorm