Warning: I'm not a native English speaker, the following explanation may contain grammar or spell errors, I will try my best to make myself clear.
Another warning: the following explanation needs programming knowledge to understand.
Spoiler: explanation
1. How RMMV works
RMMV is a frame based game engine, which, in every game loop, you won't receive a delta time, the interval between each update is *considered* fixed. (not fixed in the real world, but if you are inside of the update method, you can not tell.)
JavaScript:
SceneManager.updateMain = function() {
if (Utils.isMobileSafari()) {
this.changeScene();
this.updateScene();
} else {
var newTime = this._getTimeInMsWithoutMobileSafari();
var fTime = (newTime - this._currentTime) / 1000;
if (fTime > 0.25) fTime = 0.25;
this._currentTime = newTime;
this._accumulator += fTime;
const old_accu = this._accumulator;
let i = 0;
while (this._accumulator >= this._deltaTime) {
++i;
this.updateInputData();
this.changeScene();
this.updateScene();
this._accumulator -= this._deltaTime;
}
if(i !== 1){
console.log(i, fTime, old_accu);
}
}
this.renderScene();
this.requestUpdate();
};
复制代码
This is the updateMain method of SceneManager(I made some modification to log some important info).
This method will be called every time browser wants to repaint the page (since this method is a part of the callback of the requestAnimationFrame: https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame), if vSync is enabled (it is enabled by default), it means this method will be called 60 times a second (assume your monitor's refreshrate is 60Hz)
I will not explain the code since it is self-explanatory(I hope), and my English skill does not allow me to explain it clearer.
Just notice here:
console.log(i, fTime, old_accu);
复制代码
The i here means the times of the updateScene is called.
2. Why jitter happens
When jitter happens, you will notice something like this is printed in the console.
Code:
2 0.01699999999254942 0.033333333612730484
0 0.015999999828636646 0.01633333343391655
2 0.018000000156462193 0.03433333359037874
0 0.016000000294297934 0.01633333371331332
2 0.01699999999254942 0.033333333705862736
0 0.015999999828636646 0.016333333527048802
2 0.018000000156462193 0.03433333368351099
0 0.015999999828636646 0.016333333340784285
2 0.01699999999254942 0.0333333333333337
0 0.015999999828636646 0.01633333343391654
2 0.018000000156462193 0.03433333359037873
0 0.016000000294297934 0.016333333713313313
2 0.01699999999254942 0.033333333705862736
0 0.016000000294297934 0.01633333362018106
2 0.01699999999254942 0.033333333612730484
0 0.01500000013038516 0.01633333343391655
2 0.01699999999254942 0.033333333426465966
0 0.01500000013038516 0.016333333433916554
2 0.018000000156462193 0.034333333590378746
0 0.015999999828636646 0.016333333527048813
2 0.01699999999254942 0.03333333351959823
0 0.014999999664723873 0.0163333333407843
2 0.01699999999254942 0.033333333333333715
0 0.016000000294297934 0.016333333620181068
2 0.01699999999254942 0.033333333612730484
0 0.01500000013038516 0.01633333343391655
2 0.01699999999254942 0.033333333426465966
0 0.01500000013038516 0.01633333371331332
2 0.01699999999254942 0.033333333705862736
0 0.015999999828636646 0.016333333340784288
2 0.01699999999254942 0.03333333333333371
0 0.01500000013038516 0.01633333362018106
2 0.01699999999254942 0.033333333612730484
0 0.015999999828636646 0.01633333343391655
2 0.01699999999254942 0.033333333426465966
0 0.016000000294297934 0.01633333371331332
复制代码
The first number 0,2 means in this frame, updateScene is called zero times/twice.
Now, you will see the reason here. Every time updateScene is called, the map is scrolled a bit. If we scroll it twice in a frame, then don't scroll it in next frame, the scroll speed is not fixed, which looks stuttering.
Then, why we got this? Let's look at the seond number, the deltaTime between each call of updateMain.
Ideally, the deltaTime between each call to updateMain should be 1/60, which is 0.016666666
But we seldomly got the ideal value. Instead, we got 0.015 and 0.017, this unstable delta time is the root of the all evil.
The reason why the delta time is unstable is still unclear (A reasonable guess is that before updateMain, MV also called updateManagers, which consumes unstable time)., but I found that requestAnimationFrame also gives a timestamp as argument, and the delta between these timestamps are stable.
So the solution is simple: use the argument requestAnimationFrame passed to calculate the delta time.
Note
I haven't encounter this jitter issue after this plugin was enabled, if you still have this issue, you can change the
const DEBUG = false;
复制代码
to
const DEBUG = true;
复制代码
and watch the console, check whether it prints something like the output I paused above.
also:
If your monitor's refresh rate is not 60Hz, this jitter issue can not be fixed, unless you speed up your game.