0
How to slow down the movement of player in SFML without using frame rate limit
How to slow down the movement of player in SFML without using frame rate limit. I know this can be done with using delta time. Does anyone know about delta time and how does it work. Please explain. I will really appreciate your help.
1 Réponse
+ 1
Delta Time is the time it took to process ("process", not render!) the last frame.
Making any simulation frame rate independent is done by scaling the parameters by "delta time", for example:
player.MoveForward(100);
This will move the player exactly 100 units forward every frame. But, say we have 2 machines where the player moves forward for the same duration on each one (say we hold 'W' for one second), one of them runs the game at 30 FPS, while the other is at 60 FPS.
In one second, the player on the first machine will have moved 3000 Units forward, while the second player would have moved 6000 units.
Delta time scaling is meant to scale the movement of the player (the 100 units) by the amount of time it takes to process a single frame, hence the higher the frame-rate, the slower the movement will be, in order to compensate for the slower machine.
How it works is like the following:
Machine 1 takes 0.033 (1 / 30) seconds per frame
Machine 2 takes 0.016 (1 / 60) seconds per frame
In order to make the players on both machines move at the same speed, machine 2 (the faster machine) will have to move the player at half the speed of the first one each frame.
Multiplying by "delta time" would work as follows:
player.MoveForward(100 * deltaTime);
On machine 1 (30 FPS) this is equal to:
player.MoveForward(100 * 0.0333); // 3.33
And the second machine (60 FPS) it's equal to:
player.MoveForward(100 * 0.0166); // 1.66
This means for:
Machine 1: 3.33 * 30 = 99.9
Machine 2: 1.66 * 60 = 99.6
As you can see, after scaling they both move at (roughly) the same speed, regardless of how many frames are being processes per second (the difference in the result is because of my calculations. In practice it would be almost identical).
In practice:
See this small example on how to calculate the deltaTime: https://code.sololearn.com/cQBHZw5Z7EW4