Skip to content

Commit

Permalink
Merge pull request #1254 from liabru/runner-2
Browse files Browse the repository at this point in the history
Improve Matter.Runner
  • Loading branch information
liabru authored Jun 22, 2024
2 parents 0b131a4 + aba0e9e commit e0e3164
Show file tree
Hide file tree
Showing 9 changed files with 467 additions and 123 deletions.
1 change: 0 additions & 1 deletion demo/src/Compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ var compare = function(examples, isDev) {
ticks += 1;

var demoBuildInstance = demoBuild.example.instance;
runner.isFixed = demoBuildInstance.runner.isFixed = true;
runner.delta = demoBuildInstance.runner.delta = 1000 / 60;

window.Matter = MatterBuild;
Expand Down
16 changes: 16 additions & 0 deletions demo/src/Demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ var demo = function(examples, isDev) {
document.title = 'Matter.js Demo' + (isDev ? ' ・ Dev' : '');

if (isDev) {
// add delta control
Matter.Common.chainPathAfter(MatterTools, 'Gui.create', function() {
this.datGui.__folders["Engine"]
.add(demo, "delta", 0, 1000 / 55)
.onChange(function() {
var runner = demo.example.instance.runner;
runner.delta = demo.delta;
})
.step(0.001)
.listen();
});

Matter.after('Runner.create', function() {
demo.delta = this.delta;
});

// add compare button
var buttonSource = demo.dom.buttonSource,
buttonCompare = buttonSource.cloneNode(true);
Expand Down
1 change: 1 addition & 0 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = {
stress2: require('./stress2.js'),
stress3: require('./stress3.js'),
stress4: require('./stress4.js'),
substep: require('./substep.js'),
svg: require('./svg.js'),
terrain: require('./terrain.js'),
timescale: require('./timescale.js'),
Expand Down
4 changes: 1 addition & 3 deletions examples/stress3.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ Example.stress3 = function() {
Render.run(render);

// create runner
var runner = Runner.create({
isFixed: true
});
var runner = Runner.create();
Runner.run(runner, engine);

// add bodies
Expand Down
4 changes: 1 addition & 3 deletions examples/stress4.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ Example.stress4 = function() {
Render.run(render);

// create runner
var runner = Runner.create({
isFixed: true
});
var runner = Runner.create();

Runner.run(runner, engine);

Expand Down
122 changes: 122 additions & 0 deletions examples/substep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
var Example = Example || {};

Example.substep = function() {
var Engine = Matter.Engine,
Render = Matter.Render,
Runner = Matter.Runner,
Events = Matter.Events,
Composite = Matter.Composite,
MouseConstraint = Matter.MouseConstraint,
Mouse = Matter.Mouse,
Bodies = Matter.Bodies;

// create engine
var engine = Engine.create(),
world = engine.world;

// create renderer
var render = Render.create({
element: document.body,
engine: engine,
options: {
width: 800,
height: 600,
wireframes: false,
showDebug: true,
background: '#000',
pixelRatio: 2
}
});

Render.run(render);

// create runner with higher precision timestep (requires >= v0.20.0 beta)
var runner = Runner.create({
// 600Hz delta = 1.666ms = 10upf @ 60fps (i.e. 10x default precision)
delta: 1000 / (60 * 10),
// 50fps minimum performance target (i.e. budget allows up to ~20ms execution per frame)
maxFrameTime: 1000 / 50
});

Runner.run(runner, engine);

// demo substepping using very thin bodies (than is typically recommended)
Composite.add(world, [
Bodies.rectangle(250, 250, 300, 1.25, {
frictionAir: 0,
friction: 0,
restitution: 0.9,
angle: 0.5,
render: {
lineWidth: 0.5,
fillStyle: '#f55a3c'
}
}),
Bodies.circle(200, 200, 2.25, {
frictionAir: 0,
friction: 0,
restitution: 0.9,
angle: 0.5,
render: {
fillStyle: '#fff'
}
})
]);

// add bodies
Composite.add(world, [
Bodies.rectangle(400, 0, 800, 50, { isStatic: true, render: { visible: false } }),
Bodies.rectangle(400, 600, 800, 50, { isStatic: true, render: { visible: false } }),
Bodies.rectangle(800, 300, 50, 600, { isStatic: true, render: { visible: false } }),
Bodies.rectangle(0, 300, 50, 600, { isStatic: true, render: { visible: false } })
]);

// scene animation
Events.on(engine, 'afterUpdate', function(event) {
engine.gravity.scale = 0.00035;
engine.gravity.x = Math.cos(engine.timing.timestamp * 0.0005);
engine.gravity.y = Math.sin(engine.timing.timestamp * 0.0005);
});

// add mouse control
var mouse = Mouse.create(render.canvas),
mouseConstraint = MouseConstraint.create(engine, {
mouse: mouse,
constraint: {
stiffness: 0.2,
render: {
visible: false
}
}
});

Composite.add(world, mouseConstraint);

// keep the mouse in sync with rendering
render.mouse = mouse;

// fit the render viewport to the scene
Render.lookAt(render, {
min: { x: 0, y: 0 },
max: { x: 800, y: 600 }
});

// context for MatterTools.Demo
return {
engine: engine,
runner: runner,
render: render,
canvas: render.canvas,
stop: function() {
Matter.Render.stop(render);
Matter.Runner.stop(runner);
}
};
};

Example.substep.title = 'High Substeps (Low Delta)';
Example.substep.for = '>=0.20.0';

if (typeof module !== 'undefined') {
module.exports = Example.substep;
}
12 changes: 11 additions & 1 deletion src/core/Engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ var Body = require('../body/Body');

(function() {

Engine._deltaMax = 1000 / 60;

/**
* Creates a new engine. The options parameter is an object that specifies any properties you wish to override the defaults.
* All properties have default values, and many are pre-calculated automatically based on other properties.
Expand Down Expand Up @@ -51,7 +53,8 @@ var Body = require('../body/Body');
timestamp: 0,
timeScale: 1,
lastDelta: 0,
lastElapsed: 0
lastElapsed: 0,
lastUpdatesPerFrame: 0
}
};

Expand Down Expand Up @@ -89,6 +92,13 @@ var Body = require('../body/Body');
timestamp = timing.timestamp,
i;

// warn if high delta
if (delta > Engine._deltaMax) {
Common.warnOnce(
'Matter.Engine.update: delta argument is recommended to be less than or equal to', Engine._deltaMax.toFixed(3), 'ms.'
);
}

delta = typeof delta !== 'undefined' ? delta : Common._baseDelta;
delta *= timing.timeScale;

Expand Down
Loading

0 comments on commit e0e3164

Please sign in to comment.