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

Author Topic: Fixed timestep CPU usage  (Read 2844 times)

0 Members and 1 Guest are viewing this topic.

ScArL3T

  • Newbie
  • *
  • Posts: 32
    • View Profile
Fixed timestep CPU usage
« on: February 12, 2016, 11:34:16 pm »
Hello. I have implemented a fixed timestep. The problem with this is that my CPU usually goes to about 30-40% and I don't understand why. A solution would be to just use the setFramerateLimit method or vsync, but I don't understand why would I need a fixed timestep if I use one of those. If someone can shed some light here that would be much appreciated.

const sf::Time m_frameTime = sf::seconds(1.f/60.f);
sf::Clock clock;
sf::Time passedTime = sf::Time::Zero;

while (window.isOpen())
{
        sf::Time elapsedTime = clock.restart();
        passedTime += elapsedTime;

        while (passedTime > m_frameTime)
        {
                passedTime -= m_frameTime;

                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
                update(m_frameTime);
        }
               
        window.clear()
        window.display();

}

TCVM

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Fixed timestep CPU usage
« Reply #1 on: February 12, 2016, 11:39:05 pm »
If you use setFramerateLimit, it sets the upper bound which window can update at. Can't go above 60 (for example). However, what if your program goes under 60? Then your physics become wrong, and you suddenly have very weird behavior

If you don't setFramerateLimit, it really charges your CPU unnecessary amounts of power. Especially for a 2d program. So it's generally good to use the two combined

ScArL3T

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: Fixed timestep CPU usage
« Reply #2 on: February 12, 2016, 11:44:28 pm »
So basically, the fixed timestep is just for a smooth update method and eventually smooth rendering by interpolating?

TCVM

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Fixed timestep CPU usage
« Reply #3 on: February 12, 2016, 11:53:02 pm »
Well, not exactly. If you have a simulation you want to run the same way, every time, a timestep becomes important.

http://gafferongames.com/game-physics/fix-your-timestep/
Don't just read the code, but read the text chunks too. They describe what the code does, and why it is used

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Fixed timestep CPU usage
« Reply #4 on: February 13, 2016, 11:17:35 am »
Fixed time steps are just one strategy to keep your game's update rate/game logic speed at a constant and intended value, no matter whether the game runs at 6, 60, or 6000 frames per second.

What happens right now is that your main/drawing loop simply runs as fast as possible, so even if there's no logic update/time step yet, it will still redraw the screen (although this is not really needed).

You should enable vertical synchronization by default, possibly only for release builds to not hide significant bottlenecks/slowdowns. Then set the maximum framerate to something considerably higher, e.g. 200 fps (since people might want to run the game on a 144 Hz screen).

ScArL3T

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: Fixed timestep CPU usage
« Reply #5 on: February 13, 2016, 11:29:07 am »
Seems like I missunderstood the usage of a fixed timestep. Thank you guys for your help!  ;D