Recursive AI

Rewind a physics world without serializing a snapshot in C#

OnePhysics is a free, open-source physics library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Mark() saves a frame in memory. RollbackTo(mark) rewinds to it.

Terminal
dotnet add package RecursiveAI.OnePhysics

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OnePhysics.Physics2D;
var world = new PhysicsWorld2D();
world.AddCircle(0.5f, at: new(0, 4));
var mark = world.Mark();
var before = world.ComputeHash();
world.Step(1f / 60);
world.RollbackTo(mark);
Console.WriteLine(
    world.ComputeHash() == before);

Prints True: the restored frame has the saved frame’s hash.

Unzip the example, then dotnet run.

The full program

Save a frame, simulate four steps, then rewind and replay those steps with a late push. The second mark shows what happens to a saved frame from the discarded future. OneMath is a dependency of RecursiveAI.OnePhysics, so you don’t install it separately.

C#
using RecursiveAI.OneMath;
using RecursiveAI.OnePhysics.Physics2D;
 
const float dt = 1f / 60;
var world = new PhysicsWorld2D();
var ball = world.AddCircle(0.5f, at: new(0, 4));
world.Step(dt);
var target = world.Mark();
ulong saved = world.ComputeHash();
 
for (int i = 0; i < 4; i++) world.Step(dt);
var discarded = world.Mark();
ulong original = world.ComputeHash();
 
world.RollbackTo(target);
Console.WriteLine($"Restored saved frame: {world.ComputeHash() == saved}");
Console.WriteLine($"Discarded mark is valid: {world.IsMarkValid(discarded)}");
 
// Correct the input at the saved frame and replay the four steps.
ball.LinearVelocity += new Vector2F(2, 0);
using (world.SuppressCallbacks())
    for (int i = 0; i < 4; i++) world.Step(dt);
 
Console.WriteLine($"Corrected replay changed state: {world.ComputeHash() != original}");
world.ReleaseMark(target);

dotnet run prints:

Text
Restored saved frame: True
Discarded mark is valid: False
Corrected replay changed state: True

Rolling back to a released mark throws StaleHandleException; use IsMarkValid if you’re not sure the frame is still retained.

Variations

Keep only the latest mark. Append this to the snippet at the top:

C#
world.Step(1f / 60);
var latest = world.Mark();
world.ReleaseOldMarks(1);
Console.WriteLine(world.IsMarkValid(mark));
Console.WriteLine(world.IsMarkValid(latest));

The two added lines print False and True.

The short answer

Call Mark() before a step and keep the returned WorldMark with that step’s frame number. When you need to rewind, call RollbackTo(mark) on the same world. It restores the saved physics state without serializing it. The mark is a reference to state the world retains, not a copy of the state you can load into another world. Use a snapshot if the state needs to leave the process.

What do rollback frames mean?

A rollback frame is a simulation tick you can return to. Take the mark before applying frame 96’s inputs so it holds the state at the start of that tick. When a corrected input arrives for frame 96, roll back to that mark, apply the corrected input, and step forward using the inputs for the subsequent ticks. Keep the same step duration during replay. If you mark after the step instead, you’ve already simulated frame 96 and the mark is too late for that correction.

The full program changes the ball’s velocity before the first replayed step. In a game, you also need to restore anything outside the physics world that feeds those steps, such as your own input cursor or gameplay state. SuppressCallbacks() prevents physics callbacks during the replay; it does not suppress polled events or undo a sound you already played. See the OnePhysics rollback guide.

How long can you keep a saved physics state?

Several marks can stay live. Keep a bounded history that covers the oldest frame you’re willing to replay, because each mark holds a copy of the world’s used simulation storage. When you roll back, the target and earlier marks remain valid; all newer marks are released. If you replay the discarded frames and want to rewind to them again, make fresh marks as you go. Replaying the target frame itself doesn’t require a fresh mark because the original target is still valid.

Don’t treat the number in ReleaseOldMarks as a frame number: it’s a count of marks to retain. If you keep a frame-to-mark table, clear entries from the discarded future and replace them as you replay. If the input you need to correct is older than your retained history, you can’t recover that frame from a mark alone.

All onephysics recipes