Skip to content

Commit 95d413d

Browse files
Fix friction issues at low frame rates or repeated physics steps.
- Add `prevFrame` to hold previous frame positions (`prev` now holds previous step positions explicitly). - Reset `prev` per step, fixing physics that relies on it. - Remove `_reset` in favour of `moves`, which was all it ever checked. Ironically, `_reset` never reset itself. - Remove some `postUpdate` property setting which is now unnecessary. The `prev` values now update just before they're used, so if you check them outside the physics step, they reflect the previous step. - Reset `World#stepsLastFrame` per frame, so it won't get stuck on.
1 parent fd0dd79 commit 95d413d

2 files changed

Lines changed: 18 additions & 22 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ var Body = new Class({
159159
*/
160160
this.prev = new Vector2(gameObject.x, gameObject.y);
161161

162+
/**
163+
* The position of this Body during the previous frame.
164+
*
165+
* @name Phaser.Physics.Arcade.Body#prevFrame
166+
* @type {Phaser.Math.Vector2}
167+
* @author Benjamin D. Richards <benjamindrichards@gmail.com>
168+
*/
169+
this.prevFrame = new Vector2(gameObject.x, gameObject.y);
170+
162171
/**
163172
* Whether this Body's `rotation` is affected by its angular acceleration and angular velocity.
164173
*
@@ -696,17 +705,6 @@ var Body = new Class({
696705
*/
697706
this.physicsType = CONST.DYNAMIC_BODY;
698707

699-
/**
700-
* Whether the Body's position needs updating from its Game Object.
701-
*
702-
* @name Phaser.Physics.Arcade.Body#_reset
703-
* @type {boolean}
704-
* @private
705-
* @default true
706-
* @since 3.0.0
707-
*/
708-
this._reset = true;
709-
710708
/**
711709
* Cached horizontal scale of the Body's Game Object.
712710
*
@@ -909,10 +907,12 @@ var Body = new Class({
909907

910908
this.preRotation = this.rotation;
911909

912-
if (this._reset)
910+
if (this.moves)
913911
{
914912
this.prev.x = this.position.x;
915913
this.prev.y = this.position.y;
914+
this.prevFrame.x = this.position.x;
915+
this.prevFrame.y = this.position.y;
916916
}
917917

918918
if (willStep)
@@ -936,6 +936,9 @@ var Body = new Class({
936936
*/
937937
update: function (delta)
938938
{
939+
this.prev.x = this.position.x;
940+
this.prev.y = this.position.y;
941+
939942
if (this.moves)
940943
{
941944
this.world.updateMotion(this, delta);
@@ -975,8 +978,8 @@ var Body = new Class({
975978
*/
976979
postUpdate: function ()
977980
{
978-
var dx = this.position.x - this.prev.x;
979-
var dy = this.position.y - this.prev.y;
981+
var dx = this.position.x - this.prevFrame.x;
982+
var dy = this.position.y - this.prevFrame.y;
980983

981984
if (this.moves)
982985
{
@@ -1009,8 +1012,6 @@ var Body = new Class({
10091012

10101013
this.gameObject.x += dx;
10111014
this.gameObject.y += dy;
1012-
1013-
this._reset = true;
10141015
}
10151016

10161017
if (dx < 0)
@@ -1031,16 +1032,10 @@ var Body = new Class({
10311032
this.facing = CONST.FACING_DOWN;
10321033
}
10331034

1034-
this._dx = dx;
1035-
this._dy = dy;
1036-
10371035
if (this.allowRotation)
10381036
{
10391037
this.gameObject.angle += this.deltaZ();
10401038
}
1041-
1042-
this.prev.x = this.position.x;
1043-
this.prev.y = this.position.y;
10441039
},
10451040

10461041
/**

src/physics/arcade/World.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,7 @@ var World = new Class({
10461046
// We don't need to postUpdate if there wasn't a step this frame
10471047
if (this.stepsLastFrame)
10481048
{
1049+
this.stepsLastFrame = 0;
10491050
for (i = 0; i < len; i++)
10501051
{
10511052
body = bodies[i];

0 commit comments

Comments
 (0)