title: gameloop and deltatime interpolation c++
body: <p>I have a doubt about this gameloop implementation:</p>
  
  <pre><code>#include &lt;chrono&gt;
  #include &lt;iostream&gt;
  
  using namespace std::chrono_literals;
  
  // we use a fixed timestep of 1 / (60 fps) = 16 milliseconds
  constexpr std::chrono::nanoseconds timestep(16ms);
  
  struct game_state {
    // this contains the state of your game, such as positions and velocities
  };
  
  bool handle_events() {
    // poll for events
  
    return false; // true if the user wants to quit the game
  }
  
  void update(game_state *) {
    // update game logic here
    std::cout &lt;&lt; "Update\n";
  }
  
  void render(game_state const &amp;) {
    // render stuff here
    //std::cout &lt;&lt; "Render\n";
  }
  
  game_state interpolate(game_state const &amp; current, game_state const &amp; previous, float alpha) {
    game_state interpolated_state;
  
    // interpolate between previous and current by alpha here
  
    return interpolated_state;
  }
  
  int main() {
    using clock = std::chrono::high_resolution_clock;
  
    std::chrono::nanoseconds lag(0ns);
    auto time_start = clock::now();
    bool quit_game = false;
  
    game_state current_state;
    game_state previous_state;
  
    while(!quit_game) {
      auto delta_time = clock::now() - time_start;
      time_start = clock::now();
      lag += std::chrono::duration_cast&lt;std::chrono::nanoseconds&gt;(delta_time);
  
      quit_game = handle_events();
  
      // update game logic as lag permits
      while(lag &gt;= timestep) {
        lag -= timestep;
  
        previous_state = current_state;
        update(&amp;current_state); // update at a fixed rate each time
      }
  
      // calculate how close or far we are from the next timestep
      auto alpha = (float) lag.count() / timestep.count();
      auto interpolated_state = interpolate(current_state, previous_state, alpha);
  
      render(interpolated_state);
    }
  }
  </code></pre>
  
  <p>I need to know how i should implement the deltaTime and interpolation,
  to make a smooth "world object" movement. Is this a good implementation of "advance" using deltaTime and interpolation? or should be different? for example:</p>
  
  <p>Example: </p>
  
  <pre><code>obj* myObj = new myObj();
  
  float movement = 2.0f;
  myObj-&gt;position.x += (movement*deltaTime)*interpolation;
  </code></pre>
  
  <p>i need some help about the use of interpolation an deltatime.</p>
  
  <p>Thanks!</p>