Skip to main content

Module rollback

Module rollback 

Source
Expand description

Snapshot-based rollback and deterministic lockstep input buffer. Snapshot-based rollback and deterministic lockstep input buffer.

This module provides client-prediction / authoritative-server rollback for game and real-time physics simulations. The key idea:

  • Every tick, the simulation captures a snapshot.
  • When late input arrives (or a correction from the server), we resimulate from the confirmed tick forward.
  • Desync is detected by comparing snapshot hashes between peers.

The module is NOT coupled to any specific physics engine. The user provides the world type, a command type, and three function pointers:

FunctionSignaturePurpose
advance_fnfn(&mut World, f64, &[Cmd])Step simulation one tick
snapshot_fnfn(&World) -> Vec<u8>Serialise world state
restore_fnfn(&mut World, &[u8])Restore world from bytes

§Types

  • Frame — one tick’s saved state (snapshot + inputs).
  • RollbackBuffer — ring buffer of recent frames.
  • RollbackWorld — orchestrates stepping, rollback, and desync detection.
  • DesyncReport — emitted when a hash mismatch is detected.
  • RollbackError — error variants for resimulation failures.

§Example

use oxiphysics::rollback::RollbackWorld;

struct Sim { state: i64 }

fn advance(w: &mut Sim, _dt: f64, cmds: &[i32]) {
    w.state += cmds.iter().map(|c| *c as i64).sum::<i64>();
}
fn snapshot(w: &Sim) -> Vec<u8> { w.state.to_le_bytes().to_vec() }
fn restore(w: &mut Sim, snap: &[u8]) {
    if snap.len() == 8 {
        let arr: [u8; 8] = snap.try_into().unwrap_or([0; 8]);
        w.state = i64::from_le_bytes(arr);
    }
}

let mut rw = RollbackWorld::new(Sim { state: 0 }, 64, advance, snapshot, restore);
rw.step(1.0 / 60.0, &[5_i32]);
assert_eq!(rw.world.state, 5);

Structs§

DesyncReport
Emitted by RollbackWorld::check_desync when a hash mismatch is found.
Frame
One tick’s saved state: snapshot bytes plus all inputs for that tick.
RollbackBuffer
Rolling ring-buffer of recent Frames.
RollbackWorld
The full rollback world — generic over the simulation world type World and the command type Cmd.

Enums§

RollbackError
Error variants returned by RollbackWorld::resimulate_from.

Constants§

LOCAL_PEER_ID
Reserved peer ID for local commands submitted via RollbackWorld::step.

Type Aliases§

PeerId
Identifies a network peer. LOCAL_PEER_ID is used for local inputs.