---
title: "Encode a 3D physics snapshot delta in C#"
url: https://recursiveai.org/onegame/cookbook/encode-3d-physics-snapshot-delta/
description: "Encode a 3D physics snapshot delta in C# with OneGame: runnable .NET code, real output, and a downloadable example using the published physics package."
version: "2.0.0"
---

# Encode a 3D physics snapshot delta in C#

**onegame** is a free, open-source game engine for .NET from Recursive AI. MIT or Apache-2.0, no editor required.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneGame.Physics) · [GitHub](https://github.com/recursive-ai-oss/onegame) · [Docs](https://recursiveai.org/onegame/docs/) · [Download example (.zip)](https://recursiveai.org/onegame/cookbook/encode-3d-physics-snapshot-delta/encode-3d-physics-snapshot-delta.zip)

A snapshot delta records a physics state relative to an earlier full snapshot. Keep that baseline to decode it later.

```shell
dotnet add package RecursiveAI.OneGame.Physics
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

`Fixed.One` is one in the engine’s deterministic fixed-point number type; `FVec3` is its three-dimensional vector type.

```csharp
using OneGame.Core;
using OneGame.Physics;

var space = new PhysicsSpace3D();
space.AddSphere(Fixed.One,
    new FVec3(0, 4, 0));
var baseline = space.Snapshot();
space.Step();
var delta = space.SnapshotDelta(baseline);
var copy = new PhysicsSpace3D();
copy.RestoreDelta(baseline, delta);
Console.WriteLine(
    $"Same state: {space.Hash == copy.Hash}");
```

```text
Same state: True
```

The example creates a sphere, saves its baseline, advances one tick, and restores the delta into another space.

Unzip [the example](https://recursiveai.org/onegame/cookbook/encode-3d-physics-snapshot-delta/encode-3d-physics-snapshot-delta.zip), then `dotnet run`.

## Variations

Append this to compare the complete snapshot bytes after the delta restore.

```csharp
Console.WriteLine(space.Snapshot().AsSpan()
    .SequenceEqual(copy.Snapshot()));
```

```text
True
```

## Related recipes

- [Compress a 3D physics snapshot in C#](https://recursiveai.org/onegame/cookbook/compress-3d-physics-snapshot/)
- [Restore part of a 3D physics world in C#](https://recursiveai.org/onegame/cookbook/repair-3d-physics-region/)
- [Compare 3D physics snapshot differences in C#](https://recursiveai.org/onegame/cookbook/describe-3d-physics-state-differences/)

## The short answer

Use `PhysicsSpace3D.SnapshotDelta(baseline)` to encode the current physics state against a full snapshot from `Snapshot()`. Decode it with `RestoreDelta(baseline, delta)`. Both calls take the baseline bytes explicitly: the receiver’s current state is not a substitute for that argument. The example changes the state by stepping a falling sphere and prints a hash comparison after decoding.

## How do I encode a 3D physics snapshot delta in C#?

Capture the full baseline before advancing the simulation. Retain that byte array, step the space to the state you want to save, and produce a delta against the retained baseline. The snippet at the top runs this sequence without creating an engine world or opening a window. `PhysicsSpace3D` can be used as a standalone space; `Step()` advances one fixed tick.

Treat the baseline and delta as a pair in your storage or transport design. A delta is not a standalone full snapshot. If you keep several baselines, associate each delta with the baseline you actually passed when encoding it. The API does not choose a baseline for your application. A saved checkpoint can instead use `Snapshot()` when independent decoding is more useful than baseline management.

## How do snapshot deltas work in networked physics?

A sender and receiver need access to the same baseline. [Glenn Fiedler’s explanation of delta compression](https://gafferongames.com/post/snapshot_compression/#delta-compression) describes the networking consequence: the sender must use a baseline the receiver has received. Acknowledgments and baseline retention belong to the transport design, not to this encoding call.

OneGame exposes these methods for tooling. Its [physics integration contract](https://github.com/recursive-ai-oss/onegame/blob/60d5573da204de68cafd5896dc8d41b16c72ff9d/docs/interfaces/physics.md) says the sync stack has no per-table delta hook. Calling `SnapshotDelta` does not turn on automatic wire compression. Ordinary table serialization still writes a bounded full snapshot and tick rate. This example therefore demonstrates the codec, not a complete network replication protocol.

## How do I check a restored physics snapshot in C#?

Compare `Hash` after restoring, as the program does. The space’s hash covers saved physics sections, including events and origin, rather than only the sphere’s visible position. The variation also compares the full snapshot bytes, so its result is not merely a visual check.

Keep the two spaces at the same intended checkpoint when making that comparison. Stepping only one of them changes what is being compared. Snapshot deltas preserve the accumulated origin as part of physics state; they are not just differences between local body positions.

`RestoreDelta` bounds decoded data to 64 MiB. The wrapper also checks the lengths of the supplied baseline and delta. The API describes delta creation as an allocating tooling operation, so use it deliberately for checkpoints or diagnostics rather than assuming it is an allocation-free part of every simulation tick.
