From bc35c93106a8c2dc460644ae730767f3dd64648f Mon Sep 17 00:00:00 2001 From: liabru Date: Fri, 2 May 2014 10:48:24 +0100 Subject: [PATCH] added engine.timing.isFixed option --- src/core/Engine.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/core/Engine.js b/src/core/Engine.js index 7ecc9d1..7c321a6 100644 --- a/src/core/Engine.js +++ b/src/core/Engine.js @@ -48,7 +48,8 @@ var Engine = {}; correction: 1, deltaMin: 1000 / _fps, deltaMax: 1000 / (_fps * 0.5), - timeScale: 1 + timeScale: 1, + isFixed: false }, render: { element: element, @@ -87,7 +88,7 @@ var Engine = {}; Engine.run = function(engine) { var timing = engine.timing, delta, - correction, + correction = 1, counterTimestamp = 0, frameCounter = 0, deltaHistory = [], @@ -109,19 +110,25 @@ var Engine = {}; Events.trigger(engine, 'beforeTick', event); - delta = (timestamp - timing.timestamp) || _delta; + if (timing.isFixed) { + // fixed timestep + delta = timing.delta; + } else { + // dynamic timestep + delta = (timestamp - timing.timestamp) || _delta; - // optimistically filter delta over a few frames, to improve stability - deltaHistory.push(delta); - deltaHistory = deltaHistory.slice(-_deltaSampleSize); - delta = Math.min.apply(null, deltaHistory); - - // limit delta - delta = delta < timing.deltaMin ? timing.deltaMin : delta; - delta = delta > timing.deltaMax ? timing.deltaMax : delta; + // optimistically filter delta over a few frames, to improve stability + deltaHistory.push(delta); + deltaHistory = deltaHistory.slice(-_deltaSampleSize); + delta = Math.min.apply(null, deltaHistory); + + // limit delta + delta = delta < timing.deltaMin ? timing.deltaMin : delta; + delta = delta > timing.deltaMax ? timing.deltaMax : delta; - // time correction for delta - correction = delta / timing.delta; + // time correction for delta + correction = delta / timing.delta; + } // time correction for time scaling if (timeScalePrev !== 0)