---
title: "Attach simulated cloth to an animated bone in C#"
url: https://recursiveai.org/onephysics/cookbook/attach-cloth-to-animated-bone/
description: "Bind a procedural cloth banner to one animated bone with OnePhysics SoftSkin. Step the simulation and read the attached and free particle positions."
version: "1.0.0"
---

# Attach simulated cloth to an animated bone 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/attach-cloth-to-animated-bone/attach-cloth-to-animated-bone.zip)

Use `SoftSkin` to pin a banner’s edge to a moving bone and let the rest bend. No model file or renderer is needed.

```shell
dotnet new console
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.

`RecursiveAI.OneMath` comes with OnePhysics.

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

var world = new PhysicsWorld3D();
var mesh = SoftBodyMesh.Cloth(1, 1, 1, 1);
var weights = new float[16];
for (int i = 0; i < 4; i++)
    weights[4 * i] = 1;
var def = SoftBodyDef.Cloth(mesh, new(0, 2, 0));
def.Skin = SoftSkin.Create(mesh, [-1], [default],
    new int[16], weights, [0, 0, 0.2f, 0.2f]);
var banner = world.AddSoftBody(def);
banner.SetSkeletonPose([QuaternionF.Identity],
    new(1, 2, 0));
world.Step(1f / 60);
Console.WriteLine(banner.GetPosition(0).X);
```

In `SoftSkin.Create`, `[-1]` means one root bone, `[default]` puts its bind position at the mesh origin, `16` gives four bone slots per particle, and the last array sets how far each particle can move from its bone-driven target in meters (`0` locks it to the target).

Unzip [the example](https://recursiveai.org/onephysics/cookbook/attach-cloth-to-animated-bone/attach-cloth-to-animated-bone.zip), then `dotnet run`.

The snippet prints the first attached particle’s X position:

```text
0.5
```

## The full program

The small snippet uses one square with four particles. Its first two particles follow the bone exactly; the other two can move up to 0.2 meters from their bone-driven positions. The first particle starts at X = −0.5 and moves one meter to X = 0.5.

The downloadable program uses four cells per side, stands the banner upright and moves the bone half a meter to the right over 30 simulation steps. It prints the middle particle in the attached top row and the middle particle in the bottom row. Coordinates are world-space `(X, Y, Z)` in meters.

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

CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
var world = new PhysicsWorld3D();
const int cells = 4;
var mesh = SoftBodyMesh.Cloth(1, 1, cells, cells);
int row = cells + 1;
int count = mesh.VertexCount;
var joints = new int[4 * count]; // every slot refers to bone 0
var weights = new float[4 * count];
var maxDistances = new float[count];
for (int i = 0; i < count; i++)
{
    weights[4 * i] = 1;
    maxDistances[i] = i < row ? 0 : 0.25f;
}

var def = SoftBodyDef.Cloth(mesh, new(0, 2, 0));
// Turn the mesh from the XZ plane into an upright banner in the XY plane.
var upright = new QuaternionF(0.70710677f, 0, 0, 0.70710677f);
def.Rotation = upright;
def.Skin = SoftSkin.Create(mesh, [-1], [default], joints, weights, maxDistances);
var banner = world.AddSoftBody(def);
int topCenter = row / 2;
int bottomCenter = count - row + topCenter;
Print("Start");
for (int frame = 1; frame <= 30; frame++)
{
    double x = 0.5 * frame / 30;
    banner.SetSkeletonPose([QuaternionF.Identity], new(x, 2, 0), upright);
    world.Step(1f / 60);
    if (frame == 15 || frame == 30) Print($"Step {frame}");
}

void Print(string label)
{
    Console.WriteLine($"{label}: top {Format(banner.GetPosition(topCenter))}; " +
        $"bottom {Format(banner.GetPosition(bottomCenter))}");
}

static string Format(Vector3D p) => $"({p.X:F3}, {p.Y:F3}, {p.Z:F3})";
```

```text
Start: top (0.000, 2.500, 0.000); bottom (0.000, 1.500, -0.000)
Step 15: top (0.250, 2.500, 0.000); bottom (0.255, 1.498, -0.000)
Step 30: top (0.500, 2.500, 0.000); bottom (0.504, 1.498, 0.000)
```

`SoftSkin.Create` throws `SoftBodyException` for malformed skin arrays, such as supplying one weight per particle instead of four.

## Variations

### Apply a world-space bone transform and reset the cloth

Insert this after the animation loop and before `void Print` in the full program. Use `SetSkeletonTransforms` if your animation code already supplies joint positions and rotations in world space. Here `teleport: true` resets the particles to their new targets and clears their velocity; omit it for ordinary animation updates.

```csharp
var pose = new SkinJointPose(new(1, 2, 0), upright);
banner.SetSkeletonTransforms([pose], teleport: true);
Print("Reset");
```

```text
Reset: top (1.000, 2.500, 0.000); bottom (1.000, 1.500, -0.000)
```

## Related recipes

- [Simulate cloth with fixed anchors](https://recursiveai.org/onephysics/cookbook/simulate-cloth/)
- [Simulate a ragdoll in C#](https://recursiveai.org/onephysics/cookbook/simulate-ragdoll/)

## The short answer

Create a `SoftSkin` for the cloth mesh, assign it to `SoftBodyDef.Skin`, and set the bone pose before each physics step. Give the edge particles a maximum distance of zero so they stay on their bone-driven targets. Give the remaining particles a positive distance so the solver can move them. After stepping, `GetPosition` reads where a particle actually ended up, not where animation alone would put it.

## How do you bind a cloth mesh to bones?

A bind pose is where the mesh and bones start before animation. `SoftSkin.Create` takes the bones in parent-before-child order, with bind positions in mesh coordinates. A single bone is the root, so its parent is `-1`. Both examples put it at the mesh origin in the bind pose. Each particle has four bone indices and four weights. With only bone 0, every index is zero and the weight slots for each particle are `1, 0, 0, 0`. The weights say which bones move the target, not how tightly the cloth stays on it.

The first row of `SoftBodyMesh.Cloth` has one more particle than there are cells along X. In the upright banner, that row becomes the top edge. For a mesh you build yourself, choose the edge using its actual vertex order; the first few vertices aren’t necessarily an edge.

## How do you pin cloth to a moving object without pinning the whole mesh?

Give only the attached particles zero maximum distance. The [soft-body guide](https://recursiveai.org/onephysics/docs/soft-bodies/#skinning-to-a-skeleton) says “a distance of 0 makes the particle kinematic.” In this case that means the bone drives the particle, rather than forces moving it away from its target. This differs from `SetPinned`, which fixes a particle where it is. The other rows of the banner have room to respond to gravity and the motion of the attached edge. Update the bone before `world.Step`; with a regular pose update, targets travel from the previous pose to the next during the step.

## What does maximum distance mean in cloth simulation?

It’s the radius around a particle’s skinned position, in meters, not the distance from the bone itself or the stretch allowed in a cloth edge. Imagine a separate sphere centered on every animated target. The banner’s top row has zero-radius spheres; each lower particle can move within a sphere with a 0.25-meter radius. If part of the cloth can’t reach a collider because its target is too far away, the guide says that where a contact and a limit disagree, “the limit wins.” Keep the animated targets and their allowed movement consistent with the surface the cloth should touch.
