11/**
22* The `Matter.Engine` module contains methods for creating and manipulating engines.
33* An engine is a controller that manages updating and rendering the simulation of the world.
4+ * See `Matter.Runner` for an optional game loop utility.
45*
56* See [Demo.js](https://github.com/liabru/matter-js/blob/master/demo/js/Demo.js)
67* and [DemoMobile.js](https://github.com/liabru/matter-js/blob/master/demo/js/DemoMobile.js) for usage examples.
@@ -13,13 +14,8 @@ var Engine = {};
1314( function ( ) {
1415
1516 var _fps = 60 ,
16- _deltaSampleSize = _fps ,
1717 _delta = 1000 / _fps ;
18-
19- var _requestAnimationFrame = window . requestAnimationFrame || window . webkitRequestAnimationFrame
20- || window . mozRequestAnimationFrame || window . msRequestAnimationFrame
21- || function ( callback ) { window . setTimeout ( function ( ) { callback ( Common . now ( ) ) ; } , _delta ) ; } ;
22-
18+
2319 /**
2420 * Creates a new engine. The options parameter is an object that specifies any properties you wish to override the defaults.
2521 * All properties have default values, and many are pre-calculated automatically based on other properties.
@@ -50,7 +46,8 @@ var Engine = {};
5046 deltaMin : 1000 / _fps ,
5147 deltaMax : 1000 / ( _fps * 0.5 ) ,
5248 timeScale : 1 ,
53- isFixed : false
49+ isFixed : false ,
50+ frameRequestId : 0
5451 } ,
5552 render : {
5653 element : element ,
@@ -72,95 +69,6 @@ var Engine = {};
7269 return engine ;
7370 } ;
7471
75- /**
76- * An optional utility function that provides a game loop, that handles updating the engine for you.
77- * Calls `Engine.update` and `Engine.render` on the `requestAnimationFrame` event automatically.
78- * Handles time correction and non-fixed dynamic timing (if enabled).
79- * Triggers `beforeTick`, `tick` and `afterTick` events.
80- * @method run
81- * @param {engine } engine
82- */
83- Engine . run = function ( engine ) {
84- var counterTimestamp = 0 ,
85- frameCounter = 0 ,
86- deltaHistory = [ ] ,
87- timePrev ,
88- timeScalePrev = 1 ;
89-
90- ( function render ( time ) {
91- _requestAnimationFrame ( render ) ;
92-
93- if ( ! engine . enabled )
94- return ;
95-
96- var timing = engine . timing ,
97- delta ,
98- correction ;
99-
100- // create an event object
101- var event = {
102- timestamp : time
103- } ;
104-
105- Events . trigger ( engine , 'beforeTick' , event ) ;
106-
107- if ( timing . isFixed ) {
108- // fixed timestep
109- delta = timing . delta ;
110- } else {
111- // dynamic timestep based on wall clock between calls
112- delta = ( time - timePrev ) || timing . delta ;
113- timePrev = time ;
114-
115- // optimistically filter delta over a few frames, to improve stability
116- deltaHistory . push ( delta ) ;
117- deltaHistory = deltaHistory . slice ( - _deltaSampleSize ) ;
118- delta = Math . min . apply ( null , deltaHistory ) ;
119-
120- // limit delta
121- delta = delta < timing . deltaMin ? timing . deltaMin : delta ;
122- delta = delta > timing . deltaMax ? timing . deltaMax : delta ;
123-
124- // time correction for delta
125- correction = delta / timing . delta ;
126-
127- // update engine timing object
128- timing . delta = delta ;
129- }
130-
131- // time correction for time scaling
132- if ( timeScalePrev !== 0 )
133- correction *= timing . timeScale / timeScalePrev ;
134-
135- if ( timing . timeScale === 0 )
136- correction = 0 ;
137-
138- timeScalePrev = timing . timeScale ;
139-
140- // fps counter
141- frameCounter += 1 ;
142- if ( time - counterTimestamp >= 1000 ) {
143- timing . fps = frameCounter * ( ( time - counterTimestamp ) / 1000 ) ;
144- counterTimestamp = time ;
145- frameCounter = 0 ;
146- }
147-
148- Events . trigger ( engine , 'tick' , event ) ;
149-
150- // if world has been modified, clear the render scene graph
151- if ( engine . world . isModified )
152- engine . render . controller . clear ( engine . render ) ;
153-
154- // update
155- Engine . update ( engine , delta , correction ) ;
156-
157- // render
158- Engine . render ( engine ) ;
159-
160- Events . trigger ( engine , 'afterTick' , event ) ;
161- } ) ( ) ;
162- } ;
163-
16472 /**
16573 * Moves the simulation forward in time by `delta` ms.
16674 * Triggers `beforeUpdate` and `afterUpdate` events.
@@ -339,6 +247,12 @@ var Engine = {};
339247 }
340248 } ;
341249
250+ /**
251+ * An alias for Runner.run, see `Matter.Runner` for more information.
252+ * @method run
253+ * @param {engine } engine
254+ */
255+
342256 /*
343257 *
344258 * Events Documentation
0 commit comments