@@ -87,14 +87,18 @@ game.createClass('Body', {
8787 @private
8888 **/
8989 _collisionGroup : 0 ,
90-
91- init : function ( properties ) {
90+
91+ staticInit : function ( ) {
9292 this . force = new game . Vector ( ) ;
9393 this . position = new game . Vector ( ) ;
9494 this . velocity = new game . Vector ( ) ;
9595 this . velocityLimit = new game . Vector ( 980 , 980 ) ;
9696 this . last = new game . Vector ( ) ;
97+ } ,
98+
99+ init : function ( properties ) {
97100 game . merge ( this , properties ) ;
101+ return true ;
98102 } ,
99103
100104 /**
@@ -172,21 +176,25 @@ game.createClass('Body', {
172176 } ,
173177
174178 /**
175- @method _update
176- @private
179+ Update body position and velocity.
180+ @method update
181+ @param {Number } [delta]
177182 **/
178- _update : function ( ) {
183+ update : function ( delta ) {
184+ delta = delta || game . delta ;
179185 this . last . copy ( this . position ) ;
180186
181187 if ( this . static ) return ;
182-
183- this . velocity . x += this . world . gravity . x * this . mass * game . delta ;
184- this . velocity . y += this . world . gravity . y * this . mass * game . delta ;
185- this . velocity . x += this . force . x * game . delta ;
186- this . velocity . y += this . force . y * game . delta ;
188+
189+ if ( this . world ) {
190+ this . velocity . x += this . world . gravity . x * this . mass * delta ;
191+ this . velocity . y += this . world . gravity . y * this . mass * delta ;
192+ }
193+ this . velocity . x += this . force . x * delta ;
194+ this . velocity . y += this . force . y * delta ;
187195
188196 if ( this . damping > 0 && this . damping < 1 ) {
189- var damping = Math . pow ( 1 - this . damping , game . delta ) ;
197+ var damping = Math . pow ( 1 - this . damping , delta ) ;
190198 this . velocity . x *= damping ;
191199 this . velocity . y *= damping ;
192200 }
@@ -200,8 +208,8 @@ game.createClass('Body', {
200208 if ( this . velocity . y < - this . velocityLimit . y ) this . velocity . y = - this . velocityLimit . y ;
201209 }
202210
203- this . position . x += this . velocity . x * game . delta ;
204- this . position . y += this . velocity . y * game . delta ;
211+ this . position . x += this . velocity . x * delta ;
212+ this . position . y += this . velocity . y * delta ;
205213 }
206214} ) ;
207215
@@ -232,6 +240,7 @@ game.defineProperties('Body', {
232240 @constructor
233241 @param {Number } [x] Gravity x
234242 @param {Number } [y] Gravity y
243+ @param {Boolean } [manualUpdate] Don't update physics automatically
235244**/
236245game . createClass ( 'Physics' , {
237246 /**
@@ -251,11 +260,11 @@ game.createClass('Physics', {
251260 **/
252261 _collisionGroups : { } ,
253262
254- staticInit : function ( x , y ) {
263+ staticInit : function ( x , y , manualUpdate ) {
255264 x = typeof x === 'number' ? x : 0 ;
256265 y = typeof y === 'number' ? y : 980 ;
257266 this . gravity = new game . Vector ( x , y ) ;
258- if ( game . scene ) game . scene . physics . push ( this ) ;
267+ if ( game . scene && ! manualUpdate ) game . scene . physics . push ( this ) ;
259268 } ,
260269
261270 /**
@@ -270,6 +279,37 @@ game.createClass('Physics', {
270279 this . _addBodyCollision ( body ) ;
271280 } ,
272281
282+ /**
283+ Perform collision for body.
284+ @method collide
285+ @param {Body } body
286+ **/
287+ collide : function ( body ) {
288+ var g , i , b , group ;
289+
290+ for ( g = 0 ; g < body . collideAgainst . length ; g ++ ) {
291+ body . _collides . length = 0 ;
292+ group = this . _collisionGroups [ body . collideAgainst [ g ] ] ;
293+
294+ if ( ! group ) continue ;
295+
296+ for ( i = group . length - 1 ; i >= 0 ; i -- ) {
297+ if ( ! group ) break ;
298+ b = group [ i ] ;
299+ if ( body !== b ) {
300+ if ( this . hitTest ( body , b ) ) {
301+ body . _collides . push ( b ) ;
302+ }
303+ }
304+ }
305+ for ( i = body . _collides . length - 1 ; i >= 0 ; i -- ) {
306+ if ( this . hitResponse ( body , body . _collides [ i ] ) ) {
307+ body . afterCollide ( body . _collides [ i ] ) ;
308+ }
309+ }
310+ }
311+ } ,
312+
273313 /**
274314 Hit response a versus b.
275315 @method hitResponse
@@ -379,37 +419,6 @@ game.createClass('Physics', {
379419 this . _collisionGroups [ body . collisionGroup ] . push ( body ) ;
380420 } ,
381421
382- /**
383- @method _collide
384- @param {Body } body
385- @private
386- **/
387- _collide : function ( body ) {
388- var g , i , b , group ;
389-
390- for ( g = 0 ; g < body . collideAgainst . length ; g ++ ) {
391- body . _collides . length = 0 ;
392- group = this . _collisionGroups [ body . collideAgainst [ g ] ] ;
393-
394- if ( ! group ) continue ;
395-
396- for ( i = group . length - 1 ; i >= 0 ; i -- ) {
397- if ( ! group ) break ;
398- b = group [ i ] ;
399- if ( body !== b ) {
400- if ( this . hitTest ( body , b ) ) {
401- body . _collides . push ( b ) ;
402- }
403- }
404- }
405- for ( i = body . _collides . length - 1 ; i >= 0 ; i -- ) {
406- if ( this . hitResponse ( body , body . _collides [ i ] ) ) {
407- body . afterCollide ( body . _collides [ i ] ) ;
408- }
409- }
410- }
411- } ,
412-
413422 /**
414423 @method _removeBodyCollision
415424 @param {Body } body
@@ -434,7 +443,7 @@ game.createClass('Physics', {
434443 this . bodies . splice ( i , 1 ) ;
435444 }
436445 else {
437- this . bodies [ i ] . _update ( ) ;
446+ this . bodies [ i ] . update ( ) ;
438447 }
439448 }
440449 } ,
0 commit comments