Skip to content

Instantly share code, notes, and snippets.

@jacobgardner
Created November 10, 2016 19:29
Show Gist options
  • Save jacobgardner/fbf22a6fc407c72fcb661a044e9d6974 to your computer and use it in GitHub Desktop.
Save jacobgardner/fbf22a6fc407c72fcb661a044e9d6974 to your computer and use it in GitHub Desktop.
const TIMESTEP = 10; // 100 updates per second => 10ms = 0.01s => 1frame / 0.01s = 100fps
function renderLoop() {
// Do Renderer Things here
window.requestAnimationFrame(renderLoop);
}
window.requestAnimationFrame(renderLoop)
var lastTime = Date.now();
var currentTick = 0;
function physicsLoop() {
// We assume each tick has a deltaTime of TIMESTEP which may cause slowmotion if this is inefficient.
const now = Date.now();
setTimeout(physicsLoop, Math.max(0, TIMESTEP - (now - lastTime)));
lastTime = now;
currentTick += 1;
}
physicsLoop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment