@@ -87,14 +87,18 @@ game.createClass('Body', {
87
87
@private
88
88
**/
89
89
_collisionGroup : 0 ,
90
-
91
- init : function ( properties ) {
90
+
91
+ staticInit : function ( ) {
92
92
this . force = new game . Vector ( ) ;
93
93
this . position = new game . Vector ( ) ;
94
94
this . velocity = new game . Vector ( ) ;
95
95
this . velocityLimit = new game . Vector ( 980 , 980 ) ;
96
96
this . last = new game . Vector ( ) ;
97
+ } ,
98
+
99
+ init : function ( properties ) {
97
100
game . merge ( this , properties ) ;
101
+ return true ;
98
102
} ,
99
103
100
104
/**
@@ -172,21 +176,25 @@ game.createClass('Body', {
172
176
} ,
173
177
174
178
/**
175
- @method _update
176
- @private
179
+ Update body position and velocity.
180
+ @method update
181
+ @param {Number } [delta]
177
182
**/
178
- _update : function ( ) {
183
+ update : function ( delta ) {
184
+ delta = delta || game . delta ;
179
185
this . last . copy ( this . position ) ;
180
186
181
187
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 ;
187
195
188
196
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 ) ;
190
198
this . velocity . x *= damping ;
191
199
this . velocity . y *= damping ;
192
200
}
@@ -200,8 +208,8 @@ game.createClass('Body', {
200
208
if ( this . velocity . y < - this . velocityLimit . y ) this . velocity . y = - this . velocityLimit . y ;
201
209
}
202
210
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 ;
205
213
}
206
214
} ) ;
207
215
@@ -232,6 +240,7 @@ game.defineProperties('Body', {
232
240
@constructor
233
241
@param {Number } [x] Gravity x
234
242
@param {Number } [y] Gravity y
243
+ @param {Boolean } [manualUpdate] Don't update physics automatically
235
244
**/
236
245
game . createClass ( 'Physics' , {
237
246
/**
@@ -251,11 +260,11 @@ game.createClass('Physics', {
251
260
**/
252
261
_collisionGroups : { } ,
253
262
254
- staticInit : function ( x , y ) {
263
+ staticInit : function ( x , y , manualUpdate ) {
255
264
x = typeof x === 'number' ? x : 0 ;
256
265
y = typeof y === 'number' ? y : 980 ;
257
266
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 ) ;
259
268
} ,
260
269
261
270
/**
@@ -270,6 +279,37 @@ game.createClass('Physics', {
270
279
this . _addBodyCollision ( body ) ;
271
280
} ,
272
281
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
+
273
313
/**
274
314
Hit response a versus b.
275
315
@method hitResponse
@@ -379,37 +419,6 @@ game.createClass('Physics', {
379
419
this . _collisionGroups [ body . collisionGroup ] . push ( body ) ;
380
420
} ,
381
421
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
-
413
422
/**
414
423
@method _removeBodyCollision
415
424
@param {Body } body
@@ -434,7 +443,7 @@ game.createClass('Physics', {
434
443
this . bodies . splice ( i , 1 ) ;
435
444
}
436
445
else {
437
- this . bodies [ i ] . _update ( ) ;
446
+ this . bodies [ i ] . update ( ) ;
438
447
}
439
448
}
440
449
} ,
0 commit comments