Skip to content

Commit 87bd2e6

Browse files
committed
Queue 'late' colliding bodies for a second update
- Fixes phaserjs#4154 - Fixes phaserjs#4284
1 parent d690b70 commit 87bd2e6

1 file changed

Lines changed: 57 additions & 4 deletions

File tree

src/physics/arcade/World.js

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,28 @@ var World = new Class({
221221
*/
222222
this.pendingDestroy = new Set();
223223

224+
/**
225+
* Dynamic Bodies that need a second `update` call to resynchronize their Game Objects.
226+
* This set is filled only when the `_late` flag is on, and is processed and cleared during `postUpdate`.
227+
*
228+
* @name Phaser.Physics.Arcade.World#late
229+
* @type {Phaser.Structs.Set.<Phaser.Physics.Arcade.Body>}
230+
* @private
231+
* @since 3.16.0
232+
*/
233+
this.late = new Set();
234+
235+
/**
236+
* A flag allowing the `late` set to be filled, as appropriate.
237+
* This is on (true) only between `update` and `postUpdate` and false at other times.
238+
*
239+
* @name Phaser.Physics.Arcade.World#_late
240+
* @type {boolean}
241+
* @private
242+
* @since 3.16.0
243+
*/
244+
this._late = false;
245+
224246
/**
225247
* This simulation's collision processors.
226248
*
@@ -755,6 +777,7 @@ var World = new Class({
755777
{
756778
this.tree.remove(body);
757779
this.bodies.delete(body);
780+
this.late.delete(body);
758781
}
759782
else if (body.physicsType === CONST.STATIC_BODY)
760783
{
@@ -1043,6 +1066,7 @@ var World = new Class({
10431066
var msPerFrame = this._frameTimeMS * this.timeScale;
10441067

10451068
this._elapsed += delta;
1069+
this._late = false;
10461070

10471071
while (this._elapsed >= msPerFrame)
10481072
{
@@ -1054,6 +1078,7 @@ var World = new Class({
10541078
}
10551079

10561080
this.stepsLastFrame = stepsThisFrame;
1081+
this._late = true;
10571082
},
10581083

10591084
/**
@@ -1124,14 +1149,37 @@ var World = new Class({
11241149
postUpdate: function ()
11251150
{
11261151
var i;
1152+
var bodies;
11271153
var body;
1154+
var len;
11281155

11291156
var dynamic = this.bodies;
11301157
var staticBodies = this.staticBodies;
11311158
var pending = this.pendingDestroy;
1159+
var late = this.late;
11321160

1133-
var bodies = dynamic.entries;
1134-
var len = bodies.length;
1161+
if (late.size > 0)
1162+
{
1163+
bodies = late.entries;
1164+
len = bodies.length;
1165+
1166+
for (i = 0; i < len; i++)
1167+
{
1168+
body = bodies[i];
1169+
1170+
if (body.enable)
1171+
{
1172+
body.postUpdate();
1173+
}
1174+
}
1175+
1176+
late.clear();
1177+
}
1178+
1179+
this._late = false;
1180+
1181+
bodies = dynamic.entries;
1182+
len = bodies.length;
11351183

11361184
if (this.drawDebug)
11371185
{
@@ -1179,6 +1227,7 @@ var World = new Class({
11791227
{
11801228
dynamicTree.remove(body);
11811229
dynamic.delete(body);
1230+
late.delete(body);
11821231
}
11831232
else if (body.physicsType === CONST.STATIC_BODY)
11841233
{
@@ -1470,8 +1519,11 @@ var World = new Class({
14701519
}
14711520
else
14721521
{
1473-
body1.postUpdate();
1474-
body2.postUpdate();
1522+
if (this._late)
1523+
{
1524+
this.late.set(body1);
1525+
this.late.set(body2);
1526+
}
14751527

14761528
if (body1.onCollide || body2.onCollide)
14771529
{
@@ -2355,6 +2407,7 @@ var World = new Class({
23552407
this.staticTree.clear();
23562408
this.bodies.clear();
23572409
this.staticBodies.clear();
2410+
this.late.clear();
23582411
this.colliders.destroy();
23592412

23602413
this.removeAllListeners();

0 commit comments

Comments
 (0)