From 80bf68d44a7953ec2c339f671e630bae34b39ead Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 26 Nov 2026 04:31:31 +0100 Subject: [PATCH] Pinning: refuse a tether and an anchor-moving attach on a pinned particle, read settled liquid on pinned flesh at its anchor, pin the dormant-blood and wake-live cases Co-Authored-By: Claude Sonnet 5.5 --- docs/soa-world.md | 2 +- native/Ymir.Box3D/ymir_particles.c | 21 +++- .../Box3DSoaPinFluidTests.cs | 115 ++++++++++++++++++ .../Ymir.Box3D.Soa.Tests/Box3DSoaPinTests.cs | 77 ++++++++++++ 4 files changed, 212 insertions(+), 3 deletions(-) create mode 100644 tests/Ymir.Box3D.Soa.Tests/Box3DSoaPinFluidTests.cs diff --git a/docs/soa-world.md b/docs/soa-world.md index 07762f5..c744eae 100644 --- a/docs/soa-world.md +++ b/docs/soa-world.md @@ -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), 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. + - 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. - 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 diff --git a/native/Ymir.Box3D/ymir_particles.c b/native/Ymir.Box3D/ymir_particles.c index 8ad0cd1..425780e 100644 --- a/native/Ymir.Box3D/ymir_particles.c +++ b/native/Ymir.Box3D/ymir_particles.c @@ -1275,7 +1275,21 @@ 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; } - int32_t status = rederive_topology(p, TOPO_FULL, NULL, 0); + // 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. + 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]))) + { + status = YMIR_PARTICLES_INVALID_ARGUMENT; + } + } + if (status == YMIR_PARTICLES_OK) + { + status = rederive_topology(p, TOPO_FULL, NULL, 0); + } if (status != YMIR_PARTICLES_OK) { for (int32_t k = 0; k < n; ++k) // back out: the lanes read as they did before the command @@ -2600,7 +2614,10 @@ static float pooled_volume(const ymir_particles* p, v3 c, float r) { if (p->wet_live[w] && p->wet_particle[w] >= 0 && p->wet_particle[w] < p->particle_count && p->particle_live[p->wet_particle[w]]) { - v3 d = vsub(P(p, p->pos_x, p->pos_y, p->pos_z, p->wet_particle[w]), c); + int32_t i = p->wet_particle[w]; + // a pinned particle's pos is stale: it is where its anchor holds it now + v3 at = p->pinned[i] && p->anchor[i] >= 0 ? end_point(p, p->anchor[i]) : P(p, p->pos_x, p->pos_y, p->pos_z, i); + v3 d = vsub(at, c); if (vdot(d, d) <= r2) { total += p->wet_volume[w]; diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaPinFluidTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaPinFluidTests.cs new file mode 100644 index 0000000..5c83a1e --- /dev/null +++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaPinFluidTests.cs @@ -0,0 +1,115 @@ +using Ymir.Box3D; + +namespace Ymir.Box3D.Soa.Tests; + +using Pos = (float X, float Y, float Z); + +/// Pinned flesh and liquid (docs/soa-world.md, pinned, dormant, kinematic, anchor): settled liquid on dormant flesh rides the flesh's bone, and poured liquid stops at the bone under dormant skin. +public sealed partial class Box3DSoaFluidTests +{ + /// + /// A kinematic bone with a 5 x 5 plate of flesh on it (one-way, limb), one drop settled on the plate, a sensor on the bone around the drop. With the plate is + /// pinned after the settle. Returns the sensor's wet reading after the bone has moved m along x, and the reading before. + /// + private static (float Before, float After) WetOnMovingBone(bool pin, float travel) + { + using var world = new Box3DSoaWorld(8, gravityY: -10f, particles: new Box3DSoaParticleCapacities(64, 16, 16, 32, 64, 2, 64, 4, 2)); + var bone = world.AddAnchor(1, (0f, 0.3f, 0f)); + var plate = Graph.Block(world, (-0.05f, 0.3f, -0.05f), 5, 1, 5, R, 0.01f, Box3DSoaPhase.Of(4)); + var drop = Graph.Block(world, (0f, 0.3f + 2f * R + 0.001f, 0f), 1, 1, 1, R, FluidMass, FluidPhase); + for (var i = 0; i < plate.Length; i++) + { + var q = world.View; + world.Attach(Box3DSoaAttachKind.Body, plate[i], bone, (q.ParticleX[plate[i]], 0f, q.ParticleZ[plate[i]]), limb: true, oneWay: true); + } + var sensor = new Box3DSoaConstraintHandle[1]; + world.CreateSensors([(bone, (0f, 0f, 0f), 0.3f)], sensor); + Run(world, 20); + Assert.True(world.View.SupportParticle[drop[0].Lane] >= 0, "the drop rests on the plate"); + world.Settle(drop); + if (pin) world.SetPinned(plate, true); + world.Step(Dt, Sub); + var before = world.View.SensorWetVolume[sensor[0]]; + var steps = 30; + for (var s = 1; s <= steps; s++) + { + world.SetAnchorTarget(bone, (travel * s / steps, 0.3f, 0f), default, Dt); + world.Step(Dt, Sub); + } + world.Step(Dt, Sub); + return (before, world.View.SensorWetVolume[sensor[0]]); + } + + [Fact] + public void Settled_liquid_on_pinned_flesh_rides_its_bone() + { + // The drop settled on flesh that is then pinned: the sensor on the bone reads it after the bone has moved 2 m, as it does for live flesh. A patch read at the + // dormant particle's stale position would be left behind and read 0. + var live = WetOnMovingBone(false, 2f); + var pinned = WetOnMovingBone(true, 2f); + Readings.Add($"PIN_WET sensor on the bone before and after it moved 2 m: live {live.Before:E4} {live.After:E4}, pinned {pinned.Before:E4} {pinned.After:E4}"); + Assert.Equal(DropVolume, live.Before); + Assert.Equal(live.Before, live.After); + Assert.Equal(DropVolume, pinned.Before); + Assert.Equal(pinned.Before, pinned.After); + } + + private const float CapR = 0.07f, CapHalf = 0.18f, SkinR = 0.036f; + + /// Per fluid particle, the closest its centre came to the capsule's axis over a 343-particle pour on a horizontal capsule under a 36-particle skin ring of flesh, live or pinned. + private static (double[] Closest, int Inside) PourOnSkin(bool pinned) + { + const int Ring = 36; + using var world = new Box3DSoaWorld(8, gravityY: -10f, jointCapacity: 4, + particles: new Box3DSoaParticleCapacities(Ring + 343 + 8, 16, 16, Ring + 8, 16, 2, 343)); + var wall = world.AddStaticBox(1, (2f, 0.5f, 0f), (0.05f, 0.05f, 0.05f)); + var sq = MathF.Sqrt(0.5f); + var bone = world.AddCapsule(2, (0f, 0.5f, 0f), CapR, CapHalf, 500f, rotation: (0f, 0f, sq, sq)); // local Y lies along world x + world.AddJoint(Box3DSoaJointKind.Weld, wall, bone, (0f, 0.5f, 0f), (0f, 0f, 1f)); + var skin = new Box3DSoaParticleHandle[Ring]; + var local = new Pos[Ring]; + var desc = new Box3DSoaParticleDesc[Ring]; + for (var k = 0; k < Ring; k++) + { + var a = 2f * MathF.PI * k / Ring; + local[k] = (CapR * MathF.Cos(a), 0f, CapR * MathF.Sin(a)); // a ring in the bone's xz plane, around its axis y + } + // rotation (0, 0, s, s) is +90 degrees about z: local (x, y, z) -> world (-y, x, z), plus the bone's centre (0, 0.5, 0) + for (var k = 0; k < Ring; k++) desc[k] = new Box3DSoaParticleDesc((-local[k].Y, local[k].X + 0.5f, local[k].Z), 0.02f, SkinR, Box3DSoaPhase.Of(4)); + world.CreateParticles(desc, skin); + for (var k = 0; k < Ring; k++) world.Attach(Box3DSoaAttachKind.Body, skin[k], bone, local[k], limb: true, oneWay: true); + if (pinned) world.SetPinned(skin, true); + var pour = Graph.Block(world, (-0.15f + 0.0125f, 0.5f + 0.45f, -0.15f + 0.0125f), 7, 7, 7, R, FluidMass, FluidPhase, pitch: 2f * R); + var closest = Enumerable.Repeat(double.MaxValue, pour.Length).ToArray(); + var inside = 0; + for (var s = 0; s < 240; s++) + { + world.Step(Dt, Sub); + var v = world.View; + for (var i = 0; i < pour.Length; i++) + { + var l = pour[i].Lane; + double y = v.ParticleY[l] - 0.5, z = v.ParticleZ[l]; + var d = Math.Sqrt(y * y + z * z); + if (Math.Abs(v.ParticleX[l]) < CapHalf) closest[i] = Math.Min(closest[i], d); + if (Math.Abs(v.ParticleX[l]) < CapHalf && d < CapR) inside++; + } + } + return (closest, inside); + } + + [Fact] + public void Blood_poured_on_dormant_skin_stops_at_the_bone() + { + // Dormant flesh meets no fluid, so liquid on pinned skin stops at the bone's capsule (centre at R + r), not at the skin's surface (R + skin radius + r) where live flesh stops it. + static double Median(double[] x) { var c = x.Where(d => d != double.MaxValue).OrderBy(d => d).ToArray(); return c[c.Length / 2]; } + var live = PourOnSkin(false); + var dormant = PourOnSkin(true); + double stop = CapR + R; + var (lm, dm) = (Median(live.Closest), Median(dormant.Closest)); + Readings.Add($"PIN_BLOOD median closest approach to the axis: live skin {lm:F4}, dormant skin {dm:F4}, capsule surface plus fluid radius {stop:F4}; centres inside the capsule live {live.Inside}, dormant {dormant.Inside}"); + Assert.Equal(0, dormant.Inside); + Assert.True(dm >= stop - 0.002 && dm <= stop + 0.008, $"dormant blood lies {dm} from the axis, the capsule stops it at {stop}"); + Assert.True(lm > dm + 0.008, $"live skin holds the blood off at {lm}, dormant at {dm}"); + } +} diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaPinTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaPinTests.cs index dc44ebd..3f09bfc 100644 --- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaPinTests.cs +++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaPinTests.cs @@ -449,4 +449,81 @@ public sealed class Box3DSoaPinTests Reading(line); Assert.True(Median(samples[1]) > 0); } + + // ---- (g) attach, wake and remove against a pinned particle ---------------------------------------------- + + [Fact] + public void A_pinned_particle_takes_no_tether() + { + using var s = Chained(extra: 16); + var w = s.World; + var rope = Graph.Rope(w, Graph.Line((1f, 1f, 0f), (0.03f, 0f, 0f), 6), 0.02f, 0.0125f, Box3DSoaPhase.Of(7, noParticleContact: true)); + w.Attach(Box3DSoaAttachKind.Body, rope.Particles[5], s.Bones[0], (0f, 0f, 0.2f)); + s.View = w.View; + w.SetPinned([rope.Particles[5]], true); + 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([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); + Assert.Equal(1u, s.View.ParticlePinned[rope.Particles[5]]); + w.SetPinned([rope.Particles[5]], false); // woken, it takes the tether like any particle + w.Attach(Box3DSoaAttachKind.Static, rope.Particles[5], point: (1f, 1f, 0f), tether: true); + Refused(s, () => w.SetPinned([rope.Particles[4]], true), "a tethered component"); + } + + [Fact] + public void An_attach_cannot_move_a_pinned_particles_anchor() + { + using var s = Chained(); + 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 + 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"); + } + + [Fact] + public void Waking_a_live_particle_moves_nothing() + { + using var s = Chained(); + var w = s.World; + Run(s, 10); + var v = s.View; + var live = s.P[2][7]; + var (x, y, z, vx, vy, vz) = (v.ParticleX[live], v.ParticleY[live], v.ParticleZ[live], v.ParticleVx[live], v.ParticleVy[live], v.ParticleVz[live]); + w.SetPinned([live], false); + Assert.Equal((x, y, z, vx, vy, vz), (v.ParticleX[live], v.ParticleY[live], v.ParticleZ[live], v.ParticleVx[live], v.ParticleVy[live], v.ParticleVz[live])); + w.SetPinned([s.P[0][0]], true); + Run(s, 3); + (x, y, z, vx, vy, vz) = (v.ParticleX[live], v.ParticleY[live], v.ParticleZ[live], v.ParticleVx[live], v.ParticleVy[live], v.ParticleVz[live]); + w.SetPinned([live, s.P[0][0]], false); // a batch of a live and a pinned particle: only the pinned one is put on its anchor + Assert.Equal((x, y, z, vx, vy, vz), (v.ParticleX[live], v.ParticleY[live], v.ParticleZ[live], v.ParticleVx[live], v.ParticleVy[live], v.ParticleVz[live])); + Assert.Equal(0u, v.ParticlePinned[s.P[0][0]]); + } + + [Fact] + public void A_body_that_pinned_flesh_is_attached_to_cannot_be_removed() + { + using var s = Chained(); + var w = s.World; + w.SetPinned(s.P[1], true); + Run(s, 3); + var before = HashAll(s.View); + var bone = new Box3DSoaBodyHandle(s.Bones[1], s.View.BodyGeneration[s.Bones[1]]); + Assert.Throws(() => w.RemoveBody(bone)); + Assert.Equal((uint)Box3DSoaKind.Capsule, s.View.Kind[s.Bones[1]]); + Assert.Equal(before, HashAll(s.View)); + Run(s, 3); + Assert.True(s.View.PositionY[s.Bones[1]] > 1f); // the body is where it was + } }