Skip to content

Game loop variable time step code snippet - elapsed time not correctly computed #396

Description

@bgtdsword

In the snippet that introduces the variable time step:

double lastTime = getCurrentTime();
while (true)
{
  double current = getCurrentTime();
  double elapsed = current - lastTime;
  processInput();
  update(elapsed);
  render();
  lastTime = current;
}

the lastTime is computed at the wrong time: in that configuration, lastTime and current are computed directly after one another.
If I'm not mistaken, the correct snippet would be:

double lastTime = getCurrentTime();
while (true)
{
  double current = getCurrentTime();
  double elapsed = current - lastTime;
  lastTime = current;
  processInput();
  update(elapsed);
  render();
}

The next snippet after that gets it right.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions