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

Author Topic: Question About Fixed Time Step  (Read 1492 times)

0 Members and 1 Guest are viewing this topic.

ElysianShadow

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Question About Fixed Time Step
« on: June 18, 2014, 10:14:40 pm »
I have a question about some of the code involved using a fixed time step. Here is some example code:



const float TimePerFrame = 1 / 60.0f;

sf::Time timeSinceUpdate = sf::Time::Zero;

while ( window.isOpen() )
{
        sf::Time elapsed = clock.restart();
        timeSinceUpdate += elapsed;
       
        while ( timeSinceUpdate > TimePerFrame )
        {
                timeSinceUpdate -= TimePerFrame; // This is the part I don't understand. Why do this?
               
                handleEvents();
               
                update( TimePerFrame );
        }
       
        render();
}

 

I don't understand why you do the line that I commented? Also, is this code suppose to run the game at 60 fps? Because when I run it, the frame rate is over 9000. Sorry if it's a very simple question, but maybe I'm over thinking it and don't see something obvious. Thanks.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Question About Fixed Time Step
« Reply #1 on: June 18, 2014, 10:23:48 pm »
Without that line, the while ( timeSinceUpdate > TimePerFrame ) loop will never stop executing.

Tuffywub

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Question About Fixed Time Step
« Reply #2 on: June 18, 2014, 10:33:13 pm »
it might make more sense for timeSinceUpdate to be called unprocessedTime. It keeps track of how much time has not been updated. When you run an update, you must remove the amount of time you are updating from the unprocessedTime.

Also, frame rate is different from update rate. Update should be called at 60 times per second currently, but the way you code is written, render() is called as fast as possible. IMO, this is a good thing: if you want there to be a cap on the fps use vSync.

ElysianShadow

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: Question About Fixed Time Step
« Reply #3 on: June 18, 2014, 10:33:31 pm »
Yeah, I was definitely over thinking it.

Anyways, thanks for the responses guys

 

anything