set_pinned: pinned particles are dormant, pinned neighbours of live particles kinematic, wake writes from the anchor (ABI 18)

ymir_particles_set_pinned and ymir_box3d_soa_set_pinned (proxies gathered first); the view gains particle_pinned; remove(attachment) refuses a pinned particle's anchor. Managed SetPinned and ParticlePinned; ExpectedAbi 18; docs/soa-world.md.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-26 02:47:05 +01:00
commit 41f9179fb0
6 changed files with 115 additions and 9 deletions

View file

@ -8,12 +8,12 @@ Box3D decides every physical outcome; Ymir creates bodies, steps the world and e
`ymir_box3d_session_*` (`ymir_box3d.c`) is the planar Aetheria session: gravity is zero, Y is locked,
only yaw rotates (`motionLocks.linearY/angularX/angularZ`), and state leaves by `copy_bodies` into an
array of structs. A body cannot fall or tumble there, and every read is a copy. The SoA world is a
separate native object (`ymir_box3d_soa.c`, ABI 17) so the Aetheria session's contract is untouched.
separate native object (`ymir_box3d_soa.c`, ABI 18) so the Aetheria session's contract is untouched.
## Contract
- Native (ABI 17): `ymir_box3d_soa_create(capacity, joint_capacity, gravity, fact_capacity, hit_threshold, particle_capacities*) /
create_particles / destroy_particles / create_distances / create_bends / create_sensors / attach(n, desc*) / remove / set_material / free_slots /
- Native (ABI 18): `ymir_box3d_soa_create(capacity, joint_capacity, gravity, fact_capacity, hit_threshold, particle_capacities*) /
create_particles / destroy_particles / create_distances / create_bends / create_sensors / attach(n, desc*) / set_pinned(n, handles, pinned) / remove / set_material / free_slots /
add_body / add_joint / step / get_view / get_facts / set_anchor_target / destroy_joint / set_joint_thresholds / remove_body / free_joint_slots / free_body_slots / apply_impulse / cast_ray_closest / destroy`. `add_body` takes one descriptor (sphere, capsule, box, static box, anchor; position,
rotation, velocity, size, density, friction, restitution, rolling resistance, material id, parent, limb). Capacities are fixed at creation,
so lane pointers never move.
@ -135,6 +135,7 @@ separate native object (`ymir_box3d_soa.c`, ABI 17) so the Aetheria session's co
removed), flags (`TETHER`; `ONE_WAY`, Body only: the body side weighs nothing, so the attachment holds its particle to the body's point and never moves or loads the body, for flesh whose mass the bone already carries; `LIMB`, Body only: the particle is flesh of the body's limb tree, so contact skips every body whose limb-tree root, the `parent` lane followed to its end, is the attached body's), particle, body, point, compliance, `force` (sensor). Held lanes (derived, command time): per particle the list of bodies its live Body attachments name, which contact skips, and `limb_body`, the body of its lowest live `LIMB` attachment or -1. Limit: when a particle's `LIMB` attachments name bodies of two limb trees only the lowest one's tree is skipped; the other tree's bones collide, except the bodies the particle is attached to. Tether lanes (derived): attachment,
particle, bound, parent tether, edge, other attachment. Material table: friction, XSPH viscosity, wet friction and the caller's material id. Handles are
(lane, generation).
- Pinned, dormant, kinematic, anchor. `pinned` is a command-owned lane (`set_pinned`; `destroy_particles` frees it to 0). A pinned particle is dormant: no step walk visits it, its `x y z vx vy vz` are stale from the tick it was pinned until it wakes, and a reader draws it from its anchor, the point of its lowest live non-`TETHER` attachment (body or static), where the view's `particle_pinned` lane says which. A pinned particle that shares a live distance or bend with an unpinned one is kinematic: each substep its position is its anchor's point at the substep's start pose and its predicted position that point at the predicted pose, so the live neighbour is held against a moving border (its velocity is the anchor point's over the substep). Every step walk visits a list `rederive_active` derived at command time, the last step of every topology command and of `set_pinned`: `act` (live unpinned particles), `kin`, `act_dist` and `act_bend` (live constraints with an unpinned particle), `act_att` (attachments of unpinned particles), all in lane order, plus `anchor` and `w` (the inverse mass the step reads: 0 for pinned). With nothing pinned the lists are every live lane in lane order and `w` is `inv_mass`, bit for bit. A dormant particle is in no contact grid (flesh at rest meets no fluid and no body: it rides its bone, which is the body fluid and other bodies meet), writes no impulse, and a pinned fluid particle is refused. `set_pinned` refuses whole (INVALID_ARGUMENT) a stale or dead handle, a value other than 0 or 1, a fluid particle, and pinning a particle with no anchor or whose distance component holds a live `TETHER` attachment; a wake sets position to the anchor's point at the bodies' poses now and velocity to that point's velocity. `remove(attachment)` refuses a pinned particle's anchor. A tether attached to a component that already holds a pinned particle is not refused: wake first.
- Commands, each refused whole before anything changes: `create_particles(n, desc{position, velocity, rest, mass, radius,
phase, material})` takes the lowest free lanes; `destroy_particles(n, handles)` removes every constraint that touches them;
`create_distances(n, desc{a, b, rest (< 0 = rest-pose length), compliance})`; `create_bends(n, desc{e1, e2, compliance

View file

@ -62,7 +62,7 @@ enum
YMIR_SOA_JOINT_REMOVED = 4 // a retired joint's lane (destroy_joint): never accepted by add_joint
};
#define YMIR_SOA_ABI_VERSION 17u
#define YMIR_SOA_ABI_VERSION 18u
// A body to create. `size` by kind: sphere {radius}, capsule {radius, half_length} (segment along
// local Y), box and static box {half_x, half_y, half_z}. `parent` is the lane index of an earlier body
@ -329,6 +329,8 @@ typedef struct ymir_box3d_soa_view
const uint32_t* sensor_live;
const uint32_t* sensor_generation;
const uint64_t* sensor_removed_at;
// Particles: 1 for a pinned (dormant) particle, 0 else; notification only. A pinned particle's pos and vel are stale: draw it from its anchor attachment.
const uint32_t* particle_pinned;
} ymir_box3d_soa_view;
// A begin or end of touch between two bodies: their lane indices and the shapes' stable ids.
@ -1787,6 +1789,17 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_settle(ymir_box3d_soa_world* world,
return ymir_particles_settle(world->particles, n, h, world->step_count, out);
}
// Pins or wakes particles (see ymir_particles_set_pinned): the proxies are refreshed first, so a wake lands on the anchor's point at the bodies' poses now.
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_set_pinned(ymir_box3d_soa_world* world, int32_t n, const ymir_handle* h, uint32_t pinned)
{
if (world == NULL || world->particles == NULL)
{
return YMIR_SOA_INVALID_ARGUMENT;
}
ymir_soa_gather_proxies(world);
return ymir_particles_set_pinned(world->particles, n, h, pinned);
}
// Creates liquid sensors (see ymir_particles_create_sensors); a body must already exist.
YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create_sensors(ymir_box3d_soa_world* world, int32_t n, const ymir_sensor_desc* d, ymir_handle* out)
{
@ -1987,6 +2000,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_get_view(const ymir_box3d_soa_world
view->sensor_live = pt->sensor_live;
view->sensor_generation = pt->sensor_generation;
view->sensor_removed_at = pt->sensor_removed_at;
view->particle_pinned = pt->pinned;
}
return YMIR_SOA_OK;
}

View file

@ -1321,6 +1321,14 @@ int32_t ymir_particles_remove(ymir_particles* p, uint32_t type, int32_t n, const
return YMIR_PARTICLES_INVALID_ARGUMENT;
}
}
for (int32_t k = 0; type == YMIR_LANES_ATTACHMENT && k < n; ++k)
{
int32_t i = p->att_particle[h[k].lane];
if (p->pinned[i] && p->anchor[i] == h[k].lane)
{
return YMIR_PARTICLES_INVALID_ARGUMENT; // a pinned particle is drawn from its anchor: wake it first
}
}
if (type == YMIR_LANES_SENSOR)
{
for (int32_t k = 0; k < n; ++k)
@ -2663,6 +2671,62 @@ void ymir_particles_end(ymir_particles* p, int32_t substeps)
}
}
// ---- pinned: the command-owned dormancy of a particle ------------------------------------------------------------
// 1 when a live TETHER attachment sits on a particle of the same distance component as lane i.
static int component_tethered(const ymir_particles* p, int32_t i)
{
for (int32_t a = 0; a < p->attachment_count; ++a)
{
if (is_tether(p, a) && p->component[p->att_particle[a]] == p->component[i])
{
return 1;
}
}
return 0;
}
int32_t ymir_particles_set_pinned(ymir_particles* p, int32_t n, const ymir_handle* h, uint32_t pinned)
{
if (p == NULL || n < 0 || (n > 0 && h == NULL) || pinned > 1)
{
return YMIR_PARTICLES_INVALID_ARGUMENT;
}
for (int32_t k = 0; k < n; ++k)
{
int32_t i = h[k].lane;
if (!particle_ok(p, i, h[k].generation) || (p->phase[i] & YMIR_PHASE_FLUID) ||
(pinned && (p->anchor[i] < 0 || component_tethered(p, i))))
{
return YMIR_PARTICLES_INVALID_ARGUMENT; // a pinned particle is drawn from its anchor, and a tether would load a lane that no walk moves
}
}
for (int32_t k = 0; k < n; ++k)
{
int32_t i = h[k].lane;
if (pinned || !p->pinned[i])
{
p->pinned[i] = pinned; // 0 -> 1 writes only the lane
continue;
}
// wake: on the anchor's point, moving as that point moves, at the proxies' pose now
int32_t a = p->anchor[i];
v3 at = end_point(p, a), v = {0.0f, 0.0f, 0.0f};
if (p->att_kind[a] == YMIR_ATTACH_BODY)
{
const ymir_particle_proxy* b = p->proxies + p->att_body[a];
v = vadd((v3){b->velocity[0], b->velocity[1], b->velocity[2]},
vcross((v3){b->angular_velocity[0], b->angular_velocity[1], b->angular_velocity[2]},
vsub(at, (v3){b->position[0], b->position[1], b->position[2]})));
}
put(p->pos_x, p->pos_y, p->pos_z, i, at);
put(p->vel_x, p->vel_y, p->vel_z, i, v);
p->pinned[i] = 0;
}
rederive_active(p);
return YMIR_PARTICLES_OK;
}
// ---- settle: liquid becomes data ----------------------------------------------------------------------------
static float wet_disc_radius(float volume)

View file

@ -291,6 +291,12 @@ int32_t ymir_particles_create_bends(ymir_particles* p, int32_t n, const ymir_ben
// Attaches n particles in the lowest free lanes, by cursor, as a batch: every desc is validated, FULL when fewer than n lanes are free, one derivation, and a derivation that does not fit backs every lane out.
int32_t ymir_particles_attach(ymir_particles* p, int32_t n, const ymir_attachment_desc* d, int32_t body_count, uint64_t step_count, ymir_handle* out);
int32_t ymir_particles_remove(ymir_particles* p, uint32_t type, int32_t n, const ymir_handle* h, uint64_t step_count);
// Pins (1) or wakes (0) the named particles. A pinned particle is dormant: on no step walk (a particle of a live distance or bend with an
// unpinned particle is held on its anchor each substep instead), its pos and vel stale, drawn from its anchor. Refuses whole (INVALID_ARGUMENT,
// nothing changed) a stale or dead handle, `pinned` not 0 or 1, a fluid particle, and pinned 1 on a particle with no anchor or whose
// distance component holds a live TETHER attachment. The wake sets pos to the anchor's point at the proxies' pose now (the caller has filled
// them) and vel to that point's velocity. Ends in rederive_active. remove(ATTACHMENT) refuses a pinned particle's anchor.
int32_t ymir_particles_set_pinned(ymir_particles* p, int32_t n, const ymir_handle* h, uint32_t pinned);
// Settles the named fluid particles into wet patches and destroys them. Each reads its support from the last step (support_body, else
// support_particle) and its contact point from the proxies the caller has just filled; a drop's volume is (2 r)^3. Refuses whole
// (INVALID_ARGUMENT) a dead or non-fluid particle or one with no support, FULL when a drop finds no lane and no patch to merge into, before any

View file

@ -421,6 +421,7 @@ internal unsafe struct Box3DSoaViewNative
public uint* SensorLive;
public uint* SensorGeneration;
public ulong* SensorRemovedAt;
public uint* ParticlePinned;
}
/// <summary>Native layout of <c>ymir_box3d_soa_facts</c>: counts, dropped counters, then three pointers.</summary>
@ -667,6 +668,10 @@ internal static unsafe partial class Box3DSoaNative
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
internal static partial int ymir_box3d_soa_attach(nint world, int n, Box3DSoaAttachmentDesc* desc, Box3DSoaParticleHandle* output);
[LibraryImport(Library)]
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
internal static partial int ymir_box3d_soa_set_pinned(nint world, int n, Box3DSoaParticleHandle* particles, uint pinned);
[LibraryImport(Library)]
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
internal static partial int ymir_box3d_soa_create_sensors(nint world, int n, Box3DSoaSensorDesc* desc, Box3DSoaParticleHandle* output);
@ -938,6 +943,8 @@ public readonly unsafe struct Box3DSoaView
public ReadOnlySpan<uint> SensorLive => Lane(_v.SensorLive, SensorCount);
public ReadOnlySpan<uint> SensorGeneration => Lane(_v.SensorGeneration, SensorCount);
public ReadOnlySpan<ulong> SensorRemovedAt => Lane(_v.SensorRemovedAt, SensorCount);
/// <summary>1 for a pinned (dormant) particle, else 0; notification only. A pinned particle's position and velocity lanes are stale: draw it from its anchor (its lowest live non-tether attachment), see <see cref="Box3DSoaWorld.SetPinned"/>.</summary>
public ReadOnlySpan<uint> ParticlePinned => Lane(_v.ParticlePinned, ParticleCount);
/// <summary>N s, world: the impulse the particle phase delivered to each body over the last step (0 for a body it did not touch). Box3D has already applied it; this is notification only.</summary>
public ReadOnlySpan<float> ParticleImpulseX => Lane(_v.ParticleImpulseX, Count);
@ -956,7 +963,7 @@ public readonly unsafe struct Box3DSoaView
/// </summary>
public sealed class Box3DSoaWorld : IDisposable
{
private const uint ExpectedAbi = 17;
private const uint ExpectedAbi = 18;
private nint _handle;
/// <summary>
@ -1360,6 +1367,20 @@ public sealed class Box3DSoaWorld : IDisposable
}
}
/// <summary>
/// Pins (<paramref name="pinned"/> true) or wakes the named particles. A pinned particle is dormant: no step walk visits it, its position and velocity
/// lanes are stale, and a reader draws it from its anchor (<see cref="Box3DSoaView.ParticlePinned"/> says which). A pinned particle that shares a
/// live distance or bend with an unpinned one is held on its anchor each substep instead. A wake puts the particle on its anchor's point at the
/// bodies' poses now, moving as that point moves. Refuses the whole batch, changing nothing, for a stale or dead handle, a fluid particle, or pinning
/// a particle with no non-tether attachment or whose distance component holds a tether attachment. Removing a pinned particle's anchor attachment is refused.
/// </summary>
public unsafe void SetPinned(ReadOnlySpan<Box3DSoaParticleHandle> particles, bool pinned)
{
ObjectDisposedException.ThrowIf(_handle == 0, this);
fixed (Box3DSoaParticleHandle* h = particles)
Check(Box3DSoaNative.ymir_box3d_soa_set_pinned(_handle, particles.Length, h, pinned ? 1u : 0u));
}
/// <summary>
/// Settles fluid particles into wet patches and destroys them: each drop (volume (2 r)^3) joins the lowest patch on its support whose disc
/// holds its contact point, else takes the lowest free lane, else joins the nearest patch on that support. A drop's support is its

View file

@ -167,10 +167,10 @@ public sealed class Box3DSoaGrabTests
[Fact]
public void The_managed_side_refuses_a_native_library_of_another_abi_in_either_direction()
{
Assert.Equal(17u, Box3DSoaNative.ymir_box3d_soa_get_abi_version());
Box3DSoaWorld.RequireNativeAbi(17);
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(16)); // an older native under this managed build
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(18)); // a newer one
Assert.Equal(18u, Box3DSoaNative.ymir_box3d_soa_get_abi_version());
Box3DSoaWorld.RequireNativeAbi(18);
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(17)); // an older native under this managed build
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(19)); // a newer one
}
[Fact]