Skip to content

Commit b8dbb5d

Browse files
committed
Take dt into account for gravity calculations
1 parent 388e42c commit b8dbb5d

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

src/physics/arcade/ArcadePhysics.js

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,29 @@ Phaser.Physics.Arcade.prototype = {
172172
// If you're wondering why the velocity is halved and applied twice, read this: http://www.niksula.hut.fi/~hkankaan/Homepages/gravity.html
173173

174174
// Rotation
175-
this._velocityDelta = (this.computeVelocity(0, body, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular) - body.angularVelocity) * 0.5;
175+
this._velocityDelta = (this.computeVelocity(body, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular, 0) - body.angularVelocity) * 0.5;
176176
body.angularVelocity += this._velocityDelta;
177177
body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);
178178
body.angularVelocity += this._velocityDelta;
179179

180+
if(body.allowGravity)
181+
{
182+
// Gravity was previously applied without taking physicsElapsed into account
183+
// so it needs to be multiplied by 60 (fps) for compatibility with existing games
184+
this._gravityX = (this.gravity.x + body.gravity.x) * 60;
185+
this._gravityY = (this.gravity.y + body.gravity.y) * 60;
186+
} else {
187+
this._gravityX = this._gravityY = 0;
188+
}
189+
180190
// Horizontal
181-
this._velocityDelta = (this.computeVelocity(1, body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x) - body.velocity.x) * 0.5;
191+
this._velocityDelta = (this.computeVelocity(body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x, this._gravityX) - body.velocity.x) * 0.5;
182192
body.velocity.x += this._velocityDelta;
183193
body.x += (body.velocity.x * this.game.time.physicsElapsed);
184194
body.velocity.x += this._velocityDelta;
185195

186196
// Vertical
187-
this._velocityDelta = (this.computeVelocity(2, body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y) - body.velocity.y) * 0.5;
197+
this._velocityDelta = (this.computeVelocity(body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y, this._gravityY) - body.velocity.y) * 0.5;
188198
body.velocity.y += this._velocityDelta;
189199
body.y += (body.velocity.y * this.game.time.physicsElapsed);
190200
body.velocity.y += this._velocityDelta;
@@ -195,32 +205,21 @@ Phaser.Physics.Arcade.prototype = {
195205
* A tween-like function that takes a starting velocity and some other factors and returns an altered velocity.
196206
*
197207
* @method Phaser.Physics.Arcade#computeVelocity
198-
* @param {number} axis - 1 for horizontal, 2 for vertical.
199208
* @param {Phaser.Physics.Arcade.Body} body - The Body object to be updated.
200209
* @param {number} velocity - Any component of velocity (e.g. 20).
201210
* @param {number} acceleration - Rate at which the velocity is changing.
202211
* @param {number} drag - Really kind of a deceleration, this is how much the velocity changes if Acceleration is not set.
203212
* @param {number} [max=10000] - An absolute value cap for the velocity.
213+
* @param {number} gravity - The acceleration due to gravity. Gravity will not induce drag.
204214
* @return {number} The altered Velocity value.
205215
*/
206-
computeVelocity: function (axis, body, velocity, acceleration, drag, max) {
216+
computeVelocity: function (body, velocity, acceleration, drag, max, gravity) {
207217

208218
max = max || 10000;
209219

210-
if (axis == 1 && body.allowGravity)
211-
{
212-
velocity += this.gravity.x + body.gravity.x;
213-
}
214-
else if (axis == 2 && body.allowGravity)
215-
{
216-
velocity += this.gravity.y + body.gravity.y;
217-
}
218-
219-
if (acceleration !== 0)
220-
{
221-
velocity += acceleration * this.game.time.physicsElapsed;
222-
}
223-
else if (drag !== 0)
220+
velocity += (acceleration + gravity) * this.game.time.physicsElapsed;
221+
222+
if (acceleration === 0 && drag !== 0)
224223
{
225224
this._drag = drag * this.game.time.physicsElapsed;
226225

0 commit comments

Comments
 (0)