Skip to content

Commit 0f438d5

Browse files
committed
separateX up and working sweet with gc spike removal too
1 parent a27f6d6 commit 0f438d5

4 files changed

Lines changed: 99 additions & 90 deletions

File tree

examples/body3.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,26 @@ function preload() {
2020
}
2121

2222
var bunny;
23+
var wall;
2324
var bot;
2425

2526
function create() {
2627

2728
game.world._stage.backgroundColorString = '#182d3b';
2829

2930
bunny = game.add.sprite(200, 200, 'bunny');
31+
bunny.body.bounce.x = 0.8;
3032

31-
bunny.body.acceleration.x = 10;
32-
// bunny.body.velocity.y = 20;
33+
wall = game.add.sprite(700, 200, 'bunny');
34+
wall.body.immovable = true;
35+
36+
bunny.body.velocity.x = 180;
3337

3438
}
3539

3640
function update() {
3741

42+
game.physics.separateX(bunny.body, wall.body);
3843

3944
}
4045

src/gameobjects/Sprite.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,10 @@ Phaser.Sprite.prototype.update = function() {
243243
}
244244

245245
// Update our physics bounds
246-
this.body.update(this.center.x, this.center.y, this._cache.scaleX, this._cache.scaleY);
246+
this.body.updateBounds(this.center.x, this.center.y, this._cache.scaleX, this._cache.scaleY);
247247
}
248248

249-
this.body.updateMotion();
249+
this.body.update();
250250

251251
}
252252

src/physics/arcade/ArcadePhysics.js

Lines changed: 67 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -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-

src/physics/arcade/Body.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ Phaser.Physics.Arcade.Body = function (sprite) {
2727
this.immovable = false;
2828
this.touching = 0;
2929
this.wasTouching = 0;
30-
this.allowCollisions = 1;
30+
this.allowCollisions = 4369;
3131

32-
this._x = sprite.x;
33-
this._y = sprite.y;
34-
this._ox = sprite.x;
35-
this._oy = sprite.y;
32+
this.x = sprite.x;
33+
this.y = sprite.y;
34+
this.lastX = sprite.x;
35+
this.lastY = sprite.y;
3636

3737
};
3838

@@ -42,7 +42,7 @@ Phaser.Physics.Arcade.Body.prototype = {
4242
game: null,
4343
hitArea: null,
4444

45-
update: function (x, y, scaleX, scaleY) {
45+
updateBounds: function (x, y, scaleX, scaleY) {
4646

4747
if (scaleX != this._sx || scaleY != this._sy)
4848
{
@@ -57,10 +57,10 @@ Phaser.Physics.Arcade.Body.prototype = {
5757

5858
},
5959

60-
updateMotion: function () {
60+
update: function () {
6161

62-
this._ox = this._x;
63-
this._oy = this._y;
62+
this.lastX = this.x;
63+
this.lastY = this.y;
6464

6565
this.game.physics.updateMotion(this);
6666

@@ -71,8 +71,8 @@ Phaser.Physics.Arcade.Body.prototype = {
7171

7272
postUpdate: function () {
7373

74-
sprite.x = this._x;
75-
sprite.y = this._y;
74+
this.sprite.x = this.x;
75+
this.sprite.y = this.y;
7676

7777
},
7878

@@ -113,44 +113,44 @@ Phaser.Physics.Arcade.Body.prototype = {
113113

114114
hullX: function () {
115115

116-
if (this._x < this._ox)
116+
if (this.x < this.lastX)
117117
{
118-
return this._x;
118+
return this.x;
119119
}
120120
else
121121
{
122-
return this._ox;
122+
return this.lastX;
123123
}
124124

125125
},
126126

127127
hullY: function () {
128128

129-
if (this._y < this._oy)
129+
if (this.y < this.lastY)
130130
{
131-
return this._y;
131+
return this.y;
132132
}
133133
else
134134
{
135-
return this._oy;
135+
return this.lastY;
136136
}
137137

138138
},
139139

140-
deltaXAbs: function () {
141-
return (this.deltaX > 0 ? this.deltaX : -this.deltaX);
140+
deltaAbsX: function () {
141+
return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX());
142142
},
143143

144-
deltaYAbs: function () {
145-
return (this.deltaY > 0 ? this.deltaY : -this.deltaY);
144+
deltaAbsY: function () {
145+
return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY());
146146
},
147147

148148
deltaX: function () {
149-
return this._x - this._ox;
149+
return this.x - this.lastX;
150150
},
151151

152152
deltaY: function () {
153-
return this._y - this._oy;
153+
return this.y - this.lastY;
154154
}
155155

156156
};

0 commit comments

Comments
 (0)