# C API: Inverse kinematics

Two-bone IK for the arms and legs, feet planted on the host's ground, look-at for the head and the eyes, and hand
targets. It runs in every pose after the clip, the fades, the overrides and any layers, before skinning. Each struct
starts with `size = sizeof(struct)` and is refused (`SONA_E_ARGUMENT`) when `size` is smaller; the `_defaults`
functions set it. Avatar calls belong to the owner thread and give `SONA_E_ARGUMENT` for a NULL avatar and
`SONA_E_DESTROYED` for one being removed. Every setter re-poses the avatar at the next `sona_update`. The guide,
with the limits, the ground modes and the cost, is [Inverse kinematics](../../guides/ik.md).

| Struct | Bytes (8-byte pointers) | Fields |
|---|---|---|
| `sona_ik_target` | 92 | `size`, `flags`, `position[3]`, `rotation[4]`, `pole[3]`, `joint`, `weight`, `fade_seconds`, `reserved[8]` |
| `sona_ik_feet` | 88 (80 with 4-byte pointers) | `size`, `ground`, `plane[4]`, `query`, `user`, `weight`, `max_hips_offset`, `max_tilt_deg`, `smoothing`, `reserved[8]` |
| `sona_ik_look` | 80 | `size`, `flags`, `target[3]`, `weight`, `max_yaw_deg`, `max_up_deg`, `max_down_deg`, `smoothing`, `max_speed_deg`, `eye_threshold_deg`, `reserved[8]` |
| `sona_ik_limits` | 72 | `size`, `flags`, `elbow_max_deg`, `knee_max_deg`, `wrist_swing_deg`, `wrist_twist_deg`, `ankle_swing_deg`, `ankle_twist_deg`, `soft_reach`, `body_margin`, `hip_forward_deg`, `hip_back_deg`, `hip_out_deg`, `hip_in_deg`, `shoulder_back_deg`, `shoulder_across_deg`, `reserved[2]` |
| `sona_ik_status` | 88 | `size`, `active`, `error[4]`, `hit[4]`, `hips_offset`, `look_yaw_deg`, `look_pitch_deg`, `eyes[2]`, `pad[2]`, `reserved[8]` |

Limbs: `SONA_IK_LEFT_HAND` (0), `SONA_IK_RIGHT_HAND` (1), `SONA_IK_LEFT_FOOT` (2), `SONA_IK_RIGHT_FOOT` (3).

## sona_ik_target_defaults

```c
void sona_ik_target_defaults(sona_ik_target *t);
```

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

**Returns** Nothing.

**Example**:

```c
sona_ik_target x_ik_target;
sona_ik_target_defaults(&x_ik_target);
sona_ik_target_defaults(&x_ik_target);
```

`size` set, no flags, position 0, rotation (0, 0, 0, 1), joint -1, weight 1, and the fade a default of 0.2 s. NULL is ignored.

## sona_avatar_ik_target

```c
sona_result sona_avatar_ik_target(sona_avatar *a, uint32_t limb, const sona_ik_target *t);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `limb` | `uint32_t` | A `SONA_IK_*` limb | - |
| `t` | `const sona_ik_target *` | A [`sona_ik_target`](../fields.md#sona_ik_target) | required |

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

**Example**:

```c
sona_ik_target x_ik_target;
sona_ik_target_defaults(&x_ik_target);
sona_result result = sona_avatar_ik_target(avatar, 0, &x_ik_target);
if (result != SONA_OK) printf("sona_avatar_ik_target: %s\n", sona_result_name(result));
```

A hand puts its palm (with `SONA_IK_WRIST` its wrist) at `position`; a foot its ankle. Flags: `SONA_IK_ROTATION`
(`rotation` is the hand's or foot's world orientation from the rest pose), `SONA_IK_POLE` (`pole` is a point the
elbow or knee bends towards), `SONA_IK_RELATIVE` (position, rotation and pole are in the frame of `joint` of this
avatar), `SONA_IK_AVATAR_SPACE` (in avatar space, before the placement). `weight` 0..1 eases over `fade_seconds`.
NULL `t`: off at once. A foot target replaces planting for that foot. `SONA_E_ARGUMENT` for a limb past 3, a small
`size`, an unknown flag, a non-finite position or pole, a weight outside 0..1, a negative fade, a zero quaternion,
`SONA_IK_RELATIVE` without a joint (or with `SONA_IK_AVATAR_SPACE`), or `SONA_IK_WRIST` on a foot.

## sona_ik_feet_defaults

```c
void sona_ik_feet_defaults(sona_ik_feet *o);
```

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

**Returns** Nothing.

**Example**:

```c
sona_ik_feet x_ik_feet;
sona_ik_feet_defaults(&x_ik_feet);
sona_ik_feet_defaults(&x_ik_feet);
```

`SONA_IK_GROUND_PLANE` with the plane y = 0, weight 1, hips offset at most 0.45 m, tilt at most 35 degrees,
and the smoothing a default of 0.1 s.

## sona_avatar_ik_feet

```c
sona_result sona_avatar_ik_feet(sona_avatar *a, const sona_ik_feet *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `o` | `const sona_ik_feet *` | A [`sona_ik_feet`](../fields.md#sona_ik_feet) | required |

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

**Example**:

```c
sona_ik_feet x_ik_feet;
sona_ik_feet_defaults(&x_ik_feet);
sona_result result = sona_avatar_ik_feet(avatar, &x_ik_feet);
if (result != SONA_OK) printf("sona_avatar_ik_feet: %s\n", sona_result_name(result));
```

Plants the feet on the ground given by `ground`: `SONA_IK_GROUND_PLANE` (`plane`: n.x, n.y, n.z, d, world),
`SONA_IK_GROUND_POINTS` (`sona_avatar_ik_ground` per foot) or `SONA_IK_GROUND_CALLBACK` (`query`, a downward ray
the core asks for each foot's heel and toes; it runs on the posing threads, at the same time for different
avatars, must be safe from any thread and must not call into sona). The hips move by up to `max_hips_offset`.
NULL: off at once. `SONA_E_ARGUMENT` for a small `size`, another mode, a plane with no up component, a callback
mode without `query`, or a weight, offset, tilt or smoothing out of range.

## sona_avatar_ik_ground

```c
sona_result sona_avatar_ik_ground(sona_avatar *a, uint32_t foot, const float point[3], const float normal[3]);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `foot` | `uint32_t` | `SONA_IK_LEFT_FOOT` or `SONA_IK_RIGHT_FOOT` | - |
| `point` | `const float [3]` | A point on the ground under the foot, world space | required |
| `normal` | `const float [3]` | The ground's normal under the foot | required |

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

**Example**:

```c
float x_point[3];
memset(x_point, 0, sizeof x_point);
float x_normal2[3];
memset(x_normal2, 0, sizeof x_normal2);
sona_result result = sona_avatar_ik_ground(avatar, 0, x_point, x_normal2);
if (result != SONA_OK) printf("sona_avatar_ik_ground: %s\n", sona_result_name(result));
```

`SONA_IK_GROUND_POINTS`: the world point and normal of the ground under `SONA_IK_LEFT_FOOT` or
`SONA_IK_RIGHT_FOOT`, used from the next pose until changed. NULL `point`: no ground under that foot.
`SONA_E_ARGUMENT` for another limb, a NULL or zero normal, or a non-finite value.

## sona_ik_look_defaults

```c
void sona_ik_look_defaults(sona_ik_look *o);
```

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

**Returns** Nothing.

**Example** (also in [Cookbook: recipes for game moments](../../cookbook.md)):

```c
sona_ik_look look;
sona_ik_look_defaults(&look);
look.target[0] = player_head[0];
look.target[1] = player_head[1];
look.target[2] = player_head[2];
sona_avatar_ik_look(avatar, &look);
```

Its defaults are head and eyes, weight 1, yaw 70, up 35, down 45 degrees, smoothing by default 0.15 s, a turn rate of 360 degrees a second, eyes past 10.

## sona_avatar_ik_look

```c
sona_result sona_avatar_ik_look(sona_avatar *a, const sona_ik_look *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `o` | `const sona_ik_look *` | A [`sona_ik_look`](../fields.md#sona_ik_look) | required |

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

**Example** (also in [Cookbook: recipes for game moments](../../cookbook.md)):

```c
sona_ik_look look;
sona_ik_look_defaults(&look);
look.target[0] = player_head[0];
look.target[1] = player_head[1];
look.target[2] = player_head[2];
sona_avatar_ik_look(avatar, &look);
```

Turns the chest, neck and head towards the world point `target` inside the limits, eased; the eyes take the rest
through the face's look shapes when it has them. `SONA_IK_LOOK_HEAD` and `SONA_IK_LOOK_EYES` choose which. NULL:
off, easing back. `SONA_E_ARGUMENT` for a small `size`, an unknown flag, a non-finite target, or a limit out of
range (yaw 0..135, up and down 0..89, eyes 0..90, speed above 0).

## sona_ik_limits_defaults

```c
void sona_ik_limits_defaults(sona_ik_limits *o);
```

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

**Returns** Nothing.

**Example**:

```c
sona_ik_limits x_ik_limits;
sona_ik_limits_defaults(&x_ik_limits);
sona_ik_limits_defaults(&x_ik_limits);
```

Elbows 145, knees 150, wrists 75 swing and 80 twist, ankles 45 and 30 degrees, hips 120 forward, 25 behind, 45
out and 25 in, shoulders 45 behind (held level) and 50 across, reach easing from 0.97, a 0.02 m body margin, the body checks
on.

## sona_avatar_ik_limits

```c
sona_result sona_avatar_ik_limits(sona_avatar *a, const sona_ik_limits *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `o` | `const sona_ik_limits *` | A [`sona_ik_limits`](../fields.md#sona_ik_limits) | required |

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

**Example**:

```c
sona_ik_limits x_ik_limits;
sona_ik_limits_defaults(&x_ik_limits);
sona_result result = sona_avatar_ik_limits(avatar, &x_ik_limits);
if (result != SONA_OK) printf("sona_avatar_ik_limits: %s\n", sona_result_name(result));
```

The joint limits for this avatar's solves. `SONA_IK_NO_BODY` turns off the checks that keep the limbs clear of the
torso, head and legs. Every limit is hard: a target past one is reached short, and a clip already past one is
kept, never taken further. NULL: the defaults. `SONA_E_ARGUMENT` for a small `size`, an unknown flag or a value out
of range (hips forward 0..150, behind 0..90 and the two together at most 180; out, in and the shoulders 0..90).

## sona_avatar_ik_clear

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

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

**Returns** Nothing.

**Example**:

```c
sona_avatar_ik_clear(avatar);
```

Every target, the planting and the look-at off at once. The next pose is the clip's, bit for bit. NULL is ignored.

## sona_avatar_ik_status

```c
sona_result sona_avatar_ik_status(const sona_avatar *a, sona_ik_status *out);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `const sona_avatar *` | The avatar | required |
| `out` | `sona_ik_status *` | Receives the [`sona_ik_status`](../fields.md#sona_ik_status) | 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_ik_status x_ik_status;
memset(&x_ik_status, 0, sizeof x_ik_status);
x_ik_status.size = sizeof x_ik_status;
sona_result result = sona_avatar_ik_status(avatar, &x_ik_status);
if (result != SONA_OK) printf("sona_avatar_ik_status: %s\n", sona_result_name(result));
```

What the last pose did: `active` (`1 << limb`, `SONA_IK_ACTIVE_FEET`, `SONA_IK_ACTIVE_LOOK`), each limb's
distance from its target in metres, what held it back (`SONA_IK_HIT_REACH`, `_BEND`, `_POLE`, `_BODY`,
`_ROTATION`, `_GROUND`, `_SWING`), the hips' move, the head's turn from the clip and the eye shapes shown (255: the clip's).
Set `out->size` first. `SONA_E_NOT_READY` before the avatar was posed with IK on; `SONA_E_ARGUMENT` for a small
`size`.

## sona_ik_foot_lock_defaults

```c
void sona_ik_foot_lock_defaults(sona_ik_foot_lock *o);
```

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

**Returns** Nothing.

**Example**:

```c
sona_ik_foot_lock x_ik_foot_lock;
sona_ik_foot_lock_defaults(&x_ik_foot_lock);
sona_ik_foot_lock_defaults(&x_ik_foot_lock);
```

`size` set; `weight` 1, `max_slide` 0.25, `release_seconds` 0.15.

## sona_avatar_ik_foot_lock

```c
sona_result sona_avatar_ik_foot_lock(sona_avatar *a, const sona_ik_foot_lock *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `o` | `const sona_ik_foot_lock *` | A [`sona_ik_foot_lock`](../fields.md#sona_ik_foot_lock) | required |

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

**Example**:

```c
sona_ik_foot_lock x_ik_foot_lock;
sona_ik_foot_lock_defaults(&x_ik_foot_lock);
sona_result result = sona_avatar_ik_foot_lock(avatar, &x_ik_foot_lock);
if (result != SONA_OK) printf("sona_avatar_ik_foot_lock: %s\n", sona_result_name(result));
```

Foot locking: a foot the clip plants stays where it landed until the clip lifts the heel, so walk and run clips do not slide while the host moves the avatar no faster than the clip's feet travel. Pulled past `max_slide` it lets go. A foot target replaces it for that foot. NULL: off.

## sona_ik_spine_defaults

```c
void sona_ik_spine_defaults(sona_ik_spine *o);
```

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

**Returns** Nothing.

**Example**:

```c
sona_ik_spine x_ik_spine;
sona_ik_spine_defaults(&x_ik_spine);
sona_ik_spine_defaults(&x_ik_spine);
```

`size` set; `weight` 1, `max_bend_deg` 35, `max_twist_deg` 40, `hips_share` 0.2.

## sona_avatar_ik_spine

```c
sona_result sona_avatar_ik_spine(sona_avatar *a, const sona_ik_spine *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `o` | `const sona_ik_spine *` | A [`sona_ik_spine`](../fields.md#sona_ik_spine) | required |

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

**Example**:

```c
sona_ik_spine x_ik_spine;
sona_ik_spine_defaults(&x_ik_spine);
sona_result result = sona_avatar_ik_spine(avatar, &x_ik_spine);
if (result != SONA_OK) printf("sona_avatar_ik_spine: %s\n", sona_result_name(result));
```

Spine assist: a hand target past the arm's reach bends and turns the spine, and the hips a little, towards it. NULL: off.

## sona_ik_fingers_defaults

```c
void sona_ik_fingers_defaults(sona_ik_fingers *o);
```

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

**Returns** Nothing.

**Example**:

```c
sona_ik_fingers x_ik_fingers;
sona_ik_fingers_defaults(&x_ik_fingers);
sona_ik_fingers_defaults(&x_ik_fingers);
```

`size` set; `spread` 0, `weight` 1, `fade_seconds` 0.15.

## sona_avatar_ik_fingers

```c
sona_result sona_avatar_ik_fingers(sona_avatar *a, uint32_t hand, const sona_ik_fingers *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `hand` | `uint32_t` | `SONA_IK_LEFT_HAND` or `SONA_IK_RIGHT_HAND` | - |
| `o` | `const sona_ik_fingers *` | A [`sona_ik_fingers`](../fields.md#sona_ik_fingers) | required |

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

**Example**:

```c
sona_ik_fingers x_ik_fingers;
sona_ik_fingers_defaults(&x_ik_fingers);
sona_result result = sona_avatar_ik_fingers(avatar, 0, &x_ik_fingers);
if (result != SONA_OK) printf("sona_avatar_ik_fingers: %s\n", sona_result_name(result));
```

`hand` `SONA_IK_LEFT_HAND` or `SONA_IK_RIGHT_HAND`. NULL: back to the clip over fade_seconds.

## sona_ik_eyes_defaults

```c
void sona_ik_eyes_defaults(sona_ik_eyes *o);
```

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

**Returns** Nothing.

**Example**:

```c
sona_ik_eyes x_ik_eyes;
sona_ik_eyes_defaults(&x_ik_eyes);
sona_ik_eyes_defaults(&x_ik_eyes);
```

`size` set; `max_speed_deg` 400, `smoothing` 0.05, `lead_seconds` 0.08.

## sona_avatar_ik_eyes

```c
sona_result sona_avatar_ik_eyes(sona_avatar *a, const sona_ik_eyes *o);
```

| Parameter | Type | Meaning | Default |
|---|---|---|---|
| `a` | `sona_avatar *` | The avatar | required |
| `o` | `const sona_ik_eyes *` | A [`sona_ik_eyes`](../fields.md#sona_ik_eyes) | required |

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

**Example**:

```c
sona_ik_eyes x_ik_eyes;
sona_ik_eyes_defaults(&x_ik_eyes);
sona_result result = sona_avatar_ik_eyes(avatar, &x_ik_eyes);
if (result != SONA_OK) printf("sona_avatar_ik_eyes: %s\n", sona_result_name(result));
```

Smooth eye turns: the eyes move towards the look-at target at up to `max_speed_deg` a second and lead the head by `lead_seconds`. NULL: back to the plain look-at.
