I am getting some unexpected behavior I don't understand. I am trying to implement a fixed variable time step as described from http://gafferongames.com/game-physics/fix-your-timestep/ and http://gameprogrammingpatterns.com/game-loop.html. My inner while loop never evaluates as true when I run the program in visual studio; however, when I uncomment 'this_thread::sleep_for(1s)', the while loop will evaluate as a true after the outer loop executes 10 times or so. I would expect on faster machines, this would be the expected outcome.
So my question is why would the inner loop only evaluate as true when the 'this_thread::sleep_for(1s)' is uncommented? Shouldn't lagTime become greater than fixedDeltaTime regardless of the sleep_for? Does the math take too much time for the computer or something?
int updateCount = 0;
double CLOCKS_PER_MILLISECOND((double)CLOCKS_PER_SEC * 1000.0);
double previousTime = std::clock() / CLOCKS_PER_MILLISECOND;
double newTime = 0, deltaTime = 0, lagTime = 0;
double fixedDeltaTime = 1.0 / 60.0;
while (gameIsRunning)
{
newTime = std::clock() / CLOCKS_PER_MILLISECOND;
deltaTime = newTime - previousTime;
previousTime = newTime;
lagTime += deltaTime;
inputSystem.update();
sceneManager.update();
while(lagTime >= fixedDeltaTime) // never evalutes as true?
{
physicsSystem.update();
lagTime -= fixedDeltaTime;
updateCount++;
}
std::cerr << "-----" << updateCount << "-----" << std::endl;
physicsSystem.interpolate(lagTime / fixedDeltaTime);
renderSystem.update();
audioSystem.update();
updateCount = 0;
//std::this_thread::sleep_for(1s);
}
Thank you for any help!