Jump to content

Velocity affected by frame rate?


Recommended Posts


So I have a loop that checks the position of a moving sprite every 100ms. At each iteration, it computes how many milliseconds elapsed since last check (it's normally 100 but it varies a bit), computes the euclidean distance between the current position and the previous one, and compares it to the maximum distance that the sprite could have traveled given its speed and the elpased time. See below:

function myFunction(){
    // nevermind variables showing up out of nowhere, assume everything global or sth, not the point
    var currentData = {
        x: sprite.position.x,
        y: sprtie.position.y,
        stamp: Date.now()
    var deltaT = (currentData.stamp - lastData.stamp);
    var euclideanDistance = Math.floor(Math.sqrt(Math.pow(lastData.x - currentData.x, 2) + Math.pow(lastData.y - currentData.y, 2)));
    var maxDistance = Math.ceil(shipSpeed * (deltaT / 1000));
    if(euclideanDistance > maxDistance) console.log('alert!');
    lastData = currentData; // it's actually cloned, but that's not the point

The velocity of the sprite is set in the update loop using:

game.physics.arcade.velocityFromAngle(angle, shipSpeed, Game.playerSprite.body.velocity);

(based on the key pressed, the angle is computed, and from the angle, the velocity).

My understanding of velocity in Phaser is that it should remain the same and that it's adjusted for the frame rate, therefore the distance traveled per millisecond should remain the same. This is actually the case most of the time in normal conditions. But when I run this in my laggy Firefox, very often do I get values for `euclideanDistance` that exceed the `maxDistance`, sometimes by as much as 5. Even though I ceil() the maxdistance and floor() the euclideanDistance, to avoid mismatches of 1 or 2 pixels.

For concrete values, the value of shipSpeed is 250. In 100ms it is supposed to travel a distance of 25, yet I regularly get 29 for example. (Btw my function doesn't often ticks at 100ms, the deltaTime is often as low as 83 ms or as high as 105ms, but that's plainly explainable by lag I guess.)

Therefore it seems that the lag and variations in FPS affect the distance traveled, but not systematically. It's a bit puzzling. Or is it something else? Am I computing my variables wrong? Am I missing misunderstanding something about the physics? Any idea about an explanation and, possibly, a fix, to enforce that the distance traveled is always the same, down to the pixel, no matter what?

Link to comment
Share on other sites

I don't understand all of Phaser's timing, but my impressions are

  • Phaser uses game.time.physicsElapsed for physics calculations and recommends you do the same
  • physicsElapsed is a constant (for a given desiredFps) equal to 1s / desiredFps
  • When game.forceSingleUpdate=false, Phaser calls update a variable number of times to match FPS. If it can't keep up, it drops/skips the pending updates.
  • When game.forceSingleUpdate=true (default), Phaser calls update once per render frame no matter what. If the render rate falls, the update rate falls too and the game slows down.

Phaser doesn't interpolate motion so for short intervals (~100ms) the actual elapsed time isn't really relevant. It only matters how many updates have run. 

Link to comment
Share on other sites


  • Recently Browsing   0 members

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