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:
| Function | Signature | Purpose |
|---|---|---|
advance_fn | fn(&mut World, f64, &[Cmd]) | Step simulation one tick |
snapshot_fn | fn(&World) -> Vec<u8> | Serialise world state |
restore_fn | fn(&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§
- Desync
Report - Emitted by
RollbackWorld::check_desyncwhen a hash mismatch is found. - Frame
- One tick’s saved state: snapshot bytes plus all inputs for that tick.
- Rollback
Buffer - Rolling ring-buffer of recent
Frames. - Rollback
World - The full rollback world — generic over the simulation world type
Worldand the command typeCmd.
Enums§
- Rollback
Error - 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_IDis used for local inputs.