Recursive AI

Diagnose unstable rigid bodies in C#

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

Call world.Diagnose() to find why bodies jitter or jerk apart. Here two boxes start inside each other.

Terminal
dotnet add package RecursiveAI.OnePhysics

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

C#
using RecursiveAI.OnePhysics;
using RecursiveAI.OnePhysics.Physics3D;
 
var world = new PhysicsWorld3D();
world.AddBox(new(2, 2, 2), at: new(0, 0, 0),
    type: BodyType.Static);
world.AddBox(new(2, 2, 2), at: new(0, .5, 0));
foreach (var hit in world.Diagnose().Findings)
{
    Console.WriteLine(
        $"{hit.Id} {hit.Severity}: {hit.Code}");
    Console.WriteLine(hit.Suggestion);
}

Both boxes are 2 m on each side, but their centers are only 0.5 m apart. The fixed box acts as a solid obstacle. Run the diagnosis before the first gameplay step to see the starting overlap.

If you get an empty report, run the diagnosis at the moment the problem begins, with a probe long enough to observe it; an empty report is not a guarantee of stability.

Unzip the example, then dotnet run.

Prints:

Text
OPD1009 Warning: ShapesOverlapping
Place the bodies apart when creating them (or let one start clear of the other and move it in).

Variations

To count starting overlaps without counting unrelated findings, add using RecursiveAI.OnePhysics.Diagnosis; at the top and replace the loop with:

C#
var report = world.Diagnose();
var overlaps = report.With(
    DiagnosticCode.ShapesOverlapping);
Console.WriteLine($"Overlaps: {overlaps.Count()}");
Text
Overlaps: 1

Keep the probe enabled when checking for overlaps in a fresh scene: until the first step, the world has no contacts to inspect.

The short answer

Call world.Diagnose() on the 2D or 3D scene that shows the problem. Log each finding’s Id and Severity, then use its Explanation and Suggestion to decide what to change. Diagnosis inspects the starting state and normally steps a copy of the world; it doesn’t move the live bodies. In the example, OPD1009 tells you to fix where the boxes spawn, not to change their mass or time step.

Why do my rigid bodies explode or jitter?

Start with the geometry. If two bodies spawn deeply interpenetrating, the solver has to separate them. Move the dynamic box above the fixed one instead of starting it inside. If a joint jerks on creation, inspect its two anchor positions: a large initial gap means the first steps must pull the anchors together. If a joint or contact connects a heavy dynamic body to a very light one, use the mass-ratio finding to identify the pair rather than adjusting every body in the scene.

The affected Bodies and Joints are handles. Use world.GetBody(handle) to find the body in your scene. Compare each Values entry with its recorded limit and read the unit: a gravity warning can come from supplying a number in centimeters to a world configured in meters. Filter by the stable Code rather than parsing the human-readable explanation. There’s a cap per code (MaxFindingsPerCode, 16 by default); Suppressed tells you how many didn’t fit.

Why won’t my rigid bodies go to sleep?

Keep the observation enabled for this question. The default probe runs 90 steps at 1/60 s on a copy when a dynamic body is awake. OPD1008 means a group stayed awake during the observation or its settings held it awake. It doesn’t establish that the group could never sleep with more time. Check whether it is still being driven and whether the report also shows a joint gap, penetration that isn’t shrinking, or a motor pushing against a limit. The probe doesn’t copy event handlers, the pair filter, or custom-field callbacks, so behavior that depends on them may differ from gameplay.

Use the codes below to tell a starting-state issue from a symptom you should watch over several steps. They’re stable identifiers, not steps in a checklist; a run only emits findings it detects.

CodeFinding
OPD1001Joints fight each other
OPD1002High mass ratio across a joint
OPD1003High mass ratio at a contact
OPD1004Implausible body size
OPD1005Implausible speed
OPD1006Implausible gravity
OPD1007Risk of passing through a collider
OPD1008Bodies stay awake
OPD1009Shapes start deeply overlapping
OPD1010Joint anchor far from its body
OPD1011Joint anchors start far apart
OPD1012Joint error isn’t falling
OPD1013Contact penetration isn’t falling
OPD1014Motor isn’t reaching its target speed
OPD1015Motor drives into a held joint limit
OPD1016A step on the copy threw; the probe stopped

See the diagnostic catalog for the thresholds behind each code.

All onephysics recipes