Jump to content

physics/frame-rate bug (& fix)


jcs
 Share

Recommended Posts

hello all,

 

I noticed that in my platformer game the player sprite jumps much higher than he ought to when the frame-rate drops. (This is very clear running on mobile devices). You can also see this in the "starstruck" example if you add code into update() that will cause the frame-rate to drop (such as 10000 Math.sqrts or somesuch).

 

I believe there is an issue with the application of gravity/acceleration/drag. The frame-rate should be accounted for both in the velocity and position (or rotation) calculations, but is only accounted for in the position.

 

In ArcadePhysics.js:

 

this._velocityDelta = (this.computeVelocity(1, body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x) - body.velocity.x) / 2;

 

ought to be:

 

this._velocityDelta = (this.computeVelocity(1, body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x) - body.velocity.x) * this.game.time.physicsElapsed * 0.5 * 60;

 

(and likewise for the rotation and y-axis calculations)

 

This implements the formulae found at http://www.niksula.hut.fi/~hkankaan/Homepages/gravity.html

 

I have made this change and my game works correctly at 60fps and at the much lower frame rates I get on mobile devices (30-45fps on average). (starstruck and all the examples seem to work as well). I have this in a forked repo on github; I can enter a pull request if the dev team would like (but it's just 3 lines so someone may want to fix it themselves).

 

cheers,

 

josh

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...