# VR and XR bodies (sona_xr)

`sona_xr` puts a whole avatar body under a VR player: the headset and the two controllers go in, and the avatar's
arms reach for the controllers, the spine and hips sit under the head, the knees bend when the player crouches and
the feet step when they walk. It is an optional library beside the core (`include/sona_xr.h`, link `sona_xr`), and it
never calls OpenXR itself: your app reads the poses (`xrLocateSpace`) and feeds them in, so any runtime works. Its calls are C, as
OpenXR's are; C++ calls them as they are, and the C#, Rust and Python bindings do not wrap it (their tabs say how to
declare the calls yourself).

The VR template is a whole OpenXR app built on it: [the VR template tutorial](../tutorials/template-vr.md).

## Each frame

```c
sona_xr_input in;
sona_xr_input_defaults(&in);
in.time = now;                                  /* seconds, rising */
in.flags = SONA_XR_HAS_HEAD;                    /* and _HAS_LEFT, _HAS_RIGHT for the grips the runtime tracked */
in.head.position[0] = head_pos[0];
in.head.position[1] = head_pos[1];
in.head.position[2] = head_pos[2];
sona_xr_feed(xr, &in);
if (sona_avatar_state_of(avatar) == SONA_AVATAR_READY) sona_xr_apply(xr, avatar);
sona_update(ctx, dt, &view, NULL);
```

```cpp
#include "sona_xr.h"

sona_xr_input in;
sona_xr_input_defaults(&in);
in.time = now;                                  // seconds, rising
in.flags = SONA_XR_HAS_HEAD;                    // and _HAS_LEFT, _HAS_RIGHT for the grips the runtime tracked
in.head.position[0] = head_pos[0];
in.head.position[1] = head_pos[1];
in.head.position[2] = head_pos[2];
sona_xr_feed(xr, &in);
if (avatar.state() == SONA_AVATAR_READY) sona_xr_apply(xr, avatar.raw());
ctx.update(dt, &view);
```

```csharp
// The XR module (sona_xr) has no C# binding: its calls are C. Call them from C or C++, or declare the
// few you need with [DllImport("sona_xr")] beside the SDK's own Native class.
```

```rust
// The XR module (sona_xr) has no Rust crate: its calls are C. Declare the ones you need in an
// extern "C" block and link sona_xr beside sona360-sys.
```

```python
# The XR module (sona_xr) has no Python binding: its calls are C. Load libsona_xr with ctypes
# and declare the calls you need, as the package's own _native module does for the core.
```

Feed the head and both grip poses every frame, apply the body to the avatar, then run `sona_update` as usual. A
hand the runtime lost (its flag clear) goes back to the avatar's clip until it is tracked again. `sona_xr_apply` returns `SONA_E_NOT_READY`
until the avatar is READY and posed once.

## Spaces

Poses are in sona's world unless you say otherwise: left-handed, an avatar at turn 0 faces -Z, and a pose's
identity rotation faces -Z with +Y up. Pass `SONA_XR_OPENXR_SPACE` in `sona_xr_options.flags` to feed poses exactly as
OpenXR gives them (right-handed) and let the module turn them. `sona_xr_openxr_to_world` turns any other OpenXR pose
(a view for your camera, an object) the same way.

## The player's height

The avatar is scaled so its eyes are at the player's eyes. The eye height comes from, in order:
`sona_xr_options.user_height` when set; `sona_xr_calibrate`, called while the player stands straight; or the highest
head pose fed so far. `floor_y` in each input is the floor's world height.

## First person and mirrors

`SONA_XR_FIRST_PERSON` marks the player's own body: the body sits a little behind the eyes, so the camera is never
inside the head. Draw that copy without what would sit in front of the eyes (hats, glasses, earrings, hair), and draw
a second, whole copy for mirrors and for other players: the template makes the first-person copy with
`sona_manifest_unwear` and drives both from the same input.

`SONA_XR_NO_LEGS` leaves the legs to the avatar's clip, for seated play.

## What it did

`sona_xr_status_get` reports the last apply: the poses tracked, the eye height and scale in use, the body's turn and
placement, how far it crouches, the steps taken and where the avatar's eyes were put (to check the camera against).
`sona_xr_release` hands the avatar's joints back to its clip; `sona_xr_destroy` frees the body.
