time arrives in equal slices
alreadyYou know the game has one Game value and can read an accumulator in update.ts.
hereHow elapsed time becomes equal simulation slices, what the cap does after a long pause, and why display rate does not change the ball path.
Page: time arrives in equal slices
Job: explanation
Level: mid grug (level 2 of 6)
Source lines:
why grug give these 3 tool notes
why grug give the proof why grug give the grill why grug give the why under an answerthis page says why the code has this shape and what a reader must be careful of.
This page teaches how the loop turns uneven frame times into equal simulation steps, so the ball travels one fixed distance per step on any monitor.
why
The loop spends elapsed time in equal slices. advance at update.ts:11#advance adds a frame's elapsed time to the accumulator, then calls step with FIXED_STEP until that time is spent. The constant at update.ts:6#120 is one hundred twentieth of a second, so every call moves the ball the same distance and a bounce lands in one place whatever the refresh rate. Leftover time stays in the accumulator for the next frame, which is the catch-up in the picture above: the row consumes what the frames poured in, slice by slice.
Two caps bound the wait. FRAME_TIME_CAP at update.ts:8#0.25 limits the time one frame may pour in, so a tab that slept for ten seconds asks for the work of a quarter second. MAX_STEPS_PER_FRAME at update.ts:7#stepcap bounds how many slices one frame may spend, and reaching that bound clears the remainder. The frame loop at main.ts:9#clock hands advance the time the caller measured.
what breaks
A variable step makes the ball's position depend on the monitor. A bounce that catches a paddle at 60 Hz can miss at 144 Hz, and a replay of the same input gives two different scores.
A missing cap creates a different failure. The game catches up on a paused tab, the frame takes seconds, and the window looks frozen just when it should resume.
where it fits
main.ts calls frame once per rendered frame, and frame calls advance once. The step function owns paddles, ball, collision and scoring. why the loop pays for a fixed step records why this loop was chosen.
try it
Change BALL_SPEED and watch the ball cross the window faster while FIXED_STEP stays at one hundred twentieth. Change FIXED_STEP and count the steps in one frame: a smaller step means more steps, and the ball keeps the same screen speed. The proof prints the decisive line, update.ts:6#120.
Run the source check from the repository root:
grep -n 'FIXED_STEP =' update.ts
run grep -n 'FIXED_STEP =' update.ts
It prints:
6:export const FIXED_STEP = 1 / 120; // 120: one slice is one hundred twentieth of a second
The cited file update.ts ships with this site at the same path. Run the command from the school folder, or open update.ts.
reference: reading the advance function line by line
advance at update.ts:11#advance does four things in order. It answers the restart flag first, resetting both scores and serving. It adds Math.min(frame_time, FRAME_TIME_CAP) to the accumulator, so no frame pours in more than a quarter second. It runs the catch-up loop while (game.accumulator >= FIXED_STEP && steps < MAX_STEPS_PER_FRAME), spending one slice per turn. And when steps reaches the per-frame bound, it clears the accumulator so the dropped remainder never compounds.
Inside every slice, step moves the paddles, counts the serve wait down or moves and resolves the ball, then asks score_if_out. The x move at update.ts:36#xpass and the y move at update.ts:38#ypass bracket the two collision passes. One slice is the whole game's tick; the frame only decides how many ticks run.
reference: every connection to this page
This is reference. The next page in the reading order is at the bottom of the page.
learn first: one value holds the round
goes on to: two collision passes, two answers, why the loop pays for a fixed step, change one rule, follow the result, when this shape stops fitting
flowchart LR A0["one value holds the round"] H["you are here: time arrives in equal slices"] B0["two collision passes, two answers"] B1["why the loop pays for a fixed step"] B2["change one rule, follow the result"] B3["when this shape stops fitting"] A0 --> H H --> B0 H --> B1 H --> B2 H --> B3
this badge records quiz answers with a difficulty from 1 to 6. it does not measure independence or responsibility.
Useful Resources
Outside material, checked when this school was built.
- Fix Your Timestep! | Gaffer On Games article
The original account of accumulator loops and the spiral of death, which explains the two caps in our update file.
grill: check what you know
Grug asks. Say the answer out loud, then tap. Grug remembers what you miss and asks again in the pop quiz.
the number is a question's difficulty from 1 to 6. a coloured chip marks a question that asks for a change, an effect or a judgment, declared in the quiz row. the rung badge records quiz answers: two right at a level and the levels below it, with a declared transfer question at level 5 or 6. it does not certify independence or responsibility. the questions are level 1 of 6 recall a fact; level 2 of 6 recall a detail; level 3 of 6 trace a path; level 4 of 6 trace a longer path; level 5 of 6 predict or judge; level 6 of 6 judge a transfer.
try first, then show answer
3. The length of one simulation step
FIXED_STEP at update.ts:6#120 is one step of simulation time. Window size and scores live in state.ts.
try first, then show answer
2. To turn elapsed frame time into whole steps
advance at update.ts:11#advance adds elapsed time to the accumulator and spends it in equal slices. The accumulator holds spare time between frames; it does not hold points.
try first, then show answer
3. FRAME_TIME_CAP and MAX_STEPS_PER_FRAME
FRAME_TIME_CAP at update.ts:8#0.25 bounds the time added in one frame. MAX_STEPS_PER_FRAME at update.ts:7#stepcap bounds the loop. Together they stop a long pause from freezing the next frame.
try first, then show answer
1. The accumulator is cleared
The branch at update.ts:24#droptime sets the accumulator to zero after the limit is reached. Pending time is dropped, the window stays open, and the next frame starts fresh.
try first, then show answer
2. Run ten seconds' worth of catch-up steps, thousands of them
Without FRAME_TIME_CAP at update.ts:8#0.25, a ten second pause adds ten seconds to the accumulator and the catch-up loop runs thousands of slices before the next frame can draw. The cap bounds one frame's pour to a quarter second.
try first, then show answer
2. Two, and the half slice stays behind
The catch-up loop inside advance at update.ts:11#advance runs while the accumulator holds at least FIXED_STEP. Two and a half slices pay for two steps and leave the half slice in the accumulator for the next frame; the loop never rounds up.
try first, then show answer
accumulator
The accumulator field of the Game value at state.ts:6#aggregate holds time not yet spent. advance at update.ts:11#advance adds a frame's elapsed time to it, spends one FIXED_STEP per turn, and leaves the remainder for the next frame. A worker that saves an unfinished batch for its next tick keeps the same kind of leftover, so the field to name is the accumulator.