Recursive AI

Settle rigid bodies before gameplay 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.Settle() before play to let bodies come to rest. Here a box drops 20 cm onto a fixed floor.

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(40, 1, 40),
    at: new(0, -.5, 0),
    type: BodyType.Static);
world.AddBox(new(1, 2, 1),
    at: new(0, 1.2, 0));
var rest = world.Settle(new()
    { ReportBodies = true });
Console.WriteLine($"Clean rest: {rest.IsClean}");

Unzip the example, then dotnet run.

Prints:

Text
Clean rest: True

The full program

The download sets a play-area boundary and stops if the world doesn’t settle cleanly. It measures how far the floor could tilt before the box would tip, then checks whether a separate test piece has room to travel 1.3 m straight down. All geometry is in the code.

C#
using RecursiveAI.OnePhysics;
using RecursiveAI.OnePhysics.Physics3D;
 
var world = new PhysicsWorld3D();
world.AddBox(new(40, 1, 40),
    at: new(0, -.5, 0),
    type: BodyType.Static);
var box = world.AddBox(new(1, 2, 1),
    at: new(0, 1.2, 0));
var rest = world.Settle(new()
{
    ReportBodies = true,
    Bounds = (new(-50, -5, -50), new(50, 50, 50))
});
Console.WriteLine($"Clean rest: {rest.IsClean}");
if (!rest.IsClean)
    throw new InvalidOperationException(rest.ToString());
 
var margin = box.GetStabilityMargin();
var degrees = margin.MinTipAngle * 180 / Math.PI;
Console.WriteLine(FormattableString.Invariant(
    $"Tip angle: {degrees:F1} degrees"));
 
var probe = world.AddBox(new(.2f, .4f, .2f),
    at: new(6, 1.5, 0));
var path = probe.CheckInsertion(new(0, -1, 0), 1.3f);
Console.WriteLine($"Seats: {path.Seats}");
Text
Clean rest: True
Tip angle: 26.6 degrees
Seats: True

If IsClean is false, check whether the run converged, then look for unsupported and out-of-bounds bodies before starting gameplay.

Variations

Add damping to take bounce out of the setup run. Replace the Settle call in the snippet at the top:

C#
var rest = world.Settle(new()
{
    ReportBodies = true,
    ExtraLinearDamping = 1f
});

The same box prints Clean rest: True.

The short answer

Build the world, call world.Settle with ReportBodies = true, and start gameplay only if the report passes your checks. IsClean means the run converged, no body was unsupported, and no body was outside the configured bounds. Capture your initial gameplay snapshot after the settle, not before it: Settle advances the live world by StepsUsed steps. CheckInsertion, in contrast, only reports on a path; it doesn’t move the test body.

How do you settle a rigid body to rest on a flat surface?

Put the floor in the scene as a static body first. Its top is at Y = 0 in this example. The dynamic box is 2 m tall and starts with its center at Y = 1.2, so its bottom starts at Y = 0.2. Gravity drops the box onto the floor and the contact lets it come to rest. Setting the initial velocity to zero wouldn’t solve the problem: a box with zero speed can still be above its support and accelerate on the next step.

Give Settle a bounded number of steps (MaxSteps) rather than stepping until a velocity first reads zero. By default, it steps at 1/60 s with sleep and callbacks suppressed. It looks for low linear and angular speeds and small changes in velocity for StableSteps consecutive steps. That last check distinguishes rest from a brief pause, such as the top of a bounce. The options work in 2D too, with PhysicsWorld2D and SettleOptions2D.

Why won’t a rigid body come to rest?

Separate a convergence failure from a scene-layout failure. If Converged is false, inspect the report’s worst speeds and unbalanced force; a box still sliding down a ramp isn’t resting. An unsupported body has no path through touching contacts or joints to a static or kinematic body. Turning off gravity can make a floating body slow without giving it support. If a body’s center of mass leaves Bounds, the body is listed but ignored for convergence. Don’t let convergence alone hide an object that fell out of the level.

What makes a rigid body tip over?

GetStabilityMargin() uses contacts from the last step to find the box’s support polygon, the area over which its weight is supported. It reports the weakest tipping angle and the horizontal force at the center of mass that would tip it. A narrow base or high center of mass leaves less room before the weight shifts beyond that area. Sliding is a separate failure mode: if friction gives way first, SlidesBeforeTipping is true.

Check placement separately. CheckInsertion tells you which body blocks the path and at what distance, or whether the part seats at the requested depth. If side clearance matters, set RequiredClearance; a path that has no head-on blocker can still be too narrow. The clearance profile only measures out to the configured ProbeDistance.

All onephysics recipes