@@ -20,6 +20,20 @@ Phaser.Physics.Arcade = function (game) {
2020 this . FLOOR = this . DOWN ;
2121 this . WALL = this . LEFT | this . RIGHT ;
2222 this . ANY = this . LEFT | this . RIGHT | this . UP | this . DOWN ;
23+ // console.log('any', this.ANY);
24+ this . OVERLAP_BIAS = 4 ;
25+ this . TILE_OVERLAP = false ;
26+
27+ // avoid creating these every frame, per collision
28+ this . _obj1Bounds = new Phaser . Rectangle ;
29+ this . _obj2Bounds = new Phaser . Rectangle ;
30+ this . _overlap = 0 ;
31+ this . _maxOverlap = 0 ;
32+ this . _obj1Velocity = 0 ;
33+ this . _obj2Velocity = 0 ;
34+ this . _obj1NewVelocity = 0 ;
35+ this . _obj2NewVelocity = 0 ;
36+ this . _average = 0 ;
2337
2438 // this.angularDrag = 0;
2539 // this.gravity = new Phaser.Point;
@@ -50,13 +64,13 @@ Phaser.Physics.Arcade.prototype = {
5064 body . velocity . x += this . _velocityDelta ;
5165 this . _delta = body . velocity . x * this . game . time . physicsElapsed ;
5266 body . velocity . x += this . _velocityDelta ;
53- body . _x += this . _delta ;
67+ body . x += this . _delta ;
5468
5569 this . _velocityDelta = ( this . computeVelocity ( body . velocity . y , body . gravity . y , body . acceleration . y , body . drag . y ) - body . velocity . y ) / 2 ;
5670 body . velocity . y += this . _velocityDelta ;
5771 this . _delta = body . velocity . y * this . game . time . physicsElapsed ;
5872 body . velocity . y += this . _velocityDelta ;
59- body . _y += this . _delta ;
73+ body . y += this . _delta ;
6074
6175 } ,
6276
@@ -184,49 +198,46 @@ Phaser.Physics.Arcade.prototype = {
184198 }
185199
186200 // First, get the two object deltas
187- var overlap = 0 ;
188- var obj1Delta = object1 . x - object1 . last . x ;
189- var obj2Delta = object2 . x - object2 . last . x ;
201+ this . _overlap = 0 ;
190202
191- if ( obj1Delta != obj2Delta )
203+ if ( object1 . deltaX ( ) != object2 . deltaX ( ) )
192204 {
193205 // Check if the X hulls actually overlap
194- var obj1DeltaAbs = ( obj1Delta > 0 ) ? obj1Delta : - obj1Delta ;
195- var obj2DeltaAbs = ( obj2Delta > 0 ) ? obj2Delta : - obj2Delta ;
196- var obj1Bounds = new Quad ( object1 . x - ( ( obj1Delta > 0 ) ? obj1Delta : 0 ) , object1 . last . y , object1 . width + ( ( obj1Delta > 0 ) ? obj1Delta : - obj1Delta ) , object1 . height ) ;
197- var obj2Bounds = new Quad ( object2 . x - ( ( obj2Delta > 0 ) ? obj2Delta : 0 ) , object2 . last . y , object2 . width + ( ( obj2Delta > 0 ) ? obj2Delta : - obj2Delta ) , object2 . height ) ;
198206
199- if ( ( obj1Bounds . x + obj1Bounds . width > obj2Bounds . x ) && ( obj1Bounds . x < obj2Bounds . x + obj2Bounds . width ) && ( obj1Bounds . y + obj1Bounds . height > obj2Bounds . y ) && ( obj1Bounds . y < obj2Bounds . y + obj2Bounds . height ) )
207+ this . _obj1Bounds . setTo ( object1 . x - ( ( object1 . deltaX ( ) > 0 ) ? object1 . deltaX ( ) : 0 ) , object1 . lastY , object1 . width + ( ( object1 . deltaX ( ) > 0 ) ? object1 . deltaX ( ) : - object1 . deltaX ( ) ) , object1 . height ) ;
208+ this . _obj2Bounds . setTo ( object2 . x - ( ( object2 . deltaX ( ) > 0 ) ? object2 . deltaX ( ) : 0 ) , object2 . lastY , object2 . width + ( ( object2 . deltaX ( ) > 0 ) ? object2 . deltaX ( ) : - object2 . deltaX ( ) ) , object2 . height ) ;
209+
210+ if ( ( this . _obj1Bounds . x + this . _obj1Bounds . width > this . _obj2Bounds . x ) && ( this . _obj1Bounds . x < this . _obj2Bounds . x + this . _obj2Bounds . width ) && ( this . _obj1Bounds . y + this . _obj1Bounds . height > this . _obj2Bounds . y ) && ( this . _obj1Bounds . y < this . _obj2Bounds . y + this . _obj2Bounds . height ) )
200211 {
201- var maxOverlap = obj1DeltaAbs + obj2DeltaAbs + Collision . OVERLAP_BIAS ;
212+ this . _maxOverlap = object1 . deltaAbsX ( ) + object2 . deltaAbsX ( ) + this . OVERLAP_BIAS ;
202213
203214 // If they did overlap (and can), figure out by how much and flip the corresponding flags
204- if ( obj1Delta > obj2Delta )
215+ if ( object1 . deltaAbsX ( ) > object2 . deltaAbsX ( ) )
205216 {
206- overlap = object1 . x + object1 . width - object2 . x ;
217+ this . _overlap = object1 . x + object1 . width - object2 . x ;
207218
208- if ( ( overlap > maxOverlap ) || ! ( object1 . allowCollisions & Collision . RIGHT ) || ! ( object2 . allowCollisions & Collision . LEFT ) )
219+ if ( ( this . _overlap > this . _maxOverlap ) || ! ( object1 . allowCollisions & this . RIGHT ) || ! ( object2 . allowCollisions & this . LEFT ) )
209220 {
210- overlap = 0 ;
221+ this . _overlap = 0 ;
211222 }
212223 else
213224 {
214- object1 . touching |= Collision . RIGHT ;
215- object2 . touching |= Collision . LEFT ;
225+ object1 . touching |= this . RIGHT ;
226+ object2 . touching |= this . LEFT ;
216227 }
217228 }
218- else if ( obj1Delta < obj2Delta )
229+ else if ( object1 . deltaX ( ) < object2 . deltaX ( ) )
219230 {
220- overlap = object1 . x - object2 . width - object2 . x ;
231+ this . _overlap = object1 . x - object2 . width - object2 . x ;
221232
222- if ( ( - overlap > maxOverlap ) || ! ( object1 . allowCollisions & Collision . LEFT ) || ! ( object2 . allowCollisions & Collision . RIGHT ) )
233+ if ( ( - this . _overlap > this . _maxOverlap ) || ! ( object1 . allowCollisions & this . LEFT ) || ! ( object2 . allowCollisions & this . RIGHT ) )
223234 {
224- overlap = 0 ;
235+ this . _overlap = 0 ;
225236 }
226237 else
227238 {
228- object1 . touching |= Collision . LEFT ;
229- object2 . touching |= Collision . RIGHT ;
239+ object1 . touching |= this . LEFT ;
240+ object2 . touching |= this . RIGHT ;
230241 }
231242
232243 }
@@ -235,34 +246,34 @@ Phaser.Physics.Arcade.prototype = {
235246 }
236247
237248 // Then adjust their positions and velocities accordingly (if there was any overlap)
238- if ( overlap != 0 )
249+ if ( this . _overlap != 0 )
239250 {
240- var obj1Velocity = object1 . velocity . x ;
241- var obj2Velocity = object2 . velocity . x ;
251+ this . _obj1Velocity = object1 . velocity . x ;
252+ this . _obj2Velocity = object2 . velocity . x ;
242253
243254 if ( ! object1 . immovable && ! object2 . immovable )
244255 {
245- overlap *= 0.5 ;
246- object1 . x = object1 . x - overlap ;
247- object2 . x += overlap ;
248-
249- var obj1NewVelocity = Math . sqrt ( ( obj2Velocity * obj2Velocity * object2 . mass ) / object1 . mass ) * ( ( obj2Velocity > 0 ) ? 1 : - 1 ) ;
250- var obj2NewVelocity = Math . sqrt ( ( obj1Velocity * obj1Velocity * object1 . mass ) / object2 . mass ) * ( ( obj1Velocity > 0 ) ? 1 : - 1 ) ;
251- var average = ( obj1NewVelocity + obj2NewVelocity ) * 0.5 ;
252- obj1NewVelocity -= average ;
253- obj2NewVelocity -= average ;
254- object1 . velocity . x = average + obj1NewVelocity * object1 . elasticity ;
255- object2 . velocity . x = average + obj2NewVelocity * object2 . elasticity ;
256+ this . _overlap *= 0.5 ;
257+ object1 . x = object1 . x - this . _overlap ;
258+ object2 . x += this . _overlap ;
259+
260+ this . _obj1NewVelocity = Math . sqrt ( ( this . _obj2Velocity * this . _obj2Velocity * object2 . mass ) / object1 . mass ) * ( ( this . _obj2Velocity > 0 ) ? 1 : - 1 ) ;
261+ this . _obj2NewVelocity = Math . sqrt ( ( this . _obj1Velocity * this . _obj1Velocity * object1 . mass ) / object2 . mass ) * ( ( this . _obj1Velocity > 0 ) ? 1 : - 1 ) ;
262+ this . _average = ( this . _obj1NewVelocity + this . _obj2NewVelocity ) * 0.5 ;
263+ this . _obj1NewVelocity -= this . _average ;
264+ this . _obj2NewVelocity -= this . _average ;
265+ object1 . velocity . x = this . _average + this . _obj1NewVelocity * object1 . bounce . x ;
266+ object2 . velocity . x = this . _average + this . _obj2NewVelocity * object2 . bounce . x ;
256267 }
257268 else if ( ! object1 . immovable )
258269 {
259- object1 . x = object1 . x - overlap ;
260- object1 . velocity . x = obj2Velocity - obj1Velocity * object1 . elasticity ;
270+ object1 . x = object1 . x - this . _overlap ;
271+ object1 . velocity . x = this . _obj2Velocity - this . _obj1Velocity * object1 . bounce . x ;
261272 }
262273 else if ( ! object2 . immovable )
263274 {
264- object2 . x += overlap ;
265- object2 . velocity . x = obj1Velocity - obj2Velocity * object2 . elasticity ;
275+ object2 . x += this . _overlap ;
276+ object2 . velocity . x = this . _obj1Velocity - this . _obj2Velocity * object2 . bounce . x ;
266277 }
267278
268279 return true ;
@@ -297,10 +308,10 @@ Phaser.Physics.Arcade.prototype = {
297308 // Check if the Y hulls actually overlap
298309 var obj1DeltaAbs = ( obj1Delta > 0 ) ? obj1Delta : - obj1Delta ;
299310 var obj2DeltaAbs = ( obj2Delta > 0 ) ? obj2Delta : - obj2Delta ;
300- var obj1Bounds : Quad = new Quad ( object1 . x , object1 . y - ( ( obj1Delta > 0 ) ? obj1Delta : 0 ) , object1 . width , object1 . height + obj1DeltaAbs ) ;
301- var obj2Bounds : Quad = new Quad ( object2 . x , object2 . y - ( ( obj2Delta > 0 ) ? obj2Delta : 0 ) , object2 . width , object2 . height + obj2DeltaAbs ) ;
311+ this . _obj1Bounds = new Phaser . Rectangle ( object1 . x , object1 . y - ( ( obj1Delta > 0 ) ? obj1Delta : 0 ) , object1 . width , object1 . height + obj1DeltaAbs ) ;
312+ this . _obj2Bounds = new Phaser . Rectangle ( object2 . x , object2 . y - ( ( obj2Delta > 0 ) ? obj2Delta : 0 ) , object2 . width , object2 . height + obj2DeltaAbs ) ;
302313
303- if ( ( obj1Bounds . x + obj1Bounds . width > obj2Bounds . x ) && ( obj1Bounds . x < obj2Bounds . x + obj2Bounds . width ) && ( obj1Bounds . y + obj1Bounds . height > obj2Bounds . y ) && ( obj1Bounds . y < obj2Bounds . y + obj2Bounds . height ) )
314+ if ( ( this . _obj1Bounds . x + this . _obj1Bounds . width > this . _obj2Bounds . x ) && ( this . _obj1Bounds . x < this . _obj2Bounds . x + this . _obj2Bounds . width ) && ( this . _obj1Bounds . y + this . _obj1Bounds . height > this . _obj2Bounds . y ) && ( this . _obj1Bounds . y < this . _obj2Bounds . y + this . _obj2Bounds . height ) )
304315 {
305316 var maxOverlap = obj1DeltaAbs + obj2DeltaAbs + Collision . OVERLAP_BIAS ;
306317
@@ -339,27 +350,27 @@ Phaser.Physics.Arcade.prototype = {
339350 // Then adjust their positions and velocities accordingly (if there was any overlap)
340351 if ( overlap != 0 )
341352 {
342- var obj1Velocity = object1 . velocity . y ;
343- var obj2Velocity = object2 . velocity . y ;
353+ this . _obj1Velocity = object1 . velocity . y ;
354+ this . _obj2Velocity = object2 . velocity . y ;
344355
345356 if ( ! object1 . immovable && ! object2 . immovable )
346357 {
347358 overlap *= 0.5 ;
348359 object1 . y = object1 . y - overlap ;
349360 object2 . y += overlap ;
350361
351- var obj1NewVelocity = Math . sqrt ( ( obj2Velocity * obj2Velocity * object2 . mass ) / object1 . mass ) * ( ( obj2Velocity > 0 ) ? 1 : - 1 ) ;
352- var obj2NewVelocity = Math . sqrt ( ( obj1Velocity * obj1Velocity * object1 . mass ) / object2 . mass ) * ( ( obj1Velocity > 0 ) ? 1 : - 1 ) ;
353- var average = ( obj1NewVelocity + obj2NewVelocity ) * 0.5 ;
354- obj1NewVelocity -= average ;
355- obj2NewVelocity -= average ;
356- object1 . velocity . y = average + obj1NewVelocity * object1 . elasticity ;
357- object2 . velocity . y = average + obj2NewVelocity * object2 . elasticity ;
362+ this . _obj1NewVelocity = Math . sqrt ( ( this . _obj2Velocity * this . _obj2Velocity * object2 . mass ) / object1 . mass ) * ( ( this . _obj2Velocity > 0 ) ? 1 : - 1 ) ;
363+ this . _obj2NewVelocity = Math . sqrt ( ( this . _obj1Velocity * this . _obj1Velocity * object1 . mass ) / object2 . mass ) * ( ( this . _obj1Velocity > 0 ) ? 1 : - 1 ) ;
364+ this . _average = ( this . _obj1NewVelocity + this . _obj2NewVelocity ) * 0.5 ;
365+ this . _obj1NewVelocity -= this . _average ;
366+ this . _obj2NewVelocity -= this . _average ;
367+ object1 . velocity . y = this . _average + this . _obj1NewVelocity * object1 . elasticity ;
368+ object2 . velocity . y = this . _average + this . _obj2NewVelocity * object2 . elasticity ;
358369 }
359370 else if ( ! object1 . immovable )
360371 {
361372 object1 . y = object1 . y - overlap ;
362- object1 . velocity . y = obj2Velocity - obj1Velocity * object1 . elasticity ;
373+ object1 . velocity . y = this . _obj2Velocity - this . _obj1Velocity * object1 . elasticity ;
363374 // This is special case code that handles things like horizontal moving platforms you can ride
364375 if ( object2 . active && object2 . moves && ( obj1Delta > obj2Delta ) )
365376 {
@@ -369,7 +380,7 @@ Phaser.Physics.Arcade.prototype = {
369380 else if ( ! object2 . immovable )
370381 {
371382 object2 . y += overlap ;
372- object2 . velocity . y = obj1Velocity - obj2Velocity * object2 . elasticity ;
383+ object2 . velocity . y = this . _obj1Velocity - this . _obj2Velocity * object2 . elasticity ;
373384 // This is special case code that handles things like horizontal moving platforms you can ride
374385 if ( object1 . active && object1 . moves && ( obj1Delta < obj2Delta ) )
375386 {
@@ -385,11 +396,4 @@ Phaser.Physics.Arcade.prototype = {
385396 }
386397 } ,
387398
388-
389-
390-
391399} ;
392-
393- Phaser . Physics . Arcade . prototype . OVERLAP_BIAS = 4 ;
394- Phaser . Physics . Arcade . prototype . TILE_OVERLAP = false ;
395-
0 commit comments