---
title: "Simulate a ragdoll in C#"
url: https://recursiveai.org/onephysics/cookbook/simulate-ragdoll/
description: "Create a passive 3D humanoid ragdoll in C#, drop it onto a floor and inspect its bodies and joints with OnePhysics and a runnable example."
version: "1.0.0"
---

# Simulate a ragdoll 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-ragdoll/simulate-ragdoll.zip)

A ragdoll falls as jointed rigid bodies, not a prerecorded animation.

```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.Physics3D;

var world = new PhysicsWorld3D();
var ragdoll = world.AddRagdoll(
    RagdollDef.Humanoid,
    new Vector3D(0, 3, 0));
for (int i = 0; i < 60; i++)
    world.Step(1f / 60f);
Console.WriteLine(
    $"Parts: {ragdoll.PartCount}");
Console.WriteLine(
    $"Powered: {ragdoll.IsPowered}");
```

Creates an unpowered humanoid and advances it for one simulated second. This snippet has no floor; the full program below adds one.

```text
Parts: 13
Powered: False
```

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

## The full program

The full program adds a floor and drops the humanoid onto it. It uses the recommended solver options, prints the root body’s height before and after the fall, and checks a child joint and the head body.

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

var options = new WorldOptions3D();
Ragdoll.ApplyRecommendedOptions(options);
var world = new PhysicsWorld3D(options);
world.AddBox(new Vector3F(20, 1, 20),
    new Vector3D(0, -0.5, 0), type: BodyType.Static);
var ragdoll = world.AddRagdoll(RagdollDef.Humanoid,
    new Vector3D(0, 3, 0));
Console.WriteLine($"Parts: {ragdoll.PartCount}; joints: {ragdoll.PartCount - 1}");
Console.WriteLine($"Powered: {ragdoll.IsPowered}");
Console.WriteLine($"Root before: {ragdoll.Root.Position.Y:F3}");
for (int i = 0; i < 300; i++) world.Step(1f / 60f);
Console.WriteLine($"Root after: {ragdoll.Root.Position.Y:F3}");
Console.WriteLine($"First child joint valid: {ragdoll.GetJoint(1).IsValid}");
Console.WriteLine($"Head body valid: {ragdoll.GetBody(2).IsValid}");
```

`dotnet run` prints:

```text
Parts: 13; joints: 12
Powered: False
Root before: 3.980
Root after: 0.171
First child joint valid: True
Head body valid: True
```

An invalid skeleton throws `InvalidRagdollException`; invalid numeric fields throw `ArgumentOutOfRangeException` before creation.

## Variations

**Choose the humanoid’s height and mass.** Append this to the snippet at the top to add a 1.2-meter, 30-kilogram humanoid beside the first one.

```csharp
var smaller = world.AddRagdoll(
    RagdollDef.CreateHumanoid(1.2f, 30f),
    new Vector3D(5, 3, 0));
world.Step(1f / 60f);
Console.WriteLine($"Parts: {smaller.PartCount}");
Console.WriteLine($"Powered: {smaller.IsPowered}");
```

The added lines print:

```text
Parts: 13
Powered: False
```

## Related recipes

- [Configure physics simulation level of detail in C#](https://recursiveai.org/onephysics/cookbook/configure-physics-simulation-lod/)
- [Move a 3D physics character in C#](https://recursiveai.org/onephysics/cookbook/move-3d-physics-character/)
- [Simulate shallow water in C#](https://recursiveai.org/onephysics/cookbook/simulate-shallow-water/)

---

## The short answer

Create a `RagdollDef.Humanoid` with `world.AddRagdoll`, place a static floor beneath it and advance the world at a fixed interval. Without a pose drive, the resulting humanoid is passive: its ordinary rigid bodies and swing-twist joints respond to gravity and contact. The preset supplies the skeleton, collision shapes, masses and joint limits; it does not supply a rendered person or animation clips.

## How do I make a ragdoll physics simulation in C#?

The example above drops the preset from a placement offset of 3 meters and runs 300 steps at 1/60 second. Placement is an offset for the whole definition, not the root body’s absolute position. That is why the printed root starts at 3.980 rather than 3.000. The root ends close to the floor because the unpowered body has fallen, not because a standing animation has completed.

The [preset definition](https://github.com/recursive-ai-oss/onephysics-csharp/blob/11dad30ed91fae25354edb55930c67cc0f71fe25/src/OnePhysics/Physics3D/RagdollDefs.cs#L254) describes a 1.8-meter, 75-kilogram person with 13 parts: pelvis, torso, head, paired upper arms and forearms, and paired thighs, shins and feet. There is one joint per non-root part. Use `GetBody` to read part transforms and `GetJoint` with a child index to inspect its connecting joint; the root has no parent joint.

For a rendered character, map those body transforms to your visual skeleton in your application. This console program deliberately needs no mesh, imported skeleton or game framework. It checks body and joint state rather than producing a video.

The example calls `Ragdoll.ApplyRecommendedOptions` before constructing the world. That raises velocity iterations to at least 4, preserving higher settings. The helper is documented for powered-ragdoll stability; it does not turn this passive ragdoll into a powered one. `IsPowered` remains false.

## Is there a free C# ragdoll physics example to download?

The zip above includes the project and complete program and restores OnePhysics from NuGet. No model files need downloading. The package is free and open source under MIT or Apache-2.0. Change height and mass with the variation’s `CreateHumanoid` call if the standard preset is the wrong size; it scales the geometry and drive gains for those inputs.

For a custom skeleton, define parents before children, with exactly one root whose parent is -1. A part has a capsule, sphere or box shape, positive mass and a bind-pose transform. Its joint position and orientation are in ragdoll space, with the joint frame’s X axis as twist and Y/Z as swing. The [ragdoll specification](https://github.com/recursive-ai-oss/onephysics-csharp/blob/11dad30ed91fae25354edb55930c67cc0f71fe25/docs/spec/world3d.md#L246) explains how those frames are converted to each body’s coordinates.

Self-collision also matters when a figure folds onto itself. The default is `NonAdjacent`: joined neighbors do not collide, while other parts can. `None` filters the entire ragdoll against itself; `All` allows joined pairs too. Those options change contact behavior, not the joint hierarchy. Start from the preset before editing joint frames or collision rules, so a falling test separates a definition mistake from an animation-mapping mistake.
