SFML community forums

Help => System => Topic started by: baguettio on March 07, 2020, 10:09:02 pm

Title: How to restart the clock when it gets to a value
Post by: baguettio on March 07, 2020, 10:09:02 pm
I'm multiplying the movement in my game by the time since the last time movement was applied, which causes problems when you don't do anything for a while. Doing this:
if (loopclock.getElapsedTime().asSeconds() > 1){loopclock.restart();}
doesn't work for some reason, another solution to my movement system would also be appreciated.
Title: Re: How to restart the clock when it gets to a value
Post by: Fx8qkaoy on March 08, 2020, 08:07:05 am
I'm multiplying the movement in my game by the time since the last time movement was applied, which causes problems when you don't do anything for a while. Doing this:
if (loopclock.getElapsedTime().asSeconds() > 1){loopclock.restart();}
doesn't work for some reason, another solution to my movement system would also be appreciated.

To me looks like u let the clock still going while u are not moving. Make sure to also restart the clock when u start moving
Title: Re: How to restart the clock when it gets to a value
Post by: baguettio on March 08, 2020, 02:22:55 pm
On a key press?
Title: Re: How to restart the clock when it gets to a value
Post by: Fx8qkaoy on March 08, 2020, 06:54:14 pm
On a key press?

When u start moving. If u start on a key press, then yeah
Title: Re: How to restart the clock when it gets to a value
Post by: Hapax on March 10, 2020, 12:00:20 am
Another (simple) approach could be to restart the clock every frame (or loop/cycle/tick) but keep track of the amount of time passed and just add the duration of each frame to that total time.

So, your current code would look something like this:
sf::Time time;
sf::Clock clock;

sf::Time frameTime{ clock.restart(); } // sf::Clock's restart also returns the current elapsed time before restarting.
time += frameTime;

if (time.asSeconds() > 1.f) { time = sf::seconds(1.f); }

But, you could 'subtract' 1 each time so that it's still within the 0-1 range but in the correct part.
For example, if it's actually at 1.2 seconds, it's the 1 and also 0.2 of the next one. So, subtract 1 to get the 0.2.
However, to account for longer times, you'd need to loop the subtraction, thus:
while (time.asSeconds() > 1.f) { time -= sf::seconds(1.f); } //  instead of "if (time.asSeconds...."

This seems to be what you are asking about - keeping it within the looping 1 - but I'm not completely certain what your aim is.


Also, if you'd like to read about more clocking approaches, consider reading about time steps; most specifically: https://gafferongames.com/post/fix_your_timestep/

If you find the article too complicated (I did at first too, for sure) or maybe grasped some but not all, you can consider using Kairos' Timestep Lite (https://github.com/Hapaxia/Kairos/wiki/Timestep-Lite). It's a small class that can help control your time step automatically. It's basically an implementation of the approach in that article.
If you'd like fuller control, you can use Kairos' Timestep (https://github.com/Hapaxia/Kairos/wiki/Timestep), which is similar but doesn't need external clocks, allows changing the speed of time and also provides alphas for frame interpolation. However, it does require all of the Kairos (https://github.com/Hapaxia/Kairos/wiki) library, although it's still a small library anyway.