Skip to content

Commit 6095ab0

Browse files
committed
Split the Body step up into preUpdate, update and postUpdate. Also tidied up the postUpdate method.
1 parent b75db77 commit 6095ab0

1 file changed

Lines changed: 51 additions & 26 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -865,15 +865,15 @@ var Body = new Class({
865865
},
866866

867867
/**
868-
* Updates the Body.
868+
* Prepares the Body for a physics step by resetting all the states and syncing the position
869+
* with the parent Game Object.
870+
*
871+
* This method is only ever called once per game step.
869872
*
870-
* @method Phaser.Physics.Arcade.Body#update
871-
* @fires Phaser.Physics.Arcade.World#worldbounds
872-
* @since 3.0.0
873-
*
874-
* @param {number} delta - The delta time, in seconds, elapsed since the last frame.
873+
* @method Phaser.Physics.Arcade.Body#preUpdate
874+
* @since 3.16.3
875875
*/
876-
update: function (delta)
876+
preUpdate: function ()
877877
{
878878
// Store and reset collision flags
879879
this.wasTouching.none = this.touching.none;
@@ -919,7 +919,24 @@ var Body = new Class({
919919
this.prev.x = this.position.x;
920920
this.prev.y = this.position.y;
921921
}
922+
},
922923

924+
/**
925+
* Performs a single physics step and updates the body velocity, angle, speed and other
926+
* properties.
927+
*
928+
* This method can be called multiple times per game step.
929+
*
930+
* The results are synced back to the Game Object in `postUpdate`.
931+
*
932+
* @method Phaser.Physics.Arcade.Body#update
933+
* @fires Phaser.Physics.Arcade.World#worldbounds
934+
* @since 3.0.0
935+
*
936+
* @param {number} delta - The delta time, in seconds, elapsed since the last frame.
937+
*/
938+
update: function (delta)
939+
{
923940
if (this.moves)
924941
{
925942
this.world.updateMotion(this, delta);
@@ -936,7 +953,7 @@ var Body = new Class({
936953
this.angle = Math.atan2(vy, vx);
937954
this.speed = Math.sqrt(vx * vx + vy * vy);
938955

939-
// Now the State update will throw collision checks at the Body
956+
// Now the update will throw collision checks at the Body
940957
// And finally we'll integrate the new position back to the Sprite in postUpdate
941958

942959
if (this.collideWorldBounds && this.checkWorldBounds() && this.onWorldBounds)
@@ -951,65 +968,73 @@ var Body = new Class({
951968

952969
/**
953970
* Feeds the Body results back into the parent Game Object.
971+
*
972+
* This method is only ever called once per game step.
954973
*
955974
* @method Phaser.Physics.Arcade.Body#postUpdate
956975
* @since 3.0.0
957976
*/
958977
postUpdate: function ()
959978
{
960-
this._dx = this.position.x - this.prev.x;
961-
this._dy = this.position.y - this.prev.y;
979+
var dx = this.position.x - this.prev.x;
980+
var dy = this.position.y - this.prev.y;
962981

963982
if (this.moves)
964983
{
965-
if (this.deltaMax.x !== 0 && this._dx !== 0)
984+
var mx = this.deltaMax.x;
985+
var my = this.deltaMax.y;
986+
987+
if (mx !== 0 && dx !== 0)
966988
{
967-
if (this._dx < 0 && this._dx < -this.deltaMax.x)
989+
if (dx < 0 && dx < -mx)
968990
{
969-
this._dx = -this.deltaMax.x;
991+
dx = -mx;
970992
}
971-
else if (this._dx > 0 && this._dx > this.deltaMax.x)
993+
else if (dx > 0 && dx > mx)
972994
{
973-
this._dx = this.deltaMax.x;
995+
dx = mx;
974996
}
975997
}
976998

977-
if (this.deltaMax.y !== 0 && this._dy !== 0)
999+
if (my !== 0 && dy !== 0)
9781000
{
979-
if (this._dy < 0 && this._dy < -this.deltaMax.y)
1001+
if (dy < 0 && dy < -my)
9801002
{
981-
this._dy = -this.deltaMax.y;
1003+
dy = -my;
9821004
}
983-
else if (this._dy > 0 && this._dy > this.deltaMax.y)
1005+
else if (dy > 0 && dy > my)
9841006
{
985-
this._dy = this.deltaMax.y;
1007+
dy = my;
9861008
}
9871009
}
9881010

989-
this.gameObject.x += this._dx;
990-
this.gameObject.y += this._dy;
1011+
this.gameObject.x += dx;
1012+
this.gameObject.y += dy;
9911013

9921014
this._reset = true;
9931015
}
9941016

995-
if (this._dx < 0)
1017+
if (dx < 0)
9961018
{
9971019
this.facing = CONST.FACING_LEFT;
9981020
}
999-
else if (this._dx > 0)
1021+
else if (dx > 0)
10001022
{
10011023
this.facing = CONST.FACING_RIGHT;
10021024
}
10031025

1004-
if (this._dy < 0)
1026+
if (dy < 0)
10051027
{
10061028
this.facing = CONST.FACING_UP;
10071029
}
1008-
else if (this._dy > 0)
1030+
else if (dy > 0)
10091031
{
10101032
this.facing = CONST.FACING_DOWN;
10111033
}
10121034

1035+
this._dx = dx;
1036+
this._dy = dy;
1037+
10131038
if (this.allowRotation)
10141039
{
10151040
this.gameObject.angle += this.deltaZ();

0 commit comments

Comments
 (0)