0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-23 09:26:51 -05:00

Added substeps feature to Matter.Runner

This commit is contained in:
liabru 2019-09-01 13:49:22 +01:00
parent d7e4f585b7
commit 516494e3df

View file

@ -52,6 +52,7 @@ var Common = require('./Common');
*/ */
Runner.create = function(options) { Runner.create = function(options) {
var defaults = { var defaults = {
substeps: 1,
fps: 60, fps: 60,
deltaSampleSize: 60, deltaSampleSize: 60,
counterTimestamp: 0, counterTimestamp: 0,
@ -85,8 +86,8 @@ var Common = require('./Common');
runner = Runner.create(); runner = Runner.create();
} }
(function render(time){ (function run(time){
runner.frameRequestId = _requestAnimationFrame(render); runner.frameRequestId = _requestAnimationFrame(run);
if (time && runner.enabled) { if (time && runner.enabled) {
Runner.tick(runner, engine, time); Runner.tick(runner, engine, time);
@ -160,7 +161,14 @@ var Common = require('./Common');
// update // update
Events.trigger(runner, 'beforeUpdate', event); 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); Events.trigger(runner, 'afterUpdate', event);
// render // render
@ -291,6 +299,16 @@ var Common = require('./Common');
* @default true * @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). * 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). * If timing is fixed, then the apparent simulation speed will change depending on the frame rate (but behaviour will be deterministic).