Pinning: a pinned particle keeps the anchor it was pinned on, the view names it, and attach refuses a tether on any particle of a component holding pinned flesh

The anchor was re-derived as the lowest live non-TETHER lane at every topology command, so an attach into a lower free lane changed which attachment a pinned particle is drawn from. rederive_active now holds a pinned particle's anchor, the view exposes it as particle_anchor, and the lane-order refusal is deleted. A TETHER whose distance component holds a pinned particle is refused, the mirror of set_pinned's component_tethered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-26 05:25:08 +01:00
commit f3af17ea07
6 changed files with 56 additions and 24 deletions

View file

@ -135,7 +135,7 @@ separate native object (`ymir_box3d_soa.c`, ABI 18) 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, so blood poured on dormant skin stops at the bone's capsule, within a skin radius of the skin's surface only while the capsule is that surface), 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. `attach` refuses whole a `TETHER` on a pinned particle and an attachment of a pinned particle that takes a lane below its anchor (the anchor is stable: wake first). A patch of settled liquid on a pinned particle is read at its anchor's point, so it rides the bone.
- 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) when it was pinned, held while it sleeps so no attachment another command adds can move it, where the view's `particle_pinned` lane says which and its `particle_anchor` lane names the attachment (readers read it, never re-derive it). 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, so blood poured on dormant skin stops at the bone's capsule, within a skin radius of the skin's surface only while the capsule is that surface), 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 particle whose distance component holds a pinned particle is refused whole by `attach`, the mirror of `set_pinned` refusing a tethered component (a tether loads every lane of its component, and no walk moves a pinned one): wake first. Any other attachment of a pinned particle is accepted in whatever lane it takes and leaves its anchor and pose as they were. A patch of settled liquid on a pinned particle is read at its anchor's point, so it rides the bone.
- 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

@ -331,6 +331,8 @@ typedef struct ymir_box3d_soa_view
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;
// Particles: the attachment lane a pinned particle is drawn from (held from the command that pinned it), -1 for none; derived, read only.
const int32_t* particle_anchor;
} ymir_box3d_soa_view;
// A begin or end of touch between two bodies: their lane indices and the shapes' stable ids.
@ -2001,6 +2003,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_get_view(const ymir_box3d_soa_world
view->sensor_generation = pt->sensor_generation;
view->sensor_removed_at = pt->sensor_removed_at;
view->particle_pinned = pt->pinned;
view->particle_anchor = pt->anchor;
}
return YMIR_SOA_OK;
}

View file

@ -623,7 +623,8 @@ static void rederive_active(ymir_particles* p)
int32_t np = p->particle_count;
for (int32_t i = 0; i < np; ++i)
{
p->anchor[i] = -1;
// a pinned particle keeps the anchor it was pinned on (remove refuses it, destroy unpins), so no lane another command takes can move its pose
p->anchor[i] = p->pinned[i] ? p->anchor[i] : -1;
p->w[i] = p->particle_live[i] && !p->pinned[i] ? p->inv_mass[i] : 0.0f;
p->done[i] = 0; // scratch: marks the kinematic
}
@ -1236,6 +1237,19 @@ int32_t ymir_particles_create_bends(ymir_particles* p, int32_t n, const ymir_ben
return rederive_topology(p, TOPO_FULL, NULL, 0);
}
// 1 when a live pinned particle sits in the same distance component as lane i.
static int component_pinned(const ymir_particles* p, int32_t i)
{
for (int32_t j = 0; j < p->particle_count; ++j)
{
if (p->particle_live[j] && p->pinned[j] && p->component[j] == p->component[i])
{
return 1;
}
}
return 0;
}
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)
{
if (p == NULL || n < 0 || (n > 0 && d == NULL))
@ -1275,13 +1289,12 @@ int32_t ymir_particles_attach(ymir_particles* p, int32_t n, const ymir_attachmen
p->att_compliance[a] = d[k].compliance;
p->att_force_x[a] = p->att_force_y[a] = p->att_force_z[a] = 0.0f;
}
// A pinned particle is drawn from its anchor, the lowest live non-TETHER lane, and holds no tether (no walk moves the lane a tether would bound): an attach
// that would add a tether to it, or take a lane below its anchor, is refused whole like removing the anchor. Wake it first.
// A tether loads every lane of its distance component, and no walk moves a pinned one: a TETHER on a particle whose component holds pinned flesh is
// refused whole, the mirror of set_pinned refusing a tethered component. Wake it first. The component lane is the one before this command: an attach joins no component.
int32_t status = YMIR_PARTICLES_OK;
for (int32_t k = 0; k < n && status == YMIR_PARTICLES_OK; ++k)
{
int32_t i = d[k].particle.lane, a = p->lane_list[k];
if (p->pinned[i] && ((d[k].flags & YMIR_ATTACH_FLAG_TETHER) != 0 || (p->anchor[i] >= 0 && a < p->anchor[i])))
if ((d[k].flags & YMIR_ATTACH_FLAG_TETHER) != 0 && component_pinned(p, d[k].particle.lane))
{
status = YMIR_PARTICLES_INVALID_ARGUMENT;
}

View file

@ -197,7 +197,7 @@ typedef struct ymir_particles
// distance or bend reaches (the step sets their pos and pp from their anchor); act_dist and act_bend: the live constraints with an unpinned particle;
// act_att: the live attachments of unpinned particles. All in lane order, so with nothing pinned they are every live lane and w is inv_mass.
int32_t *act, *kin, *act_dist, *act_bend, *act_att;
int32_t* anchor; // derived: the lane of the particle's lowest live non-TETHER attachment (BODY or STATIC), -1 for none
int32_t* anchor; // derived: the lane of the particle's lowest live non-TETHER attachment (BODY or STATIC), -1 for none; a pinned particle keeps the one it was pinned on
float* w; // derived: the inverse mass the step reads: 0 for a pinned or free lane, else inv_mass
int32_t* component; // derived: the lowest lane of the particle's distance-connected component
int32_t *held_start, *held_count; // derived (rederive_topology): the slice of held_body that lists the bodies particle i is attached to (BODY attachments), contact skips them

View file

@ -422,6 +422,7 @@ internal unsafe struct Box3DSoaViewNative
public uint* SensorGeneration;
public ulong* SensorRemovedAt;
public uint* ParticlePinned;
public int* ParticleAnchor;
}
/// <summary>Native layout of <c>ymir_box3d_soa_facts</c>: counts, dropped counters, then three pointers.</summary>
@ -943,8 +944,10 @@ 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>
/// <summary>1 for a pinned (dormant) particle, else 0; notification only. A pinned particle's position and velocity lanes are stale: draw it from the attachment <see cref="ParticleAnchor"/> names, see <see cref="Box3DSoaWorld.SetPinned"/>.</summary>
public ReadOnlySpan<uint> ParticlePinned => Lane(_v.ParticlePinned, ParticleCount);
/// <summary>The attachment lane each pinned particle is drawn from: its lowest live non-tether attachment when it was pinned, held while it sleeps, so no attachment added later moves it; -1 for none. Derived, read only.</summary>
public ReadOnlySpan<int> ParticleAnchor => Lane(_v.ParticleAnchor, 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);
@ -1302,8 +1305,8 @@ public sealed class Box3DSoaWorld : IDisposable
/// <summary>
/// Ties each particle of <paramref name="attachments"/> to a body point or a world point, as one command ending in one derivation:
/// lanes are taken lowest first in order, and a refusal (a bad desc, a removed body, no free lanes, tethers past capacity) throws
/// with nothing changed. <paramref name="handles"/> may be empty.
/// lanes are taken lowest first in order, and a refusal (a bad desc, a removed body, no free lanes, tethers past capacity, a tether on a particle
/// whose distance component holds a pinned particle) throws with nothing changed. A pinned particle's anchor is held whatever lane an attachment takes. <paramref name="handles"/> may be empty.
/// </summary>
public unsafe void Attach(ReadOnlySpan<Box3DSoaAttachmentDesc> attachments, Span<Box3DSoaConstraintHandle> handles = default)
{
@ -1369,7 +1372,7 @@ 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
/// lanes are stale, and a reader draws it from its anchor (<see cref="Box3DSoaView.ParticlePinned"/> says which, <see cref="Box3DSoaView.ParticleAnchor"/> names the attachment). 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.

View file

@ -464,6 +464,7 @@ public sealed class Box3DSoaPinTests
Run(s, 3);
var free = w.FreeAttachmentSlots;
Refused(s, () => w.Attach(Box3DSoaAttachKind.Static, rope.Particles[5], point: (1f, 1f, 0f), tether: true), "a tether on a pinned particle");
Refused(s, () => w.Attach(Box3DSoaAttachKind.Static, rope.Particles[0], point: (1f, 1f, 0f), tether: true), "a tether on live flesh of a component that holds pinned flesh");
Refused(s, () => w.Attach([new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Static, rope.Particles[0], -1, (1f, 1f, 0f), 0f, false, false, false),
new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Static, rope.Particles[5], -1, (1f, 1f, 0f), 0f, true, false, false)]), "a batch with one tether on a pinned particle");
Assert.Equal(free, w.FreeAttachmentSlots);
@ -473,23 +474,35 @@ public sealed class Box3DSoaPinTests
Refused(s, () => w.SetPinned([rope.Particles[4]], true), "a tethered component");
}
[Fact]
public void An_attach_cannot_move_a_pinned_particles_anchor()
[Theory]
[InlineData(true)]
[InlineData(false)]
public void An_attach_in_any_lane_leaves_a_pinned_particles_anchor(bool freeBelow)
{
using var s = Chained();
using var s = Chained(extra: 4);
var w = s.World;
var (a, b, c) = (s.P[0][0], s.P[0][1], s.P[0][2]);
w.SetPinned([b], true); // b's anchor is its lane Anchor[0][1] = 1, above the lane 0 that a's attachment holds
w.Remove(s.Anchor[0][0]);
Run(s, 2); // a freed lane is reused only a step later
Refused(s, () => w.Attach(Box3DSoaAttachKind.Static, b, point: (1f, 1f, 1f)), "an attachment that would take a lane below the anchor");
var free = w.Attach(Box3DSoaAttachKind.Static, c, point: (0.5f, 1.8f, 0f)); // an unpinned particle takes the lane
Assert.Equal(0, free.Lane);
w.Attach(Box3DSoaAttachKind.Static, b, point: (1f, 1f, 1f)); // now above the anchor: accepted
var (b, c) = (s.P[0][1], s.P[0][2]);
w.SetPinned([b], true); // b's anchor is lane Anchor[0][1] = 1
if (freeBelow)
{
w.Remove(s.Anchor[0][0]); // lane 0, below b's anchor
Run(s, 2); // a freed lane is reused only a step later
}
var v = s.View;
var lanes = ParticleLanes(v);
// a mixed batch, new flesh beside resting flesh plus a second attachment on the resting particle: accepted whatever lanes are free
var made = new Box3DSoaConstraintHandle[2];
w.Attach([new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Static, b, -1, (1f, 1f, 1f), 0f, false, false, false),
new Box3DSoaAttachmentDesc(Box3DSoaAttachKind.Static, c, -1, (0.5f, 1.8f, 0f), 0f, false, false, false)], made);
Assert.Equal(freeBelow, made[0].Lane < s.Anchor[0][1].Lane); // b's second attachment lands below its anchor when a lane there is free
w.Attach(Box3DSoaAttachKind.Static, b, point: (1f, 1f, 1f)); // and alone
v = s.View;
Assert.Equal(s.Anchor[0][1].Lane, v.ParticleAnchor[b.Lane]);
Assert.Equal(lanes, ParticleLanes(v)); // bit for bit
Run(s, 2);
w.SetPinned([b], false);
var v = s.View;
Assert.True(Dist(At(v, b), AnchorNow(s, 0, 1).Point) <= WakeAnchor, "a woken particle lands on the anchor it was pinned on");
v = s.View;
Assert.True(Dist(At(v, b.Lane), AnchorNow(s, 0, 1).Point) <= WakeAnchor, "a woken particle lands on the anchor it was pinned on");
}
[Fact]