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

# Compress a 3D physics snapshot 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/compress-3d-physics-snapshot/compress-3d-physics-snapshot.zip)

A compressed full snapshot stores a physics checkpoint without needing a separate baseline to restore it.

```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));
space.Step();
var full = space.Snapshot();
var compressed = space.SnapshotCompressed();
Console.WriteLine($"Full bytes: {full.Length}");
Console.WriteLine(
    $"Compressed bytes: {compressed.Length}");
var copy = new PhysicsSpace3D();
copy.RestoreCompressed(compressed);
Console.WriteLine(
    $"Same state: {space.Hash == copy.Hash}");
```

```text
Full bytes: 704
Compressed bytes: 464
Same state: True
```

The example compresses a sphere’s state after one tick and restores it into a new space.

`RestoreCompressed` throws when the decoded snapshot would exceed 64 MiB.

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

## Variations

Append this to compare the restored full snapshot bytes, not only the hashes.

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

```text
True
```

## Related recipes

- [Encode a 3D physics snapshot delta in C#](https://recursiveai.org/onegame/cookbook/encode-3d-physics-snapshot-delta/)
- [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

Call `SnapshotCompressed()` on a `PhysicsSpace3D` and pass the returned bytes to `RestoreCompressed()` on the destination. This is a compressed full physics snapshot, not a baseline-dependent delta. The program creates its own sphere, steps once, restores into a new space, and prints `Same state: True` from the hash comparison.

## How do I compress physics snapshots in C#?

Use the physics space’s codec rather than replacing the snapshot with a list of visible positions. The physics API exposes full, delta, compressed, and region snapshots for different checkpoint needs. `SnapshotCompressed()` produces an independent full physics checkpoint. The corresponding restore method takes only the compressed bytes; no prior snapshot is an argument.

The example runs headlessly in a console project. It uses a standalone `PhysicsSpace3D`, so there is no entity registry or game window to configure. `Step()` advances the simulation before capture. To save a different checkpoint, move the capture call to the point in your simulation where you want to resume later.

## Is snapshot compression the same as delta compression?

No. `SnapshotDelta(baseline)` and `RestoreDelta(baseline, delta)` explicitly depend on a full baseline. `SnapshotCompressed()` and `RestoreCompressed(compressed)` do not. Choose the full compressed form when you need an independently restorable checkpoint. Choose a delta only when you also have a way to retain and identify its baseline.

[Snapshot compression in networked physics](https://gafferongames.com/post/snapshot_compression/) can also mean application-specific techniques such as quantizing state for rendering or encoding changed values against an acknowledged baseline. Those transport choices are not implied by this API. This recipe uses the library’s full snapshot codec and checks the restored state, rather than promising a compression ratio or changing position precision.

The [OneGame physics integration contract](https://github.com/recursive-ai-oss/onegame/blob/60d5573da204de68cafd5896dc8d41b16c72ff9d/docs/interfaces/physics.md) explicitly says these codecs are not automatic wire compression. Calling the compressed snapshot method does not change ordinary engine replication messages.

## Does this compress network traffic?

No. This API is for checkpoints and saves, not automatic network-traffic compression. Capturing a compressed snapshot does not change the engine’s replication messages.

## How do I check a compressed physics snapshot round trip?

Compare the source and destination hashes immediately after restoration. The hash includes saved physics sections, including the coordinate frame’s offset (`Origin`) and events, so matching a sphere’s position alone would be a narrower check. The variation compares the full uncompressed snapshot byte sequences as well. Both checks return true for the supplied input.

Full saves, deltas, and compressed snapshots preserve the coordinate frame’s offset (`Origin`). Restore before advancing the destination; otherwise you would be comparing different simulation checkpoints. This is a physics-space save, not a complete application save: entity state and your own gameplay data need their own persistence path.

The wrapper sets a 64 MiB decoded-size limit on compressed restoration and rejects an oversized compressed input. Compression creation is documented as an allocating tooling operation. Account for that when choosing capture points; do not assume that exposing the codec makes snapshot creation part of the normal per-tick stepping path.
