Skip to content

Commit 87a8af3

Browse files
committed
Added in Matter Runner
1 parent 604531d commit 87a8af3

1 file changed

Lines changed: 94 additions & 1 deletion

File tree

src/physics/matter-js/World.js

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,27 @@ var World = new Class({
150150
*/
151151
this.getDelta = GetValue(config, 'getDelta', this.update60Hz);
152152

153+
this.runner = {
154+
fps: 60,
155+
correction: 1,
156+
deltaSampleSize: 60,
157+
counterTimestamp: 0,
158+
frameCounter: 0,
159+
deltaHistory: [],
160+
timePrev: null,
161+
timeScalePrev: 1,
162+
frameRequestId: null,
163+
isFixed: false,
164+
enabled: true
165+
};
166+
167+
// var runner = Common.extend(defaults, options);
168+
169+
this.runner.delta = this.runner.delta || 1000 / this.runner.fps;
170+
this.runner.deltaMin = this.runner.deltaMin || 1000 / this.runner.fps;
171+
this.runner.deltaMax = this.runner.deltaMax || 1000 / (this.runner.fps * 0.5);
172+
this.runner.fps = 1000 / this.runner.delta;
173+
153174
/**
154175
* Automatically call Engine.update every time the game steps.
155176
* If you disable this then you are responsible for calling `World.step` directly from your game.
@@ -1034,14 +1055,86 @@ var World = new Class({
10341055
* @param {number} time - The current time. Either a High Resolution Timer value if it comes from Request Animation Frame, or Date.now if using SetTimeout.
10351056
* @param {number} delta - The delta time in ms since the last frame. This is a smoothed and capped value based on the FPS rate.
10361057
*/
1037-
update: function (time, delta)
1058+
OLDupdate: function (time, delta)
10381059
{
10391060
if (this.enabled && this.autoUpdate)
10401061
{
10411062
Engine.update(this.engine, this.getDelta(time, delta), this.correction);
10421063
}
10431064
},
10441065

1066+
update: function (time)
1067+
{
1068+
var engine = this.engine;
1069+
var runner = this.runner;
1070+
1071+
var timing = engine.timing;
1072+
var correction = 1;
1073+
var delta;
1074+
1075+
if (runner.isFixed)
1076+
{
1077+
// fixed timestep
1078+
delta = this.getDelta(time, delta);
1079+
}
1080+
else
1081+
{
1082+
// dynamic timestep based on wall clock between calls
1083+
delta = (time - runner.timePrev) || runner.delta;
1084+
runner.timePrev = time;
1085+
1086+
// optimistically filter delta over a few frames, to improve stability
1087+
runner.deltaHistory.push(delta);
1088+
runner.deltaHistory = runner.deltaHistory.slice(-runner.deltaSampleSize);
1089+
delta = Math.min.apply(null, runner.deltaHistory);
1090+
1091+
// limit delta
1092+
delta = delta < runner.deltaMin ? runner.deltaMin : delta;
1093+
delta = delta > runner.deltaMax ? runner.deltaMax : delta;
1094+
1095+
// correction for delta
1096+
correction = delta / runner.delta;
1097+
1098+
// update engine timing object
1099+
runner.delta = delta;
1100+
}
1101+
1102+
// time correction for time scaling
1103+
if (runner.timeScalePrev !== 0)
1104+
{
1105+
correction *= timing.timeScale / runner.timeScalePrev;
1106+
}
1107+
1108+
if (timing.timeScale === 0)
1109+
{
1110+
correction = 0;
1111+
}
1112+
1113+
runner.timeScalePrev = timing.timeScale;
1114+
runner.correction = correction;
1115+
1116+
// fps counter
1117+
runner.frameCounter += 1;
1118+
1119+
if (time - runner.counterTimestamp >= 1000)
1120+
{
1121+
runner.fps = runner.frameCounter * ((time - runner.counterTimestamp) / 1000);
1122+
runner.counterTimestamp = time;
1123+
runner.frameCounter = 0;
1124+
}
1125+
1126+
// Events.trigger(runner, 'tick', event);
1127+
1128+
// update
1129+
// Events.trigger(runner, 'beforeUpdate', event);
1130+
1131+
Engine.update(engine, delta, correction);
1132+
1133+
// Events.trigger(runner, 'afterUpdate', event);
1134+
1135+
// Events.trigger(runner, 'afterTick', event);
1136+
},
1137+
10451138
/**
10461139
* Manually advances the physics simulation by one iteration.
10471140
*

0 commit comments

Comments
 (0)