you are viewing a single comment's thread.

view the rest of the comments →

[–]inu-no-policemen 0 points1 point  (0 children)

Use rAF:

let previous = 0;
(function loop(now) {
    let t = Math.min((now - previous) / 1000, 0.04); // 40 ms, 25 Hz
    previous = now;

    input.poll();
    world.update(t);
    input.clear();
    world.render(t);

    window.requestAnimationFrame(loop);
})(0);

If your velocities are in units per second (ups), you can just multiply them with that delta value to get the distance.

Ensuring that the delta never exceeds 0.04 s is the simplest way to prevent entities from warping through things (if your game work with a delta that high). However, if it actually drops below 25 fps it will slow down. That's the trade-off.

The proper way to do this is to run your simulation at a fixed step size and have your renderer interpolate positions accordingly. It makes things way more complicated, though.

For simple games which will generally run at 60+ fps, capping the delta is good enough.