gameloop and deltatime interpolation c++

2.1k views Asked by At

I have a doubt about this gameloop implementation:

#include <chrono>
#include <iostream>

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 << "Update\n";
}

void render(game_state const &) {
  // render stuff here
  //std::cout << "Render\n";
}

game_state interpolate(game_state const & current, game_state const & 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<std::chrono::nanoseconds>(delta_time);

    quit_game = handle_events();

    // update game logic as lag permits
    while(lag >= timestep) {
      lag -= timestep;

      previous_state = current_state;
      update(&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);
  }
}

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:

Example:

obj* myObj = new myObj();

float movement = 2.0f;
myObj->position.x += (movement*deltaTime)*interpolation;

i need some help about the use of interpolation an deltatime.

Thanks!

1

There are 1 answers

0
dawyda254 On BEST ANSWER

Interpolation will always be used in a kind of predictive function - where a sprite will be in a future time. To answer your question on how to use the interpolation variable, you will need two update functions - one takes delta as an argument and the other takes the interpolation time. See example below on the functions (implement as per coding language you are using):

void update_funcA(int delta)
{
    sprite.x += (objectSpeedperSecond * delta);
}

void predict_funcB(int interpolation)
{
    sprite.x += (objectSpeedperSecond * interpolation);
}

As you can see above both functions do the same thing so having one called twice with the delta and interpolation values passed as arguments in each call would do but in some scenarios have two funcs will be better e.g. when dealing with gravity - physics game maybe. What you need to understand is the interpolation value is always a fraction of the intended loop time and thus you will need your sprites to move that fraction to ensure smooth movement regardless of framerate.