From 516494e3df6df3b63061ab480d97fd36f73978a1 Mon Sep 17 00:00:00 2001 From: liabru Date: Sun, 1 Sep 2019 13:49:22 +0100 Subject: [PATCH] Added substeps feature to Matter.Runner --- src/core/Runner.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/core/Runner.js b/src/core/Runner.js index f199649..f38988d 100644 --- a/src/core/Runner.js +++ b/src/core/Runner.js @@ -52,6 +52,7 @@ var Common = require('./Common'); */ Runner.create = function(options) { var defaults = { + substeps: 1, fps: 60, deltaSampleSize: 60, counterTimestamp: 0, @@ -85,8 +86,8 @@ var Common = require('./Common'); runner = Runner.create(); } - (function render(time){ - runner.frameRequestId = _requestAnimationFrame(render); + (function run(time){ + runner.frameRequestId = _requestAnimationFrame(run); if (time && runner.enabled) { Runner.tick(runner, engine, time); @@ -160,7 +161,14 @@ var Common = require('./Common'); // update Events.trigger(runner, 'beforeUpdate', event); - Engine.update(engine, delta, correction); + + var substeps = runner.substeps, + subDelta = delta / substeps; + + for (var i = 0; i < substeps; i += 1) { + Engine.update(engine, subDelta); + } + Events.trigger(runner, 'afterUpdate', event); // render @@ -291,6 +299,16 @@ var Common = require('./Common'); * @default true */ + /** + * A `Number` integer that specifies the number of `Engine.update` calls made per-tick. + * Increasing the number of substeps improves accuracy at the cost of performance. + * By default `1` update is performed per tick with time `delta`. + * If `substeps > 1` then `substeps` updates are made with `delta` being `delta / substeps`. + * @property substeps + * @type number + * @default 1 + */ + /** * A `Boolean` that specifies if the runner should use a fixed timestep (otherwise it is variable). * If timing is fixed, then the apparent simulation speed will change depending on the frame rate (but behaviour will be deterministic).