Last active
April 13, 2018 12:06
-
-
Save ianpaschal/16088cb6026695e00ff6c9f0b81cb5ab to your computer and use it in GitHub Desktop.
Timing Troubles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is what the most simple version of my code looks like. It's the same as using setInterval, | |
although setTimeout makes it easier to modify for part 2. | |
*/ | |
function tick() { | |
const delta = ENGINE._update(); | |
const drift = delta - ENGINE._step; | |
console.log( "Delta:", delta, "Drift:", drift ); | |
setTimeout( tick, ENGINE._step ); | |
} | |
tick(); | |
ENGINE._update = function() { | |
const now = Present(); | |
const delta = now - ENGINE._lastTickTime; | |
ENGINE._lastTickTime = now; | |
// Do some stuff... | |
return delta; | |
} | |
/* OUTPUT for this portion: | |
[08:29:27.777] [LOG] Delta: 100.37304598093033 Drift: 0.37304598093032837 | |
[08:29:27.881] [LOG] Delta: 104.36562204360962 Drift: 4.365622043609619 | |
[08:29:27.982] [LOG] Delta: 100.43001997470856 Drift: 0.43001997470855713 | |
[08:29:28.085] [LOG] Delta: 102.98507702350616 Drift: 2.9850770235061646 | |
[08:29:28.186] [LOG] Delta: 101.50537198781967 Drift: 1.5053719878196716 | |
[08:29:28.291] [LOG] Delta: 104.91704899072647 Drift: 4.917048990726471 | |
[08:29:28.397] [LOG] Delta: 105.72322303056717 Drift: 5.723223030567169 | |
[08:29:28.503] [LOG] Delta: 105.64269995689392 Drift: 5.642699956893921 | |
[08:29:28.608] [LOG] Delta: 105.66952002048492 Drift: 5.669520020484924 | |
[08:29:28.714] [LOG] Delta: 105.67887300252914 Drift: 5.678873002529144 | |
[08:29:28.820] [LOG] Delta: 105.68044197559357 Drift: 5.680441975593567 | |
[08:29:28.925] [LOG] Delta: 105.64465004205704 Drift: 5.644650042057037 | |
[08:29:29.031] [LOG] Delta: 105.659716963768 Drift: 5.659716963768005 | |
[08:29:29.137] [LOG] Delta: 105.66396802663803 Drift: 5.663968026638031 | |
[08:29:29.238] [LOG] Delta: 100.8074539899826 Drift: 0.807453989982605 | |
[08:29:29.339] [LOG] Delta: 101.61503797769547 Drift: 1.615037977695465 | |
[08:29:29.442] [LOG] Delta: 103.08838301897049 Drift: 3.0883830189704895 | |
[08:29:29.545] [LOG] Delta: 102.43028497695923 Drift: 2.4302849769592285 | |
[08:29:29.650] [LOG] Delta: 105.67610800266266 Drift: 5.676108002662659 | |
[08:29:29.753] [LOG] Delta: 103.1043810248375 Drift: 3.104381024837494 | |
*/ | |
/* | |
While 0-6 ms is not much, over one second this adds up to 0-60 ms, and over | |
a minute this can reach up to 3.6 seconds of lag. So some correction must be done. | |
I had the idea to subtract the drift from the next step to ensure that every other | |
step is "on beat." | |
The expected results would look like: | |
[08:29:29.339] [LOG] Delta: 101.615 Drift: 1.615 Next: 98.385 | |
[08:29:29.442] [LOG] Delta: 98.385 Drift: 0.000 Next: 100.000 | |
[08:29:29.545] [LOG] Delta: 102.430 Drift: 2.430 Next: 97.570 | |
[08:29:29.650] [LOG] Delta: 97.570 Drift: 0.000 Next: 100.000 | |
[08:29:29.753] [LOG] Delta: 103.104 Drift: 3.104 Next: 96.896 | |
*/ | |
/* I haven't been able to achieve that though. Instead, here's the code I wrote: */ | |
function tick() { | |
const delta = ENGINE._update(); | |
const drift = delta - ENGINE._step; | |
console.log( "Delta:", delta, "Drift:", drift ); | |
setTimeout( tick, ENGINE._step - drift); | |
} | |
tick(); | |
ENGINE._update = function() { | |
const now = Present(); | |
const delta = now - ENGINE._lastTickTime; | |
ENGINE._lastTickTime = now; | |
// Do some stuff... | |
return delta; | |
} | |
/* And the output: | |
[08:43:35.790] [LOG] Delta: 205.34477305412292 Drift: 105.34477305412292 | |
[08:43:35.791] [LOG] Delta: 2.0265939831733704 Drift: -97.97340601682663 | |
[08:43:35.989] [LOG] Delta: 197.6884889602661 Drift: 97.68848896026611 | |
[08:43:35.992] [LOG] Delta: 3.2490310072898865 Drift: -96.75096899271011 | |
[08:43:36.194] [LOG] Delta: 201.68472200632095 Drift: 101.68472200632095 | |
[08:43:36.196] [LOG] Delta: 2.03957998752594 Drift: -97.96042001247406 | |
[08:43:36.398] [LOG] Delta: 202.72177803516388 Drift: 102.72177803516388 | |
[08:43:36.400] [LOG] Delta: 2.115935981273651 Drift: -97.88406401872635 | |
[08:43:36.603] [LOG] Delta: 202.76934403181076 Drift: 102.76934403181076 | |
[08:43:36.605] [LOG] Delta: 2.013449966907501 Drift: -97.9865500330925 | |
[08:43:36.806] [LOG] Delta: 200.65522599220276 Drift: 100.65522599220276 | |
[08:43:36.807] [LOG] Delta: 0.7635080218315125 Drift: -99.23649197816849 | |
[08:43:37.007] [LOG] Delta: 199.9889190196991 Drift: 99.9889190196991 | |
[08:43:37.009] [LOG] Delta: 1.9681169986724854 Drift: -98.03188300132751 | |
[08:43:37.212] [LOG] Delta: 203.70313996076584 Drift: 103.70313996076584 | |
[08:43:37.214] [LOG] Delta: 2.002306044101715 Drift: -97.99769395589828 | |
[08:43:37.417] [LOG] Delta: 202.6856939792633 Drift: 102.6856939792633 | |
[08:43:37.419] [LOG] Delta: 2.0086439847946167 Drift: -97.99135601520538 | |
[08:43:37.621] [LOG] Delta: 202.28574699163437 Drift: 102.28574699163437 | |
[08:43:37.623] [LOG] Delta: 2.017973005771637 Drift: -97.98202699422836 | |
What's going wrong here? Why am I getting 2 ticks every 200ms instead of 1 every 100ms? | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment