Skip to content

Commit 586d182

Browse files
committed
Removed dirty property, optimized update loop, refactored postUpdate for fixed time step
1 parent 329c926 commit 586d182

1 file changed

Lines changed: 60 additions & 71 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 60 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -637,16 +637,6 @@ var Body = new Class({
637637
*/
638638
this.blocked = { none: true, up: false, down: false, left: false, right: false };
639639

640-
/**
641-
* Whether this Body is in its `update` phase.
642-
*
643-
* @name Phaser.Physics.Arcade.Body#dirty
644-
* @type {boolean}
645-
* @default false
646-
* @since 3.0.0
647-
*/
648-
this.dirty = false;
649-
650640
/**
651641
* Whether to automatically synchronize this Body's dimensions to the dimensions of its Game Object's visual bounds.
652642
*
@@ -789,33 +779,32 @@ var Body = new Class({
789779
transform.scaleY = sprite.scaleY;
790780
}
791781

782+
var recalc = false;
783+
792784
if (this.syncBounds)
793785
{
794786
var b = sprite.getBounds(this._bounds);
795787

796-
if (b.width !== this.width || b.height !== this.height)
797-
{
798-
this.width = b.width;
799-
this.height = b.height;
800-
this._reset = true;
801-
}
788+
this.width = b.width;
789+
this.height = b.height;
790+
recalc = true;
802791
}
803792
else
804793
{
805794
var asx = Math.abs(transform.scaleX);
806795
var asy = Math.abs(transform.scaleY);
807796

808-
if (asx !== this._sx || asy !== this._sy)
797+
if (this._sx !== asx || this._sy !== asy)
809798
{
810799
this.width = this.sourceWidth * asx;
811800
this.height = this.sourceHeight * asy;
812801
this._sx = asx;
813802
this._sy = asy;
814-
this._reset = true;
803+
recalc = true;
815804
}
816805
}
817806

818-
if (this._reset)
807+
if (recalc)
819808
{
820809
this.halfWidth = Math.floor(this.width / 2);
821810
this.halfHeight = Math.floor(this.height / 2);
@@ -845,8 +834,6 @@ var Body = new Class({
845834
*/
846835
update: function (delta)
847836
{
848-
this.dirty = true;
849-
850837
// Store and reset collision flags
851838
this.wasTouching.none = this.touching.none;
852839
this.wasTouching.up = this.touching.up;
@@ -894,21 +881,19 @@ var Body = new Class({
894881

895882
if (this.moves)
896883
{
897-
this.world.updateMotion(this);
884+
this.world.updateMotion(this, delta);
898885

899-
this.newVelocity.set(this.velocity.x * delta, this.velocity.y * delta);
886+
var vx = this.velocity.x;
887+
var vy = this.velocity.y;
900888

901-
this.position.x += this.newVelocity.x;
902-
this.position.y += this.newVelocity.y;
889+
this.newVelocity.set(vx * delta, vy * delta);
903890

904-
this.updateCenter();
891+
this.position.add(this.newVelocity);
905892

906-
if (this.position.x !== this.prev.x || this.position.y !== this.prev.y)
907-
{
908-
this.angle = Math.atan2(this.velocity.y, this.velocity.x);
909-
}
893+
this.updateCenter();
910894

911-
this.speed = Math.sqrt(this.velocity.x * this.velocity.x + this.velocity.y * this.velocity.y);
895+
this.angle = Math.atan2(vy, vx);
896+
this.speed = Math.sqrt(vx * vx + vy * vy);
912897

913898
// Now the State update will throw collision checks at the Body
914899
// And finally we'll integrate the new position back to the Sprite in postUpdate
@@ -921,47 +906,21 @@ var Body = new Class({
921906

922907
this._dx = this.deltaX();
923908
this._dy = this.deltaY();
924-
925-
this._reset = false;
926909
},
927910

928911
/**
929912
* Feeds the Body results back into the parent Game Object.
930913
*
931914
* @method Phaser.Physics.Arcade.Body#postUpdate
932915
* @since 3.0.0
916+
*
917+
* @param {boolean} resetDelta - Reset the delta properties?
933918
*/
934919
postUpdate: function ()
935920
{
936-
// Only allow postUpdate to be called once per frame
937-
if (!this.enable || !this.dirty)
938-
{
939-
return;
940-
}
941-
942-
this.dirty = false;
943-
944921
this._dx = this.deltaX();
945922
this._dy = this.deltaY();
946923

947-
if (this._dx < 0)
948-
{
949-
this.facing = CONST.FACING_LEFT;
950-
}
951-
else if (this._dx > 0)
952-
{
953-
this.facing = CONST.FACING_RIGHT;
954-
}
955-
956-
if (this._dy < 0)
957-
{
958-
this.facing = CONST.FACING_UP;
959-
}
960-
else if (this._dy > 0)
961-
{
962-
this.facing = CONST.FACING_DOWN;
963-
}
964-
965924
if (this.moves)
966925
{
967926
if (this.deltaMax.x !== 0 && this._dx !== 0)
@@ -988,16 +947,29 @@ var Body = new Class({
988947
}
989948
}
990949

991-
// this.transform.x += this._dx;
992-
// this.transform.y += this._dy;
993-
994950
this.gameObject.x += this._dx;
995951
this.gameObject.y += this._dy;
996952

997953
this._reset = true;
998954
}
999955

1000-
this.updateCenter();
956+
if (this._dx < 0)
957+
{
958+
this.facing = CONST.FACING_LEFT;
959+
}
960+
else if (this._dx > 0)
961+
{
962+
this.facing = CONST.FACING_RIGHT;
963+
}
964+
965+
if (this._dy < 0)
966+
{
967+
this.facing = CONST.FACING_UP;
968+
}
969+
else if (this._dy > 0)
970+
{
971+
this.facing = CONST.FACING_DOWN;
972+
}
1001973

1002974
if (this.allowRotation)
1003975
{
@@ -1292,8 +1264,7 @@ var Body = new Class({
12921264
},
12931265

12941266
/**
1295-
* The absolute (nonnegative) change in this Body's horizontal position from the previous step.
1296-
* This value is set only during the Body's `dirty` (update) phase.
1267+
* The absolute (non-negative) change in this Body's horizontal position from the previous step.
12971268
*
12981269
* @method Phaser.Physics.Arcade.Body#deltaAbsX
12991270
* @since 3.0.0
@@ -1302,12 +1273,11 @@ var Body = new Class({
13021273
*/
13031274
deltaAbsX: function ()
13041275
{
1305-
return (this.deltaX() > 0) ? this.deltaX() : -this.deltaX();
1276+
return (this._dx > 0) ? this._dx : -this._dx;
13061277
},
13071278

13081279
/**
1309-
* The absolute (nonnegative) change in this Body's horizontal position from the previous step.
1310-
* This value is set only during the Body's `dirty` (update) phase.
1280+
* The absolute (non-negative) change in this Body's vertical position from the previous step.
13111281
*
13121282
* @method Phaser.Physics.Arcade.Body#deltaAbsY
13131283
* @since 3.0.0
@@ -1316,12 +1286,12 @@ var Body = new Class({
13161286
*/
13171287
deltaAbsY: function ()
13181288
{
1319-
return (this.deltaY() > 0) ? this.deltaY() : -this.deltaY();
1289+
return (this._dy > 0) ? this._dy : -this._dy;
13201290
},
13211291

13221292
/**
13231293
* The change in this Body's horizontal position from the previous step.
1324-
* This value is set only during the Body's `dirty` (update) phase.
1294+
* This value is set during the Body's update phase.
13251295
*
13261296
* @method Phaser.Physics.Arcade.Body#deltaX
13271297
* @since 3.0.0
@@ -1335,6 +1305,7 @@ var Body = new Class({
13351305

13361306
/**
13371307
* The change in this Body's vertical position from the previous step.
1308+
* This value is set during the Body's update phase.
13381309
*
13391310
* @method Phaser.Physics.Arcade.Body#deltaY
13401311
* @since 3.0.0
@@ -1446,7 +1417,7 @@ var Body = new Class({
14461417
* @since 3.0.0
14471418
*
14481419
* @param {number} x - The horizontal velocity, in pixels per second.
1449-
* @param {number} y - The vertical velocity, in pixels per second.
1420+
* @param {number} [y=x] - The vertical velocity, in pixels per second.
14501421
*
14511422
* @return {Phaser.Physics.Arcade.Body} This Body object.
14521423
*/
@@ -1491,6 +1462,24 @@ var Body = new Class({
14911462
return this;
14921463
},
14931464

1465+
/**
1466+
* Sets the Body's maximum velocity.
1467+
*
1468+
* @method Phaser.Physics.Arcade.Body#setMaxVelocity
1469+
* @since 3.10.0
1470+
*
1471+
* @param {number} x - The horizontal velocity, in pixels per second.
1472+
* @param {number} [y=x] - The vertical velocity, in pixels per second.
1473+
*
1474+
* @return {Phaser.Physics.Arcade.Body} This Body object.
1475+
*/
1476+
setMaxVelocity: function (x, y)
1477+
{
1478+
this.maxVelocity.set(x, y);
1479+
1480+
return this;
1481+
},
1482+
14941483
/**
14951484
* Sets the Body's bounce.
14961485
*

0 commit comments

Comments
 (0)