Skip to content

Commit e7a1e31

Browse files
committed
Matter Physics timestep adjustments (getDelta, step, set60Hz, etc)
* Matter Physics now has a new config property `getDelta` which allows you to specify your own function to calculate the delta value given to the Matter Engine when it updates. * Matter Physics has two new methods: `set60Hz` and `set30Hz` which will set an Engine update rate of 60Hz and 30Hz respectively. 60Hz being the default. * Matter Physics has a new config and run-time property `autoUpdate`, which defaults to `true`. When enabled the Matter Engine will update in sync with the game step (set by Request Animation Frame). The delta value given to Matter is now controlled by the `getDelta` function. * Matter Physics has a new method `step` which manually advances the physics simulation by one iteration, using whatever delta and correction values you pass in to it. When used in combination with `autoUpdate=false` you can now explicitly control the update frequency of the physics simulation and unbind it from the game step.
1 parent 1f44366 commit e7a1e31

3 files changed

Lines changed: 178 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# Change Log
22

3-
## Version 3.3.1 - Tetsuo - In Development
3+
## Version 3.4.0 - In Development
44

55
### New Features
66

77
* A new property was added to Matter.World, `correction` which is used in the Engine.update call and allows you to adjust the time
88
being passed to the simulation. The default value is 1 to remain consistent with previous releases.
99
* Group.destroy has a new optional argument `destroyChildren` which will automatically call `destroy` on all children of a Group if set to true (the default is false, hence it doesn't change the public API). Fix #3246 (thanks @DouglasLapsley)
10-
10+
* Matter Physics now has a new config property `getDelta` which allows you to specify your own function to calculate the delta value given to the Matter Engine when it updates.
11+
* Matter Physics has two new methods: `set60Hz` and `set30Hz` which will set an Engine update rate of 60Hz and 30Hz respectively. 60Hz being the default.
12+
* Matter Physics has a new config and run-time property `autoUpdate`, which defaults to `true`. When enabled the Matter Engine will update in sync with the game step (set by Request Animation Frame). The delta value given to Matter is now controlled by the `getDelta` function.
13+
* Matter Physics has a new method `step` which manually advances the physics simulation by one iteration, using whatever delta and correction values you pass in to it. When used in combination with `autoUpdate=false` you can now explicitly control the update frequency of the physics simulation and unbind it from the game step.
1114

1215
### Bug Fixes
1316

src/physics/matter-js/MatterPhysics.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ var MatterPhysics = new Class({
145145
*
146146
* @method Phaser.Physics.Matter.MatterPhysics#enableAttractorPlugin
147147
* @since 3.0.0
148+
*
149+
* @return {Phaser.Physics.Matter.MatterPhysics} This Matter Physics instance.
148150
*/
149151
enableAttractorPlugin: function ()
150152
{
@@ -159,6 +161,8 @@ var MatterPhysics = new Class({
159161
*
160162
* @method Phaser.Physics.Matter.MatterPhysics#enableWrapPlugin
161163
* @since 3.0.0
164+
*
165+
* @return {Phaser.Physics.Matter.MatterPhysics} This Matter Physics instance.
162166
*/
163167
enableWrapPlugin: function ()
164168
{
@@ -194,6 +198,72 @@ var MatterPhysics = new Class({
194198
return this.world.resume();
195199
},
196200

201+
/**
202+
* Sets the Matter Engine to run at fixed timestep of 60Hz and enables `autoUpdate`.
203+
* If you have set a custom `getDelta` function then this will override it.
204+
*
205+
* @method Phaser.Physics.Matter.MatterPhysics#set60Hz
206+
* @since 3.4.0
207+
*
208+
* @return {Phaser.Physics.Matter.MatterPhysics} This Matter Physics instance.
209+
*/
210+
set60Hz: function ()
211+
{
212+
this.world.getDelta = this.world.update60Hz;
213+
this.world.autoUpdate = true;
214+
215+
return this;
216+
},
217+
218+
/**
219+
* Sets the Matter Engine to run at fixed timestep of 30Hz and enables `autoUpdate`.
220+
* If you have set a custom `getDelta` function then this will override it.
221+
*
222+
* @method Phaser.Physics.Matter.MatterPhysics#set30Hz
223+
* @since 3.4.0
224+
*
225+
* @return {Phaser.Physics.Matter.MatterPhysics} This Matter Physics instance.
226+
*/
227+
set30Hz: function ()
228+
{
229+
this.world.getDelta = this.world.update30Hz;
230+
this.world.autoUpdate = true;
231+
232+
return this;
233+
},
234+
235+
/**
236+
* Manually advances the physics simulation by one iteration.
237+
*
238+
* You can optionally pass in the `delta` and `correction` values to be used by Engine.update.
239+
* If undefined they use the Matter defaults of 60Hz and no correction.
240+
*
241+
* Calling `step` directly bypasses any checks of `enabled` or `autoUpdate`.
242+
*
243+
* It also ignores any custom `getDelta` functions, as you should be passing the delta
244+
* value in to this call.
245+
*
246+
* You can adjust the number of iterations that Engine.update performs internally.
247+
* Use the Scene Matter Physics config object to set the following properties:
248+
*
249+
* positionIterations (defaults to 6)
250+
* velocityIterations (defaults to 4)
251+
* constraintIterations (defaults to 2)
252+
*
253+
* Adjusting these values can help performance in certain situations, depending on the physics requirements
254+
* of your game.
255+
*
256+
* @method Phaser.Physics.Matter.MatterPhysics#step
257+
* @since 3.4.0
258+
*
259+
* @param {number} [delta=16.666] - [description]
260+
* @param {number} [correction=1] - [description]
261+
*/
262+
step: function (delta, correction)
263+
{
264+
this.world.step(delta, correction);
265+
},
266+
197267
/**
198268
* [description]
199269
*

src/physics/matter-js/World.js

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,52 @@ var World = new Class({
128128
* @name Phaser.Physics.Matter.World#correction
129129
* @type {number}
130130
* @default 1
131-
* @since 3.3.1
131+
* @since 3.4.0
132132
*/
133133
this.correction = GetValue(config, 'correction', 1);
134134

135+
/**
136+
* This function is called every time the core game loop steps, which is bound to the
137+
* Request Animation Frame frequency unless otherwise modified.
138+
*
139+
* The function is passed two values: `time` and `delta`, both of which come from the game step values.
140+
*
141+
* It must return a number. This number is used as the delta value passed to Matter.Engine.update.
142+
*
143+
* You can override this function with your own to define your own timestep.
144+
*
145+
* If you need to update the Engine multiple times in a single game step then call
146+
* `World.update` as many times as required. Each call will trigger the `getDelta` function.
147+
* If you wish to have full control over when the Engine updates then see the property `autoUpdate`.
148+
*
149+
* You can also adjust the number of iterations that Engine.update performs.
150+
* Use the Scene Matter Physics config object to set the following properties:
151+
*
152+
* positionIterations (defaults to 6)
153+
* velocityIterations (defaults to 4)
154+
* constraintIterations (defaults to 2)
155+
*
156+
* Adjusting these values can help performance in certain situations, depending on the physics requirements
157+
* of your game.
158+
*
159+
* @name Phaser.Physics.Matter.World#getDelta
160+
* @type {function}
161+
* @since 3.4.0
162+
*/
163+
this.getDelta = GetValue(config, 'getDelta', this.update60Hz);
164+
165+
/**
166+
* Automatically call Engine.update every time the game steps.
167+
* If you disable this then you are responsible for calling `World.step` directly from your game.
168+
* If you call `set60Hz` or `set30Hz` then `autoUpdate` is reset to `true`.
169+
*
170+
* @name Phaser.Physics.Matter.World#autoUpdate
171+
* @type {boolean}
172+
* @default true
173+
* @since 3.4.0
174+
*/
175+
this.autoUpdate = GetValue(config, 'autoUpdate', true);
176+
135177
/**
136178
* [description]
137179
*
@@ -604,12 +646,70 @@ var World = new Class({
604646
*/
605647
update: function (time, delta)
606648
{
607-
if (this.enabled)
649+
if (this.enabled && this.autoUpdate)
608650
{
609-
Engine.update(this.engine, delta, this.correction);
651+
Engine.update(this.engine, this.getDelta(time, delta), this.correction);
610652
}
611653
},
612654

655+
/**
656+
* Manually advances the physics simulation by one iteration.
657+
*
658+
* You can optionally pass in the `delta` and `correction` values to be used by Engine.update.
659+
* If undefined they use the Matter defaults of 60Hz and no correction.
660+
*
661+
* Calling `step` directly bypasses any checks of `enabled` or `autoUpdate`.
662+
*
663+
* It also ignores any custom `getDelta` functions, as you should be passing the delta
664+
* value in to this call.
665+
*
666+
* You can adjust the number of iterations that Engine.update performs internally.
667+
* Use the Scene Matter Physics config object to set the following properties:
668+
*
669+
* positionIterations (defaults to 6)
670+
* velocityIterations (defaults to 4)
671+
* constraintIterations (defaults to 2)
672+
*
673+
* Adjusting these values can help performance in certain situations, depending on the physics requirements
674+
* of your game.
675+
*
676+
* @method Phaser.Physics.Matter.World#step
677+
* @since 3.4.0
678+
*
679+
* @param {number} [delta=16.666] - [description]
680+
* @param {number} [correction=1] - [description]
681+
*/
682+
step: function (delta, correction)
683+
{
684+
Engine.update(this.engine, delta, correction);
685+
},
686+
687+
/**
688+
* Runs the Matter Engine.update at a fixed timestep of 60Hz.
689+
*
690+
* @method Phaser.Physics.Matter.World#update60Hz
691+
* @since 3.4.0
692+
*
693+
* @return {number} The delta value to be passed to Engine.update.
694+
*/
695+
update60Hz: function ()
696+
{
697+
return 1000 / 60;
698+
},
699+
700+
/**
701+
* Runs the Matter Engine.update at a fixed timestep of 30Hz.
702+
*
703+
* @method Phaser.Physics.Matter.World#update30Hz
704+
* @since 3.4.0
705+
*
706+
* @return {number} The delta value to be passed to Engine.update.
707+
*/
708+
update30Hz: function ()
709+
{
710+
return 1000 / 30;
711+
},
712+
613713
/**
614714
* [description]
615715
*

0 commit comments

Comments
 (0)