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

# Restore part of a 3D physics world 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/repair-3d-physics-region/repair-3d-physics-region.zip)

A region snapshot repairs selected physics bodies and their connected constraints. The destination must have the same tick (simulation step), origin (coordinate-frame offset), and topology (body and constraint structure).

```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();
var body = space.AddSphere(Fixed.One,
    new FVec3(0, 4, 0));
var copy = space.Fork();
var region = space.SnapshotRegion(
    new FVec3(-5, -5, -5), new FVec3(5, 5, 5));
copy.SetVelocity(body, new FVec3(2, 0, 0));
copy.RestoreRegion(region);
space.CanonicalizeBroadphase();
copy.CanonicalizeBroadphase();
Console.WriteLine(
    $"Same state: {space.Hash == copy.Hash}");
```

```text
Same state: True
```

The example changes a fork’s sphere velocity without stepping it, then repairs that region. Both spaces discard broadphase history—the collision-candidate structure’s history—before comparing full hashes.

Invalid input leaves the space unchanged.

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

## Variations

Append this to compare the repaired body’s position and velocity directly.

```csharp
Console.WriteLine(space.Position(body)
    == copy.Position(body));
Console.WriteLine(space.Velocity(body)
    == copy.Velocity(body));
```

```text
True
True
```

## Related recipes

- [Encode a 3D physics snapshot delta in C#](https://recursiveai.org/onegame/cookbook/encode-3d-physics-snapshot-delta/)
- [Compress a 3D physics snapshot in C#](https://recursiveai.org/onegame/cookbook/compress-3d-physics-snapshot/)
- [Compare 3D physics snapshot differences in C#](https://recursiveai.org/onegame/cookbook/describe-3d-physics-state-differences/)

## The short answer

Capture a region with `SnapshotRegion(minimum, maximum)` and apply it with `RestoreRegion(region)`. This is a repair at the same tick, origin, and topology, not arbitrary region rollback. Before comparing full hashes, call `CanonicalizeBroadphase()` on both spaces between steps. The example deliberately changes velocity on a fork without advancing time or adding bodies.

## How do I restore part of a 3D physics world in C#?

The example starts with one sphere and an independent fork that preserves its handle. It captures inclusive local bounds around that sphere, changes the fork’s velocity, and restores the captured region into the fork. No input files or networking are involved. The final comparison prints `Same state: True` after canonicalization on both sides.

Bounds passed to `SnapshotRegion` are local to the space’s accumulated `Origin`. Choose them in the same coordinate frame as the space, not an unrelated camera or global coordinate system. The method captures bodies in those inclusive bounds and their constraint closure. A connected constraint can therefore make the captured set broader than the bodies you initially selected spatially.

## Can I roll back only one region of a physics simulation?

Not across arbitrary times with this API. The [integration contract](https://github.com/recursive-ai-oss/onegame/blob/60d5573da204de68cafd5896dc8d41b16c72ff9d/docs/interfaces/physics.md) requires the same tick, origin, and topology for a region restore. A stale region from before stepping or changing the body structure is not a general-purpose substitute for a full checkpoint.

Use a full snapshot when you need to restore an earlier physics checkpoint. Use a topology-compatible region snapshot when the destination already has the appropriate structure and time, but selected body state needs repair. In this program, `Fork()` establishes that common structure, and changing velocity alone leaves the intended repair conditions intact.

Region capture is not an instruction to recreate whatever bodies happen to be missing, nor does it authorize mixing an old region with a newer simulation. Your application must choose a compatible checkpoint before attempting the repair.

## How do I compare physics state after a region repair?

Check the repaired body’s position and velocity for a direct local result. The variation prints true for both. To compare full state hashes, first canonicalize the broadphase on both peers. The broadphase is the collision-candidate structure; region restoration refreshes its proxies rather than importing their history.

`CanonicalizeBroadphase()` discards that history in canonical shape order. Calling it only on the repaired space does not follow the documented comparison procedure. Call it between steps on both sides, then compare `Hash`.

The canonicalization method allocates tooling buffers, as does region capture. These are explicit diagnostic and repair operations, not an automatic networking feature. Ordinary engine table serialization still uses full physics snapshots. Region repair does not add a regional synchronization protocol, choose the repair bounds, or decide when two peers are compatible; those decisions remain in the calling application.
