Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
Optimization and consistency –  Raxvan Nov 26 '13 at 17:47
    
The article you linked explains everything very clearly, perhaps you should reread that, or ask for clarification on what part you don't understand. –  AttackingHobo Nov 26 '13 at 17:57
add comment

1 Answer 1

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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