Created
November 10, 2016 19:29
-
-
Save jacobgardner/fbf22a6fc407c72fcb661a044e9d6974 to your computer and use it in GitHub Desktop.
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
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