-1
\$\begingroup\$

http://www.gameprogblog.com/generic-game-loop/

Java

boolean onLoop( Game game, GameState state, GameInput input, Graphics2D gr ) {
  long nanosElapsed = state.tick();
  updateTime += nanosElapsed;
  int updateCount = 0;
  while (updateTime >= updateRate && updateCount < maxUpdates) {
    game.input( input );
    input.clear();
    if (!game.isPlaying()) {
      return false;
    }
    state.update();
    game.update( state );
    if (!game.isPlaying()) {
      return false;
    }
    updateCount++;
    updateTime -= updateRate;
  }
  drawTime += nanosElapsed;
  int drawCount = 0;
  if (drawTime >= drawRate || updateCount > 0) {
    state.interpolate = getStateInterpolation();
    state.forward = state.interpolate * state.seconds;
    state.backward = state.forward - state.seconds;
    state.draw();
    game.draw( state, gr );
    drawCount++;
    drawTime -= (drawRate == 0 ? drawTime : drawRate);
  }
  if (sleep && drawCount == 0 && updateCount == 0) {
    long actualTime = updateTime + state.getElapsedSinceTick();
    long sleep = (updateRate - actualTime) / 1000000L;
    if (sleep > 1) {
      try {
        Thread.sleep( sleep - 1 );
      } catch (Exception e) { }
    }
  }
  return (drawCount > 0);
}

I am a beginner in game programming. I wanted to know when using fixed time step why do we have different update rates and draw rates. I have seen this in more than just the above code and if i plan to make a 30 fps game then what is a reasonable draw rate to have to match 30 fps update rate?. Can you explain in understandable terms? I appreciate your help.

\$\endgroup\$
  • \$\begingroup\$ Optimization and consistency \$\endgroup\$ – Raxvan Nov 26 '13 at 17:47
  • \$\begingroup\$ The article you linked explains everything very clearly, perhaps you should reread that, or ask for clarification on what part you don't understand. \$\endgroup\$ – AttackingHobo Nov 26 '13 at 17:57
0
\$\begingroup\$

http://gafferongames.com/game-physics/fix-your-timestep/

The short version is that there can be a huge variation in frame rate due to a myriad of factors, many outside of the game's control. These variations can cause a lot of bugs and other gameplay oddities, especially in physics. Using a fixed timestep for updates avoids bugs.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.