1

As I have found out in my previous question, window.setInterval is not the best choice for a cross-platform game loop. What is my alternative for a game loop in Javascript that will (attempt to) run at the same speed on all devices?

1 Answers1

2

You shouldn't use equal intervals for game loop. Instead of this use time_delta (current_time - last_update_time). Send time_delta to game object's update method and update position and other parameters depending on time_delta. For example, simple object moving with const velocity will be:

function update(time_delta) {
    position = position + velocity * time_delta;
}

In this case, you don't need to set fixed frame per second and allow game to update state as fast as possible.

Kostya Regent
  • 548
  • 3
  • 7
  • 4
    Good but rather incomplete answer. I'll just leave this here: http://gafferongames.com/game-physics/fix-your-timestep/

    In sum, use fixed step delta times, it gives better accuracy for physics integration and is more predictable.

    – Grimshaw Dec 05 '14 at 15:44
  • you could simplify this to position += velocity * time_delta – ndugger Dec 17 '14 at 14:36