JFLib

game/src/net/clock.rs#

Source: lib/game/src/net/clock.rs

clock -- a client's two clocks: the ticks it predicts and sends inputs for (ahead of the server by the
trip of its inputs plus a margin), and the time it shows the other entities at (behind the newest
snapshot by an interpolation delay); both run on time from outside, like FixedTick

  SteeredClock    the prediction's ticks.  The server reports how early the client's inputs arrive (the
                  smallest margin per snapshot, in ticks); the clock runs up to 10 % faster or slower to
                  hold the margin at its target (2 ticks unless set), jumps ahead when far too late and
                  holds still when far too early.  All in integers: the speed in ppm, the accumulator in
                  ns x rate x ppm
  InterpClock     the picture of the others: a render time in server ticks (a tick and a fraction, so it
                  survives the wrap of u32), interp_ticks behind the newest snapshot, steered by up to
                  10 % towards that place at every snapshot, snapped when it is far off, never past the
                  newest snapshot (it holds)

The two are separate from the tick interpolation of Pose (that blends the last two predicted ticks of
the own figure); docs/NETCODE.md, "Zeit".
  • struct SteeredClock the prediction's clock (see the module header)

impl SteeredClock

  • fn new(rate: u32, target: f64) -> SteeredClock rate ticks a second, holding the margin of the inputs at target ticks

  • fn set_tick(&mut self, tick: u32) start at tick (the next tick advance hands out)

  • fn advance(&mut self, now_ns: u64) -> u32 the clock stands at now_ns -> the ticks due (take each with next_tick)

  • fn next_tick(&mut self) -> u32 the tick to run now (call once per tick advance returned)

  • fn tick(&self) -> u32 the next tick next_tick hands out

  • fn alpha(&self) -> f64 how far the clock is into the next tick, in [0, 1) (a frame blends the last two predicted states)

  • fn on_margin(&mut self, m: i32) the server's report: the smallest margin of the client's inputs (ticks they arrived early)

  • fn speed_ppm(&self) -> u32 the speed now in ppm (1 000 000: nominal)

  • fn margin(&self) -> Option<f64> the smoothed margin (None before the first report)

  • fn jumps(&self) -> u32 how often it jumped or held

  • struct InterpClock the time the others are shown at (see the module header)

impl InterpClock

  • fn new(rate: u32, delay: f64) -> InterpClock rate ticks a second, delay ticks behind the newest snapshot
  • fn on_snapshot(&mut self, tick: u32) a snapshot of tick came (older ones than the newest only count as nothing)
  • fn advance(&mut self, now_ns: u64) the clock stands at now_ns
  • fn time(&self) -> Option<(u32, f64)> the render time: a tick and the fraction towards the next (None before the first snapshot)
  • fn behind(&self) -> f64 how far the render time is behind the newest snapshot, in ticks

Source: docs/API.md in the JFLib repository.