matter-js icon indicating copy to clipboard operation
matter-js copied to clipboard

Different simulation speed on higher refresh rate monitors

Open BeckettOBrien opened this issue 3 years ago • 8 comments

Hi! I've been playing around with matter-js and noticed that when I switched from my laptop to a high-refresh rate display, the speed of objects falling under gravity increased dramatically. When I change the monitor settings down to 60 Hz, the object falls normally. I tried using this when creating my runner:

runner = Runner.create({
    isFixed: true
});

but the simulation looks the same. Is there anything I'm missing? Thanks!

BeckettOBrien avatar Feb 08 '21 18:02 BeckettOBrien

@BeckettOBrien can you provide full code or at least part with renderer?

wokalek-work avatar Feb 13 '21 00:02 wokalek-work

Yeah that looks like it should work, could you share a minimal working example?

liabru avatar Feb 14 '21 21:02 liabru

I recorded an example on the matter-tools demo and I added a breakpoint before starting the runner and set isFixed to true. It isn't noticable in the video, but I changed my monitor's refresh rate from 240 to 60 Hz in between both example drops (around 10-12 seconds into the video). I also tried changing my G-SYNC settings between fullscreen, windowed, and off, but nothing changed. If you need any more information, I'd be happy to help!

https://user-images.githubusercontent.com/44044350/107902034-2a91a700-6efa-11eb-887a-514129c9b1ca.mov

BeckettOBrien avatar Feb 15 '21 03:02 BeckettOBrien

Ah I see - what you need to do is make sure you set the correct delta e.g. runner.delta = 1000 / 240.

You'll likely need to detect the frame rate of the browser though for it to be portable, or a better solution is to implement frame skipping and limit it to e.g. 60fps fixed.

Matter.Runner doesn't yet have these features as it is designed for debugging and simple uses, but I do hope to get around to it now that high frequency monitors are becoming common.

liabru avatar Feb 15 '21 21:02 liabru

I started messing around and found that if I comment out the delta limiting (deltaMin and deltaMax) it seems to work out. The odd thing is that if I just set the deltaMin and deltaMax to values that would make it just not matter, it doesn't seem to do anything. And even if it did, would this come with any unexpected behavior? Also, I saw that there is a pull request open for fixing timing/frame rate inconsistencies: #777, would this be one of the issues that gets solved in that pull request?

BeckettOBrien avatar Feb 17 '21 17:02 BeckettOBrien

You could also try Engine.Update() https://brm.io/matter-js/docs/classes/Engine.html#method_update

//put in the main animation loop, returns an array with [FPS, delta, lastCalledTime]

function getCurrentFPS(lastCalledTime) {
     if(!lastCalledTime) {
        lastCalledTime = Date.now();
        fps = 0;
        return [fps, 0, lastCalledTime];
     }
     delta = (Date.now() - lastCalledTime)/1000;
     lastCalledTime = Date.now();
     return [1/delta, delta, lastCalledTime];
}

and you could use it like this:

var {Engine} = Matter;

var engine = Engine.Create();

var lastCalledTime;
var fps = 0;
var delta = 0;

function getCurrentFPS(lastCalledTime) {
     if(!lastCalledTime) {
        lastCalledTime = Date.now();
        fps = 0;
        return [fps, 0, lastCalledTime];
     }
     delta = (Date.now() - lastCalledTime)/1000;
     lastCalledTime = Date.now();
     return [1/delta, delta, lastCalledTime];
}

function MainLoop() {
   // Get "getCurrentFPS"'s Output
   GCFPSOutput = getCurrentFPS(lastCalledTime);
   
   // Set fps, delta, and lastCalledTime
   FPS = GCFPSOutput[0];
   delta = GCFPSOutput[1];
   lastCalledTime = GCFPSOutput[2];
   
   // Updates engine with delta
   Engine.Update(engine, delta*1000);
}

ErrorBot1122 avatar Feb 25 '21 05:02 ErrorBot1122

Hey, the solutions above didn't work for me. Are there any updates on this?

fcisio avatar May 02 '22 19:05 fcisio

sorry, I commented on this so long ago, that I forgot how it works :p

ErrorBot1122 avatar May 15 '22 00:05 ErrorBot1122

Closing this thread to refer to updates in #702 on this topic, thanks again for the reports here.

liabru avatar Nov 12 '23 19:11 liabru