Skip to content

C API: Game features

Joints by name, pose overrides, clip crossfades and queues, a collision capsule, expressions, clip events and root motion. Like every avatar call they belong to the ctx's owner thread. Calls that return a sona_result give SONA_E_ARGUMENT for a NULL avatar or pointer. sona_joint_world, sona_avatar_override, _queue, _capsule, _expression and _expression_counts also give SONA_E_DESTROYED for an avatar that is being removed.

sona_capsule holds a[3], b[3] and radius: 28 bytes of floats, world metres.

sona_clip_event holds kind, mark, clip and time, 16 bytes:

Field Meaning
kind SONA_CLIP_STARTED (1), SONA_CLIP_LOOPED (2), SONA_CLIP_ENDED (3), SONA_CLIP_FOOT_LEFT (4), SONA_CLIP_FOOT_RIGHT (5), SONA_CLIP_MARK (6)
mark The id given to sona_avatar_mark, for MARK events; else 0
clip The sona_clip_get index of the clip; -1 for a prop's clip or a raw clip id
time Seconds into the clip

sona_joint_find

int32_t sona_joint_find(const char *name);
Parameter and type Meaning
name
const char *
A joint's name (required)

Returns int32_t.

Example (also in Animation layers, masks, blend spaces and state machines):

sona_mask_preset(SONA_MASK_UPPER_BODY, m);
/* half the wave in the spine ... */
sona_mask_branch(m, sona_joint_find("Spine"), 0.5f);
/* ... all of it from the chest up */
sona_mask_branch(m, sona_joint_find("Chest"), 1.0f);

A joint's number from its name: a humanoid name (Hips, Spine, Chest, Neck, Head, LeftUpperArm, ... PropLeft, PropRight), shape_NN for a body-shape joint, or joint_NN for any joint by number. -1 when the name is unknown or NULL.

sona_joint_name

const char *sona_joint_name(uint32_t joint);
Parameter and type Meaning
joint
uint32_t
A joint index, from sona_joint_find

Returns const char *.

Example:

const char * x_got = sona_joint_name(0);
printf("sona_joint_name: %s\n", x_got ? "a value" : "none");

The name of joints 0 to 70: the humanoid name, or shape_NN. NULL past the last joint. The string is static.

sona_joint_world

sona_result sona_joint_world(const sona_avatar *a, uint32_t joint,
                             float out[16]);
Parameter and type Meaning
a
const sona_avatar *
The avatar (required)
joint
uint32_t
A joint index, from sona_joint_find
out
float [16]
Receives a row-major 4x4 matrix (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_DESTROYED, SONA_E_NOT_READY (what each means).

Example (also in Cookbook: recipes for game moments):

float hand[16];
if (sona_joint_world(avatar, (uint32_t)sona_joint_find("RightHand"),
    hand) == SONA_OK) {
  float x = hand[12], y = hand[13], z = hand[14];
  /* your object's position */
  (void)x; (void)y; (void)z;
}

The joint's world matrix at the last sona_update: row-vector, metres, with the avatar's placement applied. SONA_E_ARGUMENT for a joint past 70. SONA_E_NOT_READY before the avatar is READY and posed. A sprite (see crowds) keeps the last pose computed before it became one.

sona_avatar_override

sona_result sona_avatar_override(sona_avatar *a, uint32_t joint,
                                 const float rotation[4], float weight,
                                 uint32_t mode);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)
joint
uint32_t
A joint index, from sona_joint_find
rotation
const float [4]
A unit quaternion (x, y, z, w); NULL clears the joint (required)
weight
float
A weight from 0 to 1
mode
uint32_t
SONA_OVERRIDE_REPLACE or SONA_OVERRIDE_ADD

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_DESTROYED (what each means).

Example:

float x_rotation[4];
memset(x_rotation, 0, sizeof x_rotation);
sona_result result = sona_avatar_override(avatar, 0, x_rotation, 0.5f, 0);
if (result != SONA_OK) printf("sona_avatar_override: %s\n",
    sona_result_name(result));

A rotation applied after the clip each frame, for look-at and aiming. rotation is a quaternion (x, y, z, w); it is normalised, and a zero or non-finite one is SONA_E_ARGUMENT. mode SONA_OVERRIDE_REPLACE (0) sets the joint's rotation from its rest orientation; SONA_OVERRIDE_ADD (1) rotates on top of the clip's. weight blends from the clip, 0 to 1 (above 1 counts as 1). A weight of 0 or a NULL rotation clears that joint. Joints the clip does not move are not affected while no clip plays. SONA_E_ARGUMENT for a joint past 70 or another mode.

sona_avatar_clear_overrides

void sona_avatar_clear_overrides(sona_avatar *a);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)

Returns Nothing.

Example:

sona_avatar_clear_overrides(avatar);

Clears every override of the avatar. NULL is ignored.

sona_avatar_crossfade

sona_result sona_avatar_crossfade(sona_avatar *a, const char *clip,
                                  const sona_play_options *opts,
                                  float seconds);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)
clip
const char *
A clip key or id (required)
opts
const sona_play_options *
The options, a sona_play_options (required)
seconds
float
Seconds to blend from the current pose; 0: a cut (0: a cut)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_DESTROYED (what each means).

Example (also in Getting started):

sona_placement to = { { 1.0f, 0.0f, -2.0f }, 90.0f, 1.0f };
/* over half a second */
sona_avatar_move(avatar, &to, 0.5f);
sona_avatar_crossfade(avatar, "GenericWave", NULL, 0.2f);

sona_avatar_play, blending from the current pose over seconds. 0, a negative or a non-finite value is a cut. It clears the queue. It returns what sona_avatar_play returns, so SONA_E_ARGUMENT for an item preview.

sona_avatar_queue

sona_result sona_avatar_queue(sona_avatar *a, const char *clip,
                              const sona_play_options *opts,
                              float fade_seconds);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)
clip
const char *
A clip key or id (required)
opts
const sona_play_options *
The options, a sona_play_options (NULL: the defaults)
fade_seconds
float
Seconds to blend over

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_DESTROYED, SONA_E_LIMIT (what each means).

Example (also in Cookbook: recipes for game moments):

/* once: loop 0, speed 1, from the start */
sona_play_options once = { 0, 1.0f, 0.0f };
sona_avatar_crossfade(avatar, "GenericWave", &once, 0.2f);
sona_avatar_queue(avatar, "idle", NULL, 0.3f);

Plays clip when the current one ends, blending over fade_seconds (0: a cut). A looping clip never ends, so queue after a non-looping one. NULL opts takes the defaults. Up to SONA_QUEUE_MAX clips wait; SONA_E_LIMIT past that. SONA_E_ARGUMENT for a NULL or over-long clip name, or an item preview. sona_avatar_play and sona_avatar_crossfade clear the queue.

sona_avatar_queued

uint32_t sona_avatar_queued(const sona_avatar *a);
Parameter and type Meaning
a
const sona_avatar *
The avatar (required)

Returns uint32_t.

Example:

uint32_t x_got = sona_avatar_queued(avatar);
printf("sona_avatar_queued: %lld\n", (long long)x_got);

How many clips are waiting in the queue. 0 for NULL.

sona_avatar_capsule

sona_result sona_avatar_capsule(const sona_avatar *a, sona_capsule *out);
Parameter and type Meaning
a
const sona_avatar *
The avatar (required)
out
sona_capsule *
Receives the sona_capsule (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_DESTROYED, SONA_E_NOT_READY (what each means).

Example:

sona_capsule x_capsule;
memset(&x_capsule, 0, sizeof x_capsule);
sona_result result = sona_avatar_capsule(avatar, &x_capsule);
if (result != SONA_OK) printf("sona_avatar_capsule: %s\n",
    sona_result_name(result));

A vertical capsule around the posed body, in world space, for collisions: the segment a..b and its radius, scaled with the avatar. SONA_E_NOT_READY before the avatar is READY and posed.

sona_avatar_expression

sona_result sona_avatar_expression(sona_avatar *a, const uint8_t values[5]);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)
values
const uint8_t [5]
The shape of the mouth, both eyebrows and both eyes; 255 leaves one as it is (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_DESTROYED (what each means).

Example (also in Cookbook: recipes for game moments):

/* mouth 6 is happy; brows and eyes from the clip */
uint8_t face[5] = { 6, 255, 255, 255, 255 };
sona_avatar_expression(avatar, face);

Sets the face. values are mouth, left eyebrow, right eyebrow, left eye, right eye, each a shape index (0 is the face's neutral shape) or 255 to leave that part as the clip has it. NULL hands the whole face back to the clip. The face texture is baked again when the shapes change; each combination is baked once and cached. sona_pose_data.expression reports what is shown.

Mouth shapes 0 to 6 are moods: neutral, sad, angry, confused, laughing, shocked, happy. 7 to 13 are speech shapes for lip sync: SONA_MOUTH_O (7), SONA_MOUTH_AI, SONA_MOUTH_EE, SONA_MOUTH_FV, SONA_MOUTH_W, SONA_MOUTH_L, SONA_MOUTH_DTH (13). SONA_MOUTH_NEUTRAL is 0. A sprite does not show expressions.

sona_avatar_expression_counts

sona_result sona_avatar_expression_counts(const sona_avatar *a,
                                          uint32_t counts[3]);
Parameter and type Meaning
a
const sona_avatar *
The avatar (required)
counts
uint32_t [3]
Receives how many mouth, eyebrow and eye shapes the avatar has (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_DESTROYED, SONA_E_NOT_READY (what each means).

Example:

uint32_t x_counts[3];
memset(x_counts, 0, sizeof x_counts);
sona_result result = sona_avatar_expression_counts(avatar, x_counts);
if (result != SONA_OK) printf("sona_avatar_expression_counts: %s\n",
    sona_result_name(result));

How many shapes this face has: counts[0] mouth, counts[1] eyebrows, counts[2] eyes. 0 for a part the face lacks. SONA_E_NOT_READY before READY.

sona_avatar_clip_events

uint32_t sona_avatar_clip_events(sona_avatar *a, sona_clip_event *out,
                                 uint32_t max);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)
out
sona_clip_event *
Receives the sona_clip_event (required)
max
uint32_t
How many entries out holds

Returns uint32_t.

Example:

sona_clip_event x_clip_event;
memset(&x_clip_event, 0, sizeof x_clip_event);
uint32_t x_got = sona_avatar_clip_events(avatar, &x_clip_event, 1);
printf("sona_avatar_clip_events: %lld\n", (long long)x_got);

Moves up to max events into out, oldest first, and returns how many. They are what happened on the avatar's clip since the last call. 0 for NULL arguments or max 0. Footfalls come from the evaluated pose, so an avatar that is not posed (culled, a sprite, or not READY) has none. Up to SONA_CLIP_EVENT_MAX are held; past that the oldest go.

sona_avatar_mark

sona_result sona_avatar_mark(sona_avatar *a, float seconds, uint32_t id);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)
seconds
float
The clip time the mark sits at, seconds
id
uint32_t
An id of your own, given back with the event

Returns SONA_OK. Errors SONA_E_ARGUMENT, SONA_E_LIMIT (what each means).

Example:

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

A MARK event with this id each time the current clip passes seconds. Marks go when another clip starts. SONA_E_ARGUMENT for a negative or NaN time; SONA_E_LIMIT past SONA_MARK_MAX marks.

sona_avatar_clear_marks

void sona_avatar_clear_marks(sona_avatar *a);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)

Returns Nothing.

Example:

sona_avatar_clear_marks(avatar);

Removes the avatar's marks. NULL is ignored.

sona_avatar_root_motion

sona_result sona_avatar_root_motion(sona_avatar *a, uint32_t mode);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)
mode
uint32_t
SONA_ROOT_MOTION_OFF, _EXTRACT or _APPLY

Returns SONA_OK. Errors SONA_E_ARGUMENT (what each means).

Example (also in Animation layers, masks, blend spaces and state machines):

float upper[SONA_JOINT_COUNT];
sona_layer_options lo;

/* the stock walk */
sona_avatar_play(a, "00400000-0064-0003-c1c8-f109a19cb2e0", NULL);
/* walk in place, the game moves the avatar */
sona_avatar_root_motion(a, SONA_ROOT_MOTION_EXTRACT);

sona_mask_preset(SONA_MASK_UPPER_BODY, upper);
sona_layer_defaults(&lo);
lo.mask = upper;
/* layer 1: the wave, on Spine and above */
sona_avatar_layer_play(a, 1, "GenericWave", &lo);

What happens to the clip's sideways travel of the hips. SONA_ROOT_MOTION_OFF (0) leaves it in the pose. SONA_ROOT_MOTION_EXTRACT (1) holds the hips where the clip starts and collects the travel for sona_avatar_take_root_motion. SONA_ROOT_MOTION_APPLY (2) moves the avatar's placement by it each update. Setting a mode drops what was collected. SONA_E_ARGUMENT for another mode.

sona_avatar_take_root_motion

sona_result sona_avatar_take_root_motion(sona_avatar *a, float delta[3]);
Parameter and type Meaning
a
sona_avatar *
The avatar (required)
delta
float [3]
Receives the root motion since the last call, world space (required)

Returns SONA_OK. Errors SONA_E_ARGUMENT (what each means).

Example:

float x_delta[3];
memset(x_delta, 0, sizeof x_delta);
sona_result result = sona_avatar_take_root_motion(avatar, x_delta);
if (result != SONA_OK) printf("sona_avatar_take_root_motion: %s\n",
    sona_result_name(result));

The travel collected since the last call, in world metres (the avatar's turn and scale applied), and clears it.

This page as Markdown · All the docs as one file (llms-full.txt)