Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to restart the clock when it gets to a value  (Read 6083 times)

0 Members and 1 Guest are viewing this topic.

baguettio

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
How to restart the clock when it gets to a value
« 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.

Fx8qkaoy

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: How to restart the clock when it gets to a value
« Reply #1 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

baguettio

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: How to restart the clock when it gets to a value
« Reply #2 on: March 08, 2020, 02:22:55 pm »
On a key press?

Fx8qkaoy

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: How to restart the clock when it gets to a value
« Reply #3 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

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to restart the clock when it gets to a value
« Reply #4 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. 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, 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 library, although it's still a small library anyway.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything