Jump to content

Sprite's Velocity depends on Frame Rate


omidh
 Share

Recommended Posts

I'm trying to create a multiplayer game which players have a sprite that they can move it. 

I wrote this simple code to move sprites:

function update() {
      theSprite.y += speedOnY;
      theSprite.x += speedOnX;
}
   

It works fine in single player games but because this is a multiplayer one, the sprites position must be synced however sprite's speed is dependent to player's current frame rate. If frame rate drops then speed drops and causes desync. The question is how can I move my sprites in a constant speed and independent of the frame rate?

Link to comment
Share on other sites

 

If I recall correctly, this is what Delta Time on the Love2D framework is for. 

from the wiki: 

Quote

dt is the most common shorthand for delta-time, which is usually passed through love.update to represent the amount of time which has passed since it was last called. It is in seconds, but because of the speed of modern processors is usually smaller than 1 (values like 0.01 are common).

 

 

Quote

 

What is DT?

Delta Time, also known as dt, is how much time has passed between 2 frames, hence the "delta" part of the word. Delta Time is also approximately equal to the reciprocal of the current frames per second (FPS), and consequently, FPS is approximately equal to the reciprocal of Delta Time:

1
2
dt = 1/fps
fps = 1/dt

 

 

and the application:

 

 

 

Quote

 

Constant speed

Another way to use DT is to make objects move at constant speed. By constant I mean that it ideally looks the same at any framerate (I said ideally because DT isn't exact, but it's okay for most purposes). You can do that by multiplying the speed by DT. This has the effect of the object moving at that speed every second.

1
2
3
4
function love.update(dt)
    -- this has the effect of moving at a set speed every second, for example, 500 pixels every second
    object.x = object.x + speed*dt
end

Constant Acceleration

Another way to use DT is to add constant acceleration. Like before, by constant I mean it looks the same at any machine, slow or fast. You can do this by adding speed by acceleration multiplied by dt.

1
2
3
4
function love.update(dt)
    speed = speed + accel*dt
    object.x = object.x + speed*dt
end

 

 
I imagine you could try something like that for Phaser (Maybe is already done?)
 
 

 

Link to comment
Share on other sites

12 hours ago, samme said:

function create() { // or init()
    game.time.forceSingleUpdate = false;
}

 

Thanks but time property is extra here.

After setting it to false I noticed that fpsProblemNotifier is dispatched periodically every 7~8 seconds and in the same time fps indicator is constant 60 however I feel the lag on intervals. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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