Skip to content

feat: add buffered snapshot interpolation - #146

Merged
Timerk merged 8 commits into
mainfrom
feat/snapshot-interpolation
Nov 26, 2026
Merged

Timerk merged 8 commits into
mainfrom
feat/snapshot-interpolation

Conversation

@Timerk

@Timerk Timerk commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Phase 3 of the real-time netcode rework (GAME_PLAN.md, Real-time netcode rework).

Stacked PR. Dependency chain: #144 (feat/network-simulator) → #145 (feat/netcode-ticks) → this PR. Merge in that order. After #145 merges, retarget this PR to main.

Behaviour

Remote pilots and aliens no longer lerp toward the newest snapshot. They are drawn about 100 ms in the past, between buffered server-tick snapshots.

  • SnapshotBuffer (scripts/snapshot_buffer.gd): one per remote entity. It holds up to 32 transforms keyed by server tick, in tick order, so out-of-order packets still fill gaps; stale and duplicate ticks are ignored.
    • Positions follow a cubic Hermite curve between the two bracketing samples, with tangents taken across neighbouring samples. Angles take the shortest path.
    • When the buffer runs dry, the entity extrapolates its last motion for at most 250 ms (MAX_EXTRAPOLATION_TICKS), then holds.
  • ServerClock (scripts/server_clock.gd): estimates server time from the median offset (snapshot tick / 60 minus arrival time) over the last 2 s.
    • The render time trails that estimate by INTERPOLATION_DELAY (100 ms, a playtesting setting).
    • It advances once per physics tick, in step with the local ship and camera, and time-scales by at most ±5% to close a filtered error. It snaps only past a 250 ms error.
  • Discontinuities clear the buffer and snap: spawn and launch, despawn and dock, a new life or alive state (respawn, death, jump), map change or visit, alien encounter reset or the end of a return home, and session start or end. The existing sequence, visit and life rejection is unchanged.
  • Non-transform state (health, shields, alive, docked, energy) still applies immediately. Death is not delayed to the render time, so a ship can vanish about one delay early. Its explosion appears at the authoritative position, which is within the explosion's size.
  • Effects: laser and explosion endpoints near an entity's newest received position are moved onto where it is drawn (FlightSession.drawn_point), so beams join the drawn ships.
  • F8 overlay and NetStats: interpolation delay, buffer depth min/mean, extrapolated share, underruns and clock drift.
  • Network schema 25.

Results

docs/netcode-baseline.md has the full before/after table. Before is the phase 2 build running the same extended benchmark. The machine was heavily loaded by unrelated jobs during these runs (13–38 fps with 3 extra clients), and both builds ran under the same load.

150 ± 30 ms, 5% loss Path error p95 Drawn accel p95 (server's own path: 60) Spike ticks Extrapolated
before (lerp), 3 extra clients 0.12 m 2999 m/s² 83.9% –
after, 3 extra clients 0.03 m 118 m/s² 3.6% 8.5%
before (lerp), 1 extra client 0.10 m 1610 m/s² 86.7% –
after, 1 extra client 0.01 m 81 m/s² 0.4% 1.0%

Tests

  • tests/snapshot_buffer_test.gd: bracketing, shortest-path angles, out-of-order and duplicate ticks, capacity, the extrapolation cap and hold, reset, and clock stability under ±30 ms jitter with 5% loss (no snaps, time-scale within ±5%, within 30 ms of ideal). Also checks latency-rise slewing and stall snapping.
  • tests/interpolation_network_test.gd: an observer at 150 ± 30 ms with 5% loss through the relay watches a moving, turning pilot. It bounds the render-time error against the server's path, drawn acceleration against the server's, the spike share and the extrapolated share. It also replays the old per-tick lerp from the same packets and asserts that the lerp stutters (about 80% spike ticks). Finally it checks that buffers clear on leave and disconnect.
  • Both tests are added to the Windows and Linux suites. rpc_compatibility_test now expects schema 25, and autopilot_test draws aliens at the newest tick.
  • ./tools/dev.ps1 check: 73 of 75 scripts passed in the full run.
    • dedicated_server_test (an admission timeout) passed when rerun on its own.
    • net_simulator_test "Lowering latency while running takes effect immediately" (a 12 ms raw-UDP timing tolerance) fails under the current machine load on the unmodified phase 2 base as well, 3 runs out of 3. It passes on an idle machine. Phase 3 does not touch the relay or that test.
  • Linux suites run in CI only; there is no local WSL distro.

Known limitations

  • No lag compensation. Remote targets are drawn about 100 ms plus the one-way latency in the past, but the server decides hits on current positions. Lag compensation is a possible future step.
  • The windowed two-client run was not done. There was no interactive session. The interpolation was checked headless through the relay test, the benchmark and the HUD/overlay tests instead.
  • Death is not delayed to the render time (see above).
  • Notes for phase 4: remote state for pilots carries no velocity, so tangents come from neighbouring samples. A velocity field in the public snapshot would allow true Hermite tangents. Snapshot ticks must stay exact, because the clock and buffers key on them.
  • Notes for phase 5: the relay test's bounds leave about 2× headroom over runs on a loaded desktop. Extrapolation rises when arrivals bunch into frames on slow hosts.

🤖 Generated with Claude Code

Timerk and others added 8 commits November 26, 2026 20:21
SnapshotBuffer keeps a remote entity's transforms by server tick and samples them at a render tick: Hermite interpolation between bracketing samples, linear extrapolation for at most 250 ms, then hold. ServerClock estimates server time from the median snapshot offset over 2 s and slews the render time, 100 ms behind it, by at most 5%.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Remote pilots and aliens are drawn at the render time each physics tick instead of lerping toward the newest snapshot. Buffers clear on spawn, despawn, new lives, map changes, dock and launch, and alien resets; late packets of the same life still fill gaps. Lasers and explosions anchor to the drawn ships. Network schema 25.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…overlay

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fold the interpolation readings into one line and clock drift into the jitter line, so the overlay still fits at 960x600. Loosen the relay test's acceleration bound for the 60 fps suite runner.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Timerk
Timerk force-pushed the feat/snapshot-interpolation branch from 7c9933a to 7f7ec31 Compare November 26, 2026 19:21
@Timerk
Timerk changed the base branch from feat/netcode-ticks to main November 26, 2026 19:21
@Timerk
Timerk merged commit 4591e9d into main Nov 26, 2026
1 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant