change dt handling

This commit is contained in:
Magnus Ulimoen 2019-09-03 19:41:20 +02:00
parent fae2501b85
commit 41b86029e8
1 changed files with 19 additions and 8 deletions

27
main.js
View File

@ -150,20 +150,31 @@ async function run() {
const height = 50; const height = 50;
const universe = Universe.new(width, height); const universe = Universe.new(width, height);
const TIMEFACTOR = 1.0/7000; const TIMEFACTOR = 20000.0;
let t = performance.now()*TIMEFACTOR; const MAX_DT = 1.0/Math.max(width, height)/2.0;
let t = 0;
let first_draw = true;
let warn_time = -1;
universe.init(0.5, 0.5); universe.init(0.5, 0.5);
function drawMe(t_draw) { function drawMe(t_draw) {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
let dt = t_draw*TIMEFACTOR - t; let dt = (t_draw - t) / TIMEFACTOR;
if (dt >= 0.01) { t = t_draw;
console.warn("Can not keep up with framerate"); if (first_draw || dt <= 0.0) {
t = t_draw*TIMEFACTOR; first_draw = false;
dt = 0.01; dt = MAX_DT;
} else { } else {
t += dt; if (dt >= MAX_DT) {
warn_time += 1;
if (warn_time != -2) {
console.warn("Can not keep up with framerate");
}
dt = MAX_DT;
}
} }
universe.advance(dt); universe.advance(dt);