Merge pull request #4287 from samme/fix/arcade-body-deltas

Queue 'late' colliding bodies for a second update
This commit is contained in:
Richard Davey 2019-01-18 16:03:36 +00:00 committed by GitHub
commit 7237277abe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -178,6 +178,28 @@ var World = new Class({
*/ */
this.pendingDestroy = new Set(); this.pendingDestroy = new Set();
/**
* Dynamic Bodies that need a second `update` call to resynchronize their Game Objects.
* This set is filled only when the `_late` flag is on, and is processed and cleared during `postUpdate`.
*
* @name Phaser.Physics.Arcade.World#late
* @type {Phaser.Structs.Set.<Phaser.Physics.Arcade.Body>}
* @private
* @since 3.16.0
*/
this.late = new Set();
/**
* A flag allowing the `late` set to be filled, as appropriate.
* This is on (true) only between `update` and `postUpdate` and false at other times.
*
* @name Phaser.Physics.Arcade.World#_late
* @type {boolean}
* @private
* @since 3.16.0
*/
this._late = false;
/** /**
* This simulation's collision processors. * This simulation's collision processors.
* *
@ -712,6 +734,7 @@ var World = new Class({
{ {
this.tree.remove(body); this.tree.remove(body);
this.bodies.delete(body); this.bodies.delete(body);
this.late.delete(body);
} }
else if (body.physicsType === CONST.STATIC_BODY) else if (body.physicsType === CONST.STATIC_BODY)
{ {
@ -1000,6 +1023,7 @@ var World = new Class({
var msPerFrame = this._frameTimeMS * this.timeScale; var msPerFrame = this._frameTimeMS * this.timeScale;
this._elapsed += delta; this._elapsed += delta;
this._late = false;
while (this._elapsed >= msPerFrame) while (this._elapsed >= msPerFrame)
{ {
@ -1011,6 +1035,7 @@ var World = new Class({
} }
this.stepsLastFrame = stepsThisFrame; this.stepsLastFrame = stepsThisFrame;
this._late = true;
}, },
/** /**
@ -1081,14 +1106,37 @@ var World = new Class({
postUpdate: function () postUpdate: function ()
{ {
var i; var i;
var bodies;
var body; var body;
var len;
var dynamic = this.bodies; var dynamic = this.bodies;
var staticBodies = this.staticBodies; var staticBodies = this.staticBodies;
var pending = this.pendingDestroy; var pending = this.pendingDestroy;
var late = this.late;
var bodies = dynamic.entries; if (late.size > 0)
var len = bodies.length; {
bodies = late.entries;
len = bodies.length;
for (i = 0; i < len; i++)
{
body = bodies[i];
if (body.enable)
{
body.postUpdate();
}
}
late.clear();
}
this._late = false;
bodies = dynamic.entries;
len = bodies.length;
if (this.drawDebug) if (this.drawDebug)
{ {
@ -1136,6 +1184,7 @@ var World = new Class({
{ {
dynamicTree.remove(body); dynamicTree.remove(body);
dynamic.delete(body); dynamic.delete(body);
late.delete(body);
} }
else if (body.physicsType === CONST.STATIC_BODY) else if (body.physicsType === CONST.STATIC_BODY)
{ {
@ -1427,8 +1476,11 @@ var World = new Class({
} }
else else
{ {
body1.postUpdate(); if (this._late)
body2.postUpdate(); {
this.late.set(body1);
this.late.set(body2);
}
if (body1.onCollide || body2.onCollide) if (body1.onCollide || body2.onCollide)
{ {
@ -2310,6 +2362,7 @@ var World = new Class({
this.staticTree.clear(); this.staticTree.clear();
this.bodies.clear(); this.bodies.clear();
this.staticBodies.clear(); this.staticBodies.clear();
this.late.clear();
this.colliders.destroy(); this.colliders.destroy();
this.removeAllListeners(); this.removeAllListeners();