---
title: "Simulate a volumetric soft body in C#"
url: https://recursiveai.org/onephysics/cookbook/simulate-volumetric-soft-body/
description: "Drop a jelly box onto a rigid floor in C#, step its volume constraints, and read particle positions with OnePhysics."
version: "1.0.0"
---

# Simulate a volumetric soft body in C#

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

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePhysics) · [GitHub](https://github.com/recursive-ai-oss/onephysics-csharp) · [Docs](https://recursiveai.org/onephysics/docs/) · [Download example (.zip)](https://recursiveai.org/onephysics/cookbook/simulate-volumetric-soft-body/simulate-volumetric-soft-body.zip)

A volumetric soft body deforms while its constraints resist changes in volume. This example drops a jelly box onto a rigid floor.

```shell
dotnet add package RecursiveAI.OnePhysics
```

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

```csharp
using RecursiveAI.OneMath;
using RecursiveAI.OnePhysics;
using RecursiveAI.OnePhysics.Physics3D;
var world = new PhysicsWorld3D();
world.AddBox(new(10, 1, 10),
    new(0, -0.5, 0), type: BodyType.Static);
var jelly = world.AddSoftBox(new Vector3F(1, 1, 1),
    at: new Vector3D(0, 2, 0), SoftMaterial.Jelly);
for (int i = 0; i < 180; i++)
    world.Step(1f / 60);
Console.WriteLine($"Volume: {jelly.Volume:F3}");
```

Prints:

```text
Volume: 0.963
```

Unzip [the example](https://recursiveai.org/onephysics/cookbook/simulate-volumetric-soft-body/simulate-volumetric-soft-body.zip), then `dotnet run`.

## The full program

The full program creates the floor and a closed jelly box, advances the simulation, then reads one particle and the volume. Sizes are full extents, not half extents.

```csharp
using RecursiveAI.OneMath;
using RecursiveAI.OnePhysics;
using RecursiveAI.OnePhysics.Physics3D;

var world = new PhysicsWorld3D();
world.AddBox(new Vector3F(10, 1, 10),
    new Vector3D(0, -0.5, 0), type: BodyType.Static);
var jelly = world.AddSoftBox(new Vector3F(1, 1, 1),
    at: new Vector3D(0, 2, 0), SoftMaterial.Jelly);
for (int i = 0; i < 180; i++)
    world.Step(1f / 60);
Console.WriteLine($"Particle Y: {jelly.GetPosition(0).Y:F3}");
Console.WriteLine($"Rest volume: {jelly.RestVolume:F3}");
Console.WriteLine($"Volume: {jelly.Volume:F3}");
```

```text
Particle Y: 0.078
Rest volume: 1.000
Volume: 0.963
```

Invalid mesh data throws `SoftBodyException` with an error code naming the field or element.

## Variations

**Make the volume constraint as stiff as possible.** Append this to the full program; zero compliance means the constraint does not intentionally yield.

```csharp
jelly.VolumeCompliance = 0;
world.Step(1f / 60);
Console.WriteLine($"Compliance: {jelly.VolumeCompliance}");
```

```text
Compliance: 0
```

## Related recipes

- [Simulate cloth in C#](https://recursiveai.org/onephysics/cookbook/simulate-cloth/)

## The short answer

Use `PhysicsWorld3D.AddSoftBox` with `SoftMaterial.Jelly`, add a static floor, and call `Step` at a fixed interval. Read the deformed geometry with `GetPosition` or `CopyPositions`. The box is a volume-bearing soft body, not an open cloth sheet. The program above reports both its original volume and its volume after landing, so you can see the deformation without a renderer.

## What is soft body physics?

A soft body is a collection of particles joined by constraints rather than a single rigid shape. In OnePhysics, edge constraints resist stretching, bend constraints resist bending, and volume constraints resist compression. Its 3D solver uses extended position-based dynamics (XPBD). Each particle also acts as a small sphere for collision, which is why the lowest particle center sits above the floor rather than exactly on it.

The [soft-body guide](https://recursiveai.org/onephysics/docs/soft-bodies/) provides cloth, rubber and jelly presets. Rubber springs back stiffly; jelly is wobbly. An open sheet is appropriate for fabric, but a rubber block needs a closed surface or tetrahedra to describe a volume. `SoftBodyMesh.FromTriangles` and `FromTetrahedra` accept custom geometry; generated boxes and spheres avoid the need to author a mesh for a first experiment.

## How do you simulate a soft body in C#?

Create the world and rigid collision geometry before stepping it. `AddSoftBox` supplies a generated mesh and material settings together. The example uses a fixed step and reads the particle state only after all steps finish. There is no display or rendering dependency: your application can use those positions to update its own visible mesh.

Positions use double-precision vectors; sizes and most settings use floats. The handle’s `GetPosition(i)` reads one particle without allocating an array. For a visible mesh, copy the particle positions into a span rather than allocating a new collection on every frame. `RestVolume` is the original volume; `Volume` is the current value. They need not be equal after contact, because the solver uses a finite number of passes and the jelly preset allows deformation.

A soft body is stored separately from rigid bodies. Use its own particle accessors, not a rigid body’s position, to draw its deformed shape. Destroying it invalidates its handle; later access throws `StaleHandleException`.

## How do you make a soft body less squishy?

Lower compliance makes a constraint stiffer; zero is the stiffest setting. The variation changes only volume compliance, so edges and bending retain their preset behavior. If the problem is excessive stretching rather than compression, inspect the edge settings instead of treating volume compliance as a universal stiffness control.

`WorldOptions3D.SoftSubsteps` and `SoftIterations` control how often the soft constraints are solved. The defaults are four substeps and three iterations. Increase them when the chosen mass and stiffness need more solver work, but test the actual contact scene. The guide warns that heavy rigid bodies sink into light soft bodies: it specifically contrasts a 5 kg box on an 8 kg cushion with a 300 kg body on a 5 kg block. A mass imbalance is not repaired merely by choosing the rubber preset.
