Ymir SoA: remove_body, refused while named

Body kind REMOVED, body_generation/body_removed_at, body slot reuse (twin of the joint slot), free_body_slots, remove_body(lane, generation): refused whole while a live attachment or sensor names the body; else wet patches go (ymir_particles_release_body, no Box3D call), joints naming it retire, Box3D destroys it, the lane tombstones. Every per-body loop skips REMOVED; attach, sensors, add_joint and parent refuse a removed lane. Box3DSoaBodyHandle returned by every Add* body helper. A removed body's shape ends a touch only by skipping it here; the next commit names it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-25 21:24:11 +01:00
commit e1f1449927
8 changed files with 526 additions and 28 deletions

View file

@ -36,7 +36,7 @@ public sealed class Box3DSoaImpulseTests
var fore = world.AddCapsule(3, (0.78f, 0.2f, 0f), 0.04f, 0.14f, 1000f, rotation: QuarterTurnZ);
world.AddJoint(Box3DSoaJointKind.Spherical, torso, upper, (0.2f, 0.2f, 0f), (0f, 0f, 1f));
world.AddJoint(Box3DSoaJointKind.Revolute, upper, fore, (0.6f, 0.2f, 0f), (0f, 0f, 1f));
var arm = new[] { (torso, 0.35f * 0.5f * 0.2f * 680f), (upper, CapsuleMass(0.045f, 0.15f, 1000f)), (fore, CapsuleMass(0.04f, 0.14f, 1000f)) };
var arm = new (int Lane, float Mass)[] { (torso, 0.35f * 0.5f * 0.2f * 680f), (upper, CapsuleMass(0.045f, 0.15f, 1000f)), (fore, CapsuleMass(0.04f, 0.14f, 1000f)) };
return (world, arm);
}

View file

@ -0,0 +1,282 @@
using Ymir.Box3D;
namespace Ymir.Box3D.Soa.Tests;
/// <summary>Body removal: docs/soa-world.md, Release. RemoveBody is the one body-ending path; it is refused while a liquid sensor or attachment names the body.</summary>
public sealed class Box3DSoaRemoveTests
{
private const float Dt = 1f / 60f;
private const float R = 0.0125f;
private static Box3DSoaWorld World(int bodies = 8, int joints = 4, int fluid = 16, int wets = 8, int sensors = 4) =>
new(bodies, gravityY: -10f, jointCapacity: joints,
particles: new Box3DSoaParticleCapacities(16, 16, 16, 4, 16, 2, fluid, wets, sensors));
private static void Run(Box3DSoaWorld world, int steps)
{
for (var i = 0; i < steps; i++) world.Step(Dt);
}
/// <summary>The state a refusal must leave alone: every body and joint lane that RemoveBody writes.</summary>
private static string Snapshot(Box3DSoaWorld world)
{
var v = world.View;
return string.Join(",", v.Kind.ToArray()) + "|" + string.Join(",", v.BodyGeneration.ToArray()) + "|" + string.Join(",", v.StableId.ToArray()) +
"|" + string.Join(",", v.JointKind.ToArray()) + "|" + string.Join(",", v.JointGeneration.ToArray()) + "|" + string.Join(",", v.WetLive.ToArray()) +
"|" + string.Join(",", v.SensorLive.ToArray()) + "|" + string.Join(",", v.AttachmentKind.ToArray()) + "|" + world.StepCount;
}
[Fact]
public void Removing_a_body_retires_its_joints_and_zeroes_its_lane()
{
using var world = World();
var pin = world.AddStaticBox(1, (0f, 5f, 0f), (0.1f, 0.1f, 0.1f));
var upper = world.AddCapsule(2, (0f, 4.5f, 0f), 0.1f, 0.4f, 1000f, parent: pin, limb: 3, materialId: 9);
var lower = world.AddCapsule(3, (0f, 3.5f, 0f), 0.1f, 0.4f, 1000f, parent: upper);
var elsewhere = world.AddSphere(4, (3f, 5f, 0f), 0.1f, 1000f);
var j1 = world.AddJoint(Box3DSoaJointKind.Spherical, pin, upper, (0f, 5f, 0f), (0f, 0f, 1f));
var j2 = world.AddJoint(Box3DSoaJointKind.Revolute, upper, lower, (0f, 4f, 0f), (0f, 0f, 1f));
var j3 = world.AddJoint(Box3DSoaJointKind.Weld, pin, elsewhere, (1f, 5f, 0f), (0f, 0f, 1f));
Run(world, 10);
var view = world.View;
Assert.Equal(upper.Generation, view.BodyGeneration[upper]);
world.RemoveBody(upper);
Assert.Equal((uint)Box3DSoaKind.Removed, view.Kind[upper]);
Assert.Equal(upper.Generation + 1, view.BodyGeneration[upper]);
Assert.Equal(0ul, view.StableId[upper]);
Assert.Equal(-1, view.Parent[upper]);
Assert.Equal(0, view.Limb[upper]);
Assert.Equal(0ul, view.MaterialId[upper]);
Assert.Equal(0, view.ContactCount[upper]);
Assert.Equal(0f, view.PositionX[upper] + view.PositionY[upper] + view.PositionZ[upper]);
Assert.Equal(0f, view.RotationX[upper] + view.RotationW[upper] + view.VelocityY[upper]);
Assert.Equal(0f, view.Size0[upper] + view.Size1[upper] + view.ContactImpulse[upper]);
foreach (var joint in new[] { j1, j2 })
{
Assert.Equal((uint)Box3DSoaJointKind.Removed, view.JointKind[joint]);
Assert.Equal(joint.Generation + 1, view.JointGeneration[joint]);
Assert.Equal(-1, view.JointBodyA[joint]);
Assert.Throws<InvalidOperationException>(() => world.DestroyJoint(joint)); // the old handles are stale
}
Assert.Equal((uint)Box3DSoaJointKind.Weld, view.JointKind[j3]); // a joint that does not name it stays
Assert.Throws<InvalidOperationException>(() => world.RemoveBody(upper)); // already gone
Assert.Throws<InvalidOperationException>(() => world.RemoveBody(new Box3DSoaBodyHandle(lower.Lane, lower.Generation + 1))); // stale
Assert.Throws<InvalidOperationException>(() => world.RemoveBody(new Box3DSoaBodyHandle(8, 0))); // out of range
Assert.Throws<InvalidOperationException>(() => world.RemoveBody(new Box3DSoaBodyHandle(-1, 0)));
Run(world, 30); // the world steps on, the removed lane stays a tombstone, the orphan falls free
Assert.Equal((uint)Box3DSoaKind.Removed, view.Kind[upper]);
Assert.Equal(0f, view.PositionY[upper]);
Assert.True(view.PositionY[lower] < 3.5f - 0.5f, "the child of a removed body is free");
Assert.Equal(lower.Generation, view.BodyGeneration[lower]);
}
[Fact]
public void Removal_is_refused_while_an_attachment_or_a_sensor_names_the_body()
{
using var world = World();
var floor = world.AddStaticBox(1, (0f, -0.5f, 0f), (2f, 0.5f, 2f));
var box = world.AddBox(2, (0f, 0.1f, 0f), (0.1f, 0.1f, 0.1f), 500f);
var other = world.AddSphere(3, (1f, 0.1f, 0f), 0.1f, 500f);
var joint = world.AddJoint(Box3DSoaJointKind.Weld, box, other, (0.5f, 0.1f, 0f), (0f, 0f, 1f));
var p = new Box3DSoaParticleHandle[1];
world.CreateParticles([new((0f, 0.1f, 0f), 0.02f, R)], p);
var attachment = world.Attach(Box3DSoaAttachKind.Body, p[0], box, (0f, 0f, 0f));
var sensors = new Box3DSoaConstraintHandle[1];
world.CreateSensors([(box, (0f, 0f, 0f), 0.2f)], sensors);
Run(world, 5);
var before = Snapshot(world);
Assert.Throws<InvalidOperationException>(() => world.RemoveBody(box)); // both name it
Assert.Equal(before, Snapshot(world));
world.Remove(attachment);
Assert.Throws<InvalidOperationException>(() => world.RemoveBody(box)); // the sensor still does
Assert.Equal((uint)Box3DSoaKind.Box, world.View.Kind[box]);
Assert.Equal((uint)Box3DSoaJointKind.Weld, world.View.JointKind[joint]); // a refusal retired nothing
world.Remove(sensors[0]);
world.RemoveBody(box);
var removed = box.Lane;
Assert.Equal((uint)Box3DSoaKind.Removed, world.View.Kind[removed]);
Assert.Throws<InvalidOperationException>(() => world.Attach(Box3DSoaAttachKind.Body, p[0], removed, (0f, 0f, 0f)));
Assert.Throws<InvalidOperationException>(() => world.CreateSensors([(removed, (0f, 0f, 0f), 0.2f)]));
Assert.Throws<InvalidOperationException>(() => world.AddJoint(Box3DSoaJointKind.Weld, floor, removed, (0f, 0f, 0f), (0f, 0f, 1f)));
Assert.Throws<InvalidOperationException>(() => world.AddSphere(10, (2f, 1f, 0f), 0.1f, 500f, parent: removed));
Assert.Equal(0, world.View.AttachmentCount == 0 ? 0 : world.View.AttachmentKind.ToArray().Count(k => k == (uint)Box3DSoaAttachKind.Body));
}
[Fact]
public void A_removed_anchor_ends_its_grab()
{
using var world = World();
var anchor = world.AddAnchor(1, (0f, 5f, 0f));
var sphere = world.AddSphere(2, (0f, 5f, 0f), 0.1f, 1000f);
var grab = world.AddMotorJoint(anchor, sphere, (0f, 5f, 0f), (5f, 0.7f, 5000f), (0f, 0f, 0f));
Run(world, 30);
var view = world.View;
Assert.Equal(5f, view.PositionY[sphere], 0.3f); // held
world.RemoveBody(anchor);
Assert.Equal((uint)Box3DSoaJointKind.Removed, view.JointKind[grab]);
Assert.Throws<InvalidOperationException>(() => world.DestroyJoint(grab)); // the grab handle is stale
Run(world, 60);
Assert.True(view.PositionY[sphere] < 3f, "the grabbed body falls");
}
[Fact]
public void A_drop_resting_on_a_removed_body_cannot_settle()
{
using var world = World();
var floor = world.AddStaticBox(1, (0f, -0.5f, 0f), (2f, 0.5f, 2f), friction: 0.3f);
var p = new Box3DSoaParticleHandle[1];
world.CreateParticles([new((0f, R, 0f), 0.015625f, R, Box3DSoaPhase.Of(0, fluid: true), 0u, null, default)], p);
Run(world, 20);
var view = world.View;
Assert.Equal(floor.Lane, view.SupportBody[0]);
world.RemoveBody(floor);
Assert.Equal(floor.Lane, view.SupportBody[0]); // last step's fact: it may name the dead lane until the next substep
var before = Snapshot(world);
Assert.Throws<InvalidOperationException>(() => world.Settle(p));
Assert.Equal(before, Snapshot(world));
Assert.Equal(1u, view.ParticleLive[0]);
}
[Fact]
public void Wet_patches_go_with_the_body()
{
using var world = World(fluid: 16, wets: 16);
var a = world.AddStaticBox(1, (-1f, -0.5f, 0f), (0.5f, 0.5f, 0.5f), friction: 0.3f);
var b = world.AddStaticBox(2, (1f, -0.5f, 0f), (0.5f, 0.5f, 0.5f), friction: 0.3f);
var at = new[] { -1.3f, -1f, -0.7f, 0.9f, 1.2f };
var phase = Box3DSoaPhase.Of(0, fluid: true);
world.CreateParticles(at.Select(x => new Box3DSoaParticleDesc((x, R, 0f), 0.015625f, R, phase, 0u, null, default)).ToArray());
Run(world, 20);
var view = world.View;
Assert.Equal(new[] { a.Lane, a.Lane, a.Lane, b.Lane, b.Lane }, Enumerable.Range(0, 5).Select(i => view.SupportBody[i]).ToArray());
world.Settle(Enumerable.Range(0, 5).Select(i => new Box3DSoaParticleHandle(i, view.ParticleGeneration[i])).ToArray());
view = world.View;
var onA = Enumerable.Range(0, view.WetCount).Where(w => view.WetLive[w] != 0 && view.WetBody[w] == a.Lane).ToArray();
var onB = Enumerable.Range(0, view.WetCount).Where(w => view.WetLive[w] != 0 && view.WetBody[w] == b.Lane).ToArray();
Assert.Equal(3, onA.Length);
Assert.Equal(2, onB.Length);
var volumeB = view.WetVolume[b.Lane];
var generationsB = onB.Select(w => view.WetGeneration[w]).ToArray();
Assert.True(view.WetVolume[a.Lane] > 0f && volumeB > 0f);
var generationsA = onA.Select(w => view.WetGeneration[w]).ToArray();
world.RemoveBody(a);
for (var k = 0; k < onA.Length; k++)
{
Assert.Equal(0u, view.WetLive[onA[k]]);
Assert.Equal(generationsA[k] + 1, view.WetGeneration[onA[k]]);
}
Assert.Equal(0f, view.WetVolume[a.Lane]);
Assert.Equal(volumeB, view.WetVolume[b.Lane]); // the other body's patches are untouched
for (var k = 0; k < onB.Length; k++)
{
Assert.Equal(1u, view.WetLive[onB[k]]);
Assert.Equal(generationsB[k], view.WetGeneration[onB[k]]);
}
}
[Fact]
public void Lanes_survive_the_removal_of_others()
{
using var world = World(bodies: 8, joints: 0);
var h = Enumerable.Range(0, 6).Select(i => world.AddSphere((ulong)(i + 1), (i * 0.5f, 5f + i, 0f), 0.1f, 1000f, velocity: (0.1f * i, 0f, 0f))).ToArray();
Run(world, 10);
var view = world.View;
var keep = new[] { 0, 1, 2, 4, 5 };
var posBefore = keep.Select(i => (view.PositionX[h[i]], view.PositionY[h[i]], view.VelocityX[h[i]])).ToArray();
world.RemoveBody(h[3]);
foreach (var i in keep)
{
Assert.Equal((uint)Box3DSoaKind.Sphere, view.Kind[h[i]]);
Assert.Equal((ulong)(i + 1), view.StableId[h[i]]);
Assert.Equal(h[i].Generation, view.BodyGeneration[h[i]]);
}
Assert.Equal(posBefore, keep.Select(i => (view.PositionX[h[i]], view.PositionY[h[i]], view.VelocityX[h[i]])).ToArray()); // continuity: nothing moved
Run(world, 5);
Assert.Equal(6, view.Count); // the high-water mark
Assert.All(keep, i => Assert.True(view.PositionY[h[i]] < 5f + i, "the others keep falling"));
}
[Fact]
public void A_removed_lane_is_reused_one_step_later_with_a_new_generation()
{
using var world = World(bodies: 3, joints: 0);
var a = world.AddSphere(1, (0f, 5f, 0f), 0.1f, 1000f);
var b = world.AddSphere(2, (1f, 5f, 0f), 0.1f, 1000f);
var c = world.AddSphere(3, (2f, 5f, 0f), 0.1f, 1000f);
Assert.Equal(0, world.FreeBodySlots);
Assert.Throws<InvalidOperationException>(() => world.AddSphere(4, (3f, 5f, 0f), 0.1f, 1000f)); // full
world.RemoveBody(b);
Assert.Equal(0, world.FreeBodySlots); // not within the removing step
Assert.Throws<InvalidOperationException>(() => world.AddSphere(4, (3f, 5f, 0f), 0.1f, 1000f));
world.Step(Dt);
Assert.Equal(1, world.FreeBodySlots);
var d = world.AddSphere(2, (3f, 5f, 0f), 0.1f, 1000f); // the removed stable id may be reused
Assert.Equal(b.Lane, d.Lane);
Assert.Equal(b.Generation + 1, d.Generation);
var view = world.View;
Assert.Equal((uint)Box3DSoaKind.Sphere, view.Kind[d]);
Assert.Equal(3f, view.PositionX[d], 1e-5f);
Assert.Equal(3, view.Count);
Assert.Throws<InvalidOperationException>(() => world.RemoveBody(b)); // the old handle is stale against the new occupant
Assert.Equal((uint)Box3DSoaKind.Sphere, view.Kind[d]);
Assert.Throws<InvalidOperationException>(() => world.AddSphere(2, (4f, 5f, 0f), 0.1f, 1000f)); // a live stable id still collides (and the world is full)
world.RemoveBody(a);
world.Step(Dt);
Assert.Equal(a.Lane, world.AddSphere(7, (5f, 5f, 0f), 0.1f, 1000f).Lane); // lowest free lane first
_ = c;
}
private static byte[] Replay()
{
using var world = World(bodies: 8, joints: 4);
world.AddStaticBox(1, (0f, -0.5f, 0f), (4f, 0.5f, 4f));
var a = world.AddCapsule(2, (0f, 1f, 0f), 0.1f, 0.3f, 800f);
var b = world.AddSphere(3, (0.3f, 2f, 0f), 0.1f, 800f);
var c = world.AddBox(4, (-0.3f, 3f, 0f), (0.1f, 0.1f, 0.1f), 800f);
var j1 = world.AddJoint(Box3DSoaJointKind.Spherical, a, b, (0.1f, 1.5f, 0f), (0f, 0f, 1f), forceThreshold: 50f);
var j2 = world.AddJoint(Box3DSoaJointKind.Weld, a, c, (-0.1f, 1.5f, 0f), (0f, 0f, 1f));
var view = world.View;
var o = new MemoryStream();
void B<T>(ReadOnlySpan<T> lane) where T : unmanaged => o.Write(System.Runtime.InteropServices.MemoryMarshal.AsBytes(lane));
for (var i = 0; i < 300; i++)
{
if (i == 60) world.RemoveBody(b);
if (i == 120) world.SetJointThresholds(j2, 5f, float.PositiveInfinity);
world.Step(Dt);
B(view.PositionX); B(view.PositionY); B(view.RotationW); B(view.VelocityY); B(view.Kind); B(view.BodyGeneration); B(view.StableId);
B(view.JointKind); B(view.JointGeneration); B(view.JointConstraintForceY); B(view.JointConstraintTorqueZ);
B(world.Facts.Overloads);
}
Assert.Equal((uint)Box3DSoaJointKind.Removed, view.JointKind[j1]);
return o.ToArray();
}
[Fact]
public void A_world_with_removals_and_thresholds_repeats_bit_for_bit()
{
var first = Replay();
Assert.True(first.AsSpan().SequenceEqual(Replay()));
Assert.Contains(first, x => x != 0);
}
[Fact]
public void Removing_a_body_and_the_step_after_allocate_nothing_managed()
{
using var world = World(bodies: 8, joints: 2);
var a = world.AddSphere(1, (0f, 5f, 0f), 0.1f, 1000f);
var b = world.AddSphere(2, (1f, 5f, 0f), 0.1f, 1000f);
world.AddJoint(Box3DSoaJointKind.Weld, a, b, (0.5f, 5f, 0f), (0f, 0f, 1f));
Run(world, 5);
var before = GC.GetAllocatedBytesForCurrentThread();
world.RemoveBody(a);
world.Step(Dt);
Assert.Equal(0L, GC.GetAllocatedBytesForCurrentThread() - before);
}
}

View file

@ -25,7 +25,7 @@ public sealed partial class Box3DSoaFluidTests
world.CreateParticles([Fluid((patchAt.X, DropRadius, patchAt.Z), FluidMass * scale * scale * scale, DropRadius, material: 1u)]);
for (var tick = 0; tick < 20; tick++) world.Step(Dt, Sub);
if (patch) world.Settle([Handle(world, 0)]); else world.DestroyParticles([Handle(world, 0)]);
var lanes = boxes.Select((at, i) => world.AddBox(10ul + (ulong)i, (at.X, 0.1f, at.Z), (0.1f, 0.1f, 0.1f), BoxMass / 0.008f, friction: 0.6f)).ToArray();
var lanes = boxes.Select((at, i) => (int)world.AddBox(10ul + (ulong)i, (at.X, 0.1f, at.Z), (0.1f, 0.1f, 0.1f), BoxMass / 0.008f, friction: 0.6f)).ToArray();
return (world, lanes);
}

View file

@ -335,7 +335,7 @@ public sealed partial class Box3DSoaFluidTests
public void Remove_wet_never_reaches_the_topology_derivation()
{
// Rederiving an unchanged graph writes the same bytes, so no lane tells the call from its absence; the rule is pinned on the source (the authority map: remove(WET)
// ends in rederive_wet, never in rederive_topology; rederive_wet's callers are settle, remove(WET) and destroy).
// ends in rederive_wet, never in rederive_topology; rederive_wet's callers are settle, remove(WET), release_body and destroy).
var source = NativeSource("ymir_particles.c").Replace("\r\n", "\n");
var at = source.IndexOf("if (type == YMIR_LANES_WET)", StringComparison.Ordinal);
Assert.True(at > 0);
@ -344,7 +344,7 @@ public sealed partial class Box3DSoaFluidTests
Assert.Contains("rederive_wet(p);", branch);
Assert.DoesNotContain("rederive_topology", branch);
Assert.DoesNotContain("TOPO_", branch);
Assert.Equal(3, System.Text.RegularExpressions.Regex.Matches(source, @"[^_]rederive_wet\(p\);").Count); // settle, remove(WET), and destroy when a destroyed solid takes its patches (particle_wet_mu goes dry again)
Assert.Equal(4, System.Text.RegularExpressions.Regex.Matches(source, @"[^_]rederive_wet\(p\);").Count); // settle, remove(WET), release_body (a removed body takes its patches), and destroy when a destroyed solid takes its patches (particle_wet_mu goes dry again)
}
// ---- determinism ----------------------------------------------------------------------------------------