# C API: Ragdolls

Bodies fitted to the avatar for the host's physics engine, and the handoff to a ragdoll and back: [Physics-ready avatars](../../guides/physics.md).

## sona_ragdoll_defaults

```c
void sona_ragdoll_defaults(sona_ragdoll_options *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `o` | `sona_ragdoll_options *` | The [`sona_ragdoll_options`](../fields.md#sona_ragdoll_options) to fill with the defaults | required |

**Returns** Nothing.

**Example**:

```c
sona_ragdoll_options x_ragdoll_options;
memset(&x_ragdoll_options, 0, sizeof x_ragdoll_options);
x_ragdoll_options.size = sizeof x_ragdoll_options;
sona_ragdoll_defaults(&x_ragdoll_options);
```

`size` set; `coverage` 0.9.

## sona_avatar_ragdoll

```c
sona_result sona_avatar_ragdoll(sona_avatar *a, const sona_ragdoll_options *o, sona_ragdoll_body *out, uint32_t cap, uint32_t *count);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `o` | `const sona_ragdoll_options *` | The options, a [`sona_ragdoll_options`](../fields.md#sona_ragdoll_options) | required |
| `out` | `sona_ragdoll_body *` | Receives the [`sona_ragdoll_body`](../fields.md#sona_ragdoll_body) | required |
| `cap` | `uint32_t` | How many entries `out` holds | - |
| `count` | `uint32_t *` | Receives how many entries there are; may be NULL | `NULL`: not written |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

Fits the bodies to the avatar as it is now (its rig, body shape and outfit) and writes up to cap of them; count (may be NULL) gets `SONA_RAGDOLL_BODIES`. The same avatar and options give the same bits. From this call on, each pose keeps the bodies' last two transforms for `sona_avatar_ragdoll_state`. `SONA_E_NOT_READY` before the avatar is READY and posed; `SONA_E_ARGUMENT` for a bad size, flag, mass or coverage.

## sona_avatar_ragdoll_state

```c
sona_result sona_avatar_ragdoll_state(const sona_avatar *a, sona_ragdoll_state *out, uint32_t cap, uint32_t *count);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `out` | `sona_ragdoll_state *` | Receives the [`sona_ragdoll_state`](../fields.md#sona_ragdoll_state) | required |
| `cap` | `uint32_t` | How many entries `out` holds | - |
| `count` | `uint32_t *` | Receives how many entries there are; may be NULL | `NULL`: not written |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY` ([what each means](../../troubleshooting.md#error-codes)).

The bodies at the last pose, with velocities from the last two poses (on the scene clock; zero until there have been two). For the host's bodies when it takes over. `SONA_E_NOT_READY` before `sona_avatar_ragdoll`.

## sona_avatar_ragdoll_drive

```c
sona_result sona_avatar_ragdoll_drive(sona_avatar *a, const sona_ragdoll_state *bodies, uint32_t count, float weight, uint32_t flags);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `bodies` | `const sona_ragdoll_state *` | A [`sona_ragdoll_state`](../fields.md#sona_ragdoll_state) | required |
| `count` | `uint32_t` | How many entries the array holds | - |
| `weight` | `float` | How much of the host's bodies to show over the clip, 0 to 1 | - |
| `flags` | `uint32_t` | Option bits for the call; 0: none | `0`: none |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_MEMORY` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_ragdoll_state x_ragdoll_state;
memset(&x_ragdoll_state, 0, sizeof x_ragdoll_state);
sona_result result = sona_avatar_ragdoll_drive(avatar, &x_ragdoll_state, 1, 0.5f, 0);
if (result != SONA_OK) printf("sona_avatar_ragdoll_drive: %s\n", sona_result_name(result));
```

The host's simulated bodies onto the avatar: from the next pose it draws them, until released. `count` is `SONA_RAGDOLL_BODIES`; velocities are not read. `weight` 0..1 blends from the clip's pose (0.3: a hit reaction); every joint between bodies (fingers, toes, the neck, the face) keeps its clip pose relative to its body. Call it after each physics step. `SONA_RAGDOLL_KEEP_LENGTHS`: only the bodies' turns and the hips' position are taken, so the bones keep their lengths. `SONA_E_ARGUMENT` for a bad count, weight or flag, or a body that is not finite.

## sona_avatar_ragdoll_release

```c
sona_result sona_avatar_ragdoll_release(sona_avatar *a, float blend_seconds);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `blend_seconds` | `float` | Seconds over which the ragdoll blends back to the clip | - |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_result result = sona_avatar_ragdoll_release(avatar, 0.5f);
if (result != SONA_OK) printf("sona_avatar_ragdoll_release: %s\n", sona_result_name(result));
```

Back to the animation: the last drawn ragdoll pose stays where it is in the world and blends into the clip's pose over blend_seconds (smoothstep; 0: at once), turn by turn down the body, so limbs stay joined. Play the get-up clip and move the placement to `sona_ragdoll_info`'s getup_position and getup_turn_deg first.

## sona_avatar_ragdoll_info

```c
sona_result sona_avatar_ragdoll_info(const sona_avatar *a, sona_ragdoll_info *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `out` | `sona_ragdoll_info *` | Receives the [`sona_ragdoll_info`](../fields.md#sona_ragdoll_info) | required |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY` ([what each means](../../troubleshooting.md#error-codes)).

**Example**:

```c
sona_ragdoll_info x_ragdoll_info;
memset(&x_ragdoll_info, 0, sizeof x_ragdoll_info);
x_ragdoll_info.size = sizeof x_ragdoll_info;
sona_result result = sona_avatar_ragdoll_info(avatar, &x_ragdoll_info);
if (result != SONA_OK) printf("sona_avatar_ragdoll_info: %s\n", sona_result_name(result));
```

`SONA_E_NOT_READY` before the avatar has been posed since `sona_avatar_ragdoll`.

## sona_avatar_ragdoll_clear

```c
void sona_avatar_ragdoll_clear(sona_avatar *a);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |

**Returns** Nothing.

**Example**:

```c
sona_avatar_ragdoll_clear(avatar);
```

Off at once: no tracking, no drive, no blend.

## sona_avatar_ragdoll_motors

```c
sona_result sona_avatar_ragdoll_motors(const sona_avatar *a, sona_ragdoll_motor *out, uint32_t cap, uint32_t *count);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `out` | `sona_ragdoll_motor *` | Receives the [`sona_ragdoll_motor`](../fields.md#sona_ragdoll_motor) | required |
| `cap` | `uint32_t` | How many entries `out` holds | - |
| `count` | `uint32_t *` | Receives how many entries there are; may be NULL | `NULL`: not written |

**Returns** `SONA_OK`. **Errors** `SONA_E_ARGUMENT`, `SONA_E_DESTROYED`, `SONA_E_NOT_READY` ([what each means](../../troubleshooting.md#error-codes)).

The targets of the last pose, velocities from the last two (zero until there have been two). count (may be NULL) gets `SONA_RAGDOLL_BODIES`. `SONA_E_NOT_READY` before `sona_avatar_ragdoll`. The stock get-up clips are "getup_back" and "getup_front" ([physics guide](../../guides/physics.md), "Getting up").
