0:01 / 0:30
Playback speed

Delta time

Follow the two runners as the frame rate falls. Which one still covers the same distance per second?

#MotionAndFeel

The time since the last frame. Multiply movement by it and speed stays the same on any computer.

The bar shows the frame rate dropping from 60 to 12. The top monkey moves a fixed step each frame, so it slows to a crawl. The bottom one moves by speed × delta time: choppier, but it covers the same ground.

Code

src/demos/delta_time.goose
namespace delta_time_lesson;
import stage;
import std;
import math;

fn bounce(distance: f64) -> f64 { 126.0 - abs(distance % 252.0 - 126.0) }

export "delta-time" fn draw(time: f64, dt: f64, px: f64, py: f64, hover: bool) {
    artwork("draft");
    let t = time % 4.0;
    let cycle = floor(time / 4);
    let slow = t >= 2;
    let frames = cycle * 144 + floor(if slow { 120 + (t - 2) * 12 } else { t * 60 });
    let seconds = if slow { cycle * 4 + floor(t * 12) / 12 } else { time };
    rect(0, 0, W, 10, INK);
    for i in (if slow { 12 } else { 60 }) {
        rect(4 + i * (if slow { 12.6 } else { 2.52 }), 3, if slow { 9.0 } else { 1.6 }, 4, if slow { RED } else { MOSS });
    }
    rect(0, 48, W, 1, LINE);
    rect(0, 90, W, 1, LINE);
    actor(16 + bounce(frames * 0.55), 48, floor(frames / 8) % 4.0, 2, "rose", "runner", (frames * 0.55) % 252 >= 126);
    actor(16 + bounce(seconds * 33), 90, floor(seconds * 7.5) % 4.0, 2, "moss", "runner", (seconds * 33) % 252 >= 126);
}

Stage drawing helpers · All demos on GitHub

Comments

No comments yet.