why the loop pays for a fixed step
alreadyYou have read the fixed-step lesson and know what the accumulator does.
hereWhy the fixed step was chosen, what a variable step would cost, and the cap that came with the decision.
Page: why the loop pays for a fixed step
Job: explanation
Level: senior grug (level 3 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 the tradeoff behind the fixed step: the monitor independence it buys, the accumulator machinery it costs, and why the cheaper alternative was refused.
why
The decision is written at docs/001-fixed-timestep.md:3#accepted. The cause is the ball's constant speed in world units. A frame time used directly as a step makes the same input land at different places on a 60 Hz screen and a 144 Hz screen, so a saved ball on one machine becomes a miss on another.
The price is an accumulator and a catch-up loop, and the note chose that price on purpose. The alternative it refused, feeding frame_time straight into step as dt, has no machinery at all, and that is exactly why it loses: the same two frames land at different places on a 60 Hz screen and a 144 Hz screen, so a bounce can miss a paddle on one machine and hit it on another. That failure is hard to reproduce on the machine where the code was written, because the code is unchanged; the monitor is. The fixed step also keeps step free of wall-clock reads, so the physics function stays testable with made-up numbers.
The cap is the second half of the cost. update.ts:17#min caps elapsed time before it enters the loop; without it, a paused tab converts into thousands of catch-up steps and a frozen frame at the moment the window should resume. The rest of the code gains a simpler contract: step takes a fixed dt and never asks what time it is.
what breaks
Dropping the accumulator and using frame_time as dt makes the physics follow the monitor. The bug shows up as a paddle that misses on a fast display, and it is hard to reproduce on the machine where the code was written.
Dropping the cap makes the catch-up loop run for seconds after a pause. The window freezes at the moment a player expects it to resume.
where it fits
time arrives in equal slices teaches the mechanism. This page explains the choice behind it. two collision passes, two answers and a goal changes one score, then waits both run inside step, so they inherit the fixed slice.
try it
Read the decision note, then change FRAME_TIME_CAP to a larger number and open a slow frame. The proof prints the decisive line, update.ts:8#0.25.
run grep -n 'FRAME_TIME_CAP =' update.ts
It prints:
8:export const FRAME_TIME_CAP = 0.25; // cap: a frame may pour in at most a quarter 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: every connection to this page
This is reference. The next page in the reading order is at the bottom of the page.
learn first: time arrives in equal slices
goes on to: change one rule, follow the result, when this shape stops fitting
flowchart LR A0["time arrives in equal slices"] H["you are here: why the loop pays for a fixed step"] B0["change one rule, follow the result"] B1["when this shape stops fitting"] A0 --> H H --> B0 H --> B1
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.
- Fixed timestep without interpolation | Jakub's tech blog article
Weighs render ticks against interpolation using a small game demo, so a reader sees the cost of our simpler loop.
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 ball landing differently on two refresh rates
The reason note names a 60 Hz screen and a 144 Hz screen in docs/001-fixed-timestep.md:11#60. A variable step makes the same input land at two places there.
try first, then show answer
1. In docs/001-fixed-timestep.md
The decision line is docs/001-fixed-timestep.md:3#accepted. The browser example shows a frame loop; this repository keeps its own notes under docs/.
try first, then show answer
2. An accumulator and a catch-up loop
advance at update.ts:11#advance spends an accumulator in equal slices. The loop is the price paid for repeatable physics.
try first, then show answer
3. To keep a paused tab from freezing the frame
update.ts:17#min bounds the time added in one frame. The cap keeps a long pause from turning into thousands of catch-up steps.
try first, then show answer
2. A paddle can miss a ball it would save on a 60 Hz screen, because the same frames land in different places
docs/001-fixed-timestep.md:3#accepted names the cause: the ball travels at a constant speed in world units, so a variable dt makes the same two frames land in different places at 60 Hz and 144 Hz. A paddle save on one monitor becomes a miss on the other.
try first, then show answer
2. The machine's own refresh rate is the one where the ball still saves
The variable-step failure follows the monitor. On the author's own machine the physics still saves the ball, so the bug hides until someone runs the same input on a display with a different refresh rate.