From 32578bd1092b96622bbf98cca5899e855a88610d Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Wed, 25 Nov 2026 21:00:00 +0100 Subject: [PATCH] Ymir SoA: one joint owner for every kind, ABI 16 ymir_soa_retire_joint is the only b3DestroyJoint; destroy_joint takes any live joint kind with the slot's generation. The motor-only gate is gone. Lane state is the tombstone rule: kind REMOVED, bodies -1, every lane 0, generation + 1. Co-Authored-By: Claude Sonnet 5.5 --- native/Ymir.Box3D/ymir_box3d_soa.c | 33 +++++++++++-------- src/Ymir.Box3D/Box3DSoaWorld.cs | 12 +++---- .../Ymir.Box3D.Soa.Tests/Box3DSoaGrabTests.cs | 30 ++++++++++------- 3 files changed, 45 insertions(+), 30 deletions(-) diff --git a/native/Ymir.Box3D/ymir_box3d_soa.c b/native/Ymir.Box3D/ymir_box3d_soa.c index b5bd42d..0c6d25f 100644 --- a/native/Ymir.Box3D/ymir_box3d_soa.c +++ b/native/Ymir.Box3D/ymir_box3d_soa.c @@ -55,10 +55,10 @@ enum YMIR_SOA_JOINT_SPHERICAL = 1, YMIR_SOA_JOINT_WELD = 2, YMIR_SOA_JOINT_MOTOR = 3, - YMIR_SOA_JOINT_REMOVED = 4 // a destroyed joint's lane: never accepted by add_joint + YMIR_SOA_JOINT_REMOVED = 4 // a retired joint's lane (destroy_joint): never accepted by add_joint }; -#define YMIR_SOA_ABI_VERSION 15u +#define YMIR_SOA_ABI_VERSION 16u // 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 @@ -92,7 +92,7 @@ typedef struct ymir_box3d_soa_body_desc // frame origin onto body A's (so B's anchor point onto A's), the angular spring pulls B's frame orientation onto // A's. `*_hertz` 0 turns that spring off; `max_force` (N) and `max_torque` (N m) bound what it may apply, so a // weak grab hangs below its target. All six are finite and >= 0 and are ignored by the other kinds. -// `ymir_box3d_soa_destroy_joint` releases a motor joint: its lane becomes a tombstone (kind REMOVED = 4, bodies -1, +// `ymir_box3d_soa_destroy_joint` releases a joint of any kind: its lane becomes a tombstone (kind REMOVED = 4, bodies -1, // every lane 0) that add_joint may reuse only once a step has completed since. typedef struct ymir_box3d_soa_joint_desc { @@ -980,17 +980,11 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_add_joint( return YMIR_SOA_OK; } -// Releases a motor joint (a grab) named by its lane and the slot's generation read when it was added. Only motor -// joints: general joint removal is another cut's. A generation that is not the slot's current one is refused, so -// a stale handle never destroys a later joint in a reused slot. The lane becomes a tombstone, kind REMOVED, -// bodies -1 and every lane 0, until a later step lets add_joint reuse it. -YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_destroy_joint(ymir_box3d_soa_world* world, int32_t joint, uint32_t generation) +// The one path that ends a joint: Box3D destroys it (waking its bodies), the lane becomes a tombstone (kind +// REMOVED, bodies -1, every lane 0) and its generation moves on, so no handle names it again. add_joint may +// reuse the lane only once a step has completed since. +static void ymir_soa_retire_joint(ymir_box3d_soa_world* world, int32_t joint) { - if (world == NULL || joint < 0 || joint >= world->joint_count || world->joint_kind[joint] != YMIR_SOA_JOINT_MOTOR || - world->joint_generation[joint] != generation) - { - return YMIR_SOA_INVALID_ARGUMENT; - } b3DestroyJoint(world->joints[joint], true); world->joints[joint] = (b3JointId){0}; world->joint_kind[joint] = YMIR_SOA_JOINT_REMOVED; @@ -1002,6 +996,19 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_destroy_joint(ymir_box3d_soa_world* { ymir_soa_joint_lane(world, lane)[joint] = 0.0f; } +} + +// Releases a joint of any kind (a grab, a cut, a tear) named by its lane and the slot's generation read when it +// was added. A generation that is not the slot's current one, or a removed lane, is refused, so a stale handle never +// destroys a later joint in a reused slot. +YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_destroy_joint(ymir_box3d_soa_world* world, int32_t joint, uint32_t generation) +{ + if (world == NULL || joint < 0 || joint >= world->joint_count || world->joint_kind[joint] > YMIR_SOA_JOINT_MOTOR || + world->joint_generation[joint] != generation) + { + return YMIR_SOA_INVALID_ARGUMENT; + } + ymir_soa_retire_joint(world, joint); return YMIR_SOA_OK; } diff --git a/src/Ymir.Box3D/Box3DSoaWorld.cs b/src/Ymir.Box3D/Box3DSoaWorld.cs index 8d61016..4259b89 100644 --- a/src/Ymir.Box3D/Box3DSoaWorld.cs +++ b/src/Ymir.Box3D/Box3DSoaWorld.cs @@ -20,7 +20,7 @@ public enum Box3DSoaJointKind : uint Weld = 2, /// Position springs pulling body B's frame onto body A's, bounded by a maximum force and torque. Motor = 3, - /// The kind lane of a destroyed joint (a tombstone): bodies read -1 and every other lane 0. + /// The kind lane of a retired joint (a tombstone, from DestroyJoint): bodies read -1 and every other lane 0. Removed = 4 } @@ -850,7 +850,7 @@ public readonly unsafe struct Box3DSoaView /// public sealed class Box3DSoaWorld : IDisposable { - private const uint ExpectedAbi = 15; + private const uint ExpectedAbi = 16; private nint _handle; /// @@ -1009,11 +1009,11 @@ public sealed class Box3DSoaWorld : IDisposable } /// - /// Releases a motor joint (a grab); the anchor and the grabbed body stay. The joint's lane becomes a tombstone: + /// Releases a joint of any kind (a grab, a cut, a tear); the bodies stay. The joint's lane becomes a tombstone: /// kind , bodies -1, every other lane 0. It is reused by a later AddJoint only - /// once a has completed, so lanes read this step stay valid. Throws for any other kind of joint - /// (general removal is another cut's), a lane that is not a live joint, or a handle whose generation is not the slot's: - /// a release made with a stale handle never destroys a later grab in a reused slot. + /// once a has completed, so lanes read this step stay valid. Throws for a lane that is not a live joint + /// or a handle whose generation is not the slot's: a release made with a stale handle never destroys a later joint in a reused slot. + /// The lanes keep no last state; read what you need before removing. /// public void DestroyJoint(Box3DSoaJointHandle joint) { diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaGrabTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaGrabTests.cs index 786e933..14d204d 100644 --- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaGrabTests.cs +++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaGrabTests.cs @@ -167,14 +167,14 @@ public sealed class Box3DSoaGrabTests [Fact] public void The_managed_side_refuses_a_native_library_of_another_abi_in_either_direction() { - Assert.Equal(15u, Box3DSoaNative.ymir_box3d_soa_get_abi_version()); - Box3DSoaWorld.RequireNativeAbi(15); - Assert.Throws(() => Box3DSoaWorld.RequireNativeAbi(14)); // an older native under this managed build - Assert.Throws(() => Box3DSoaWorld.RequireNativeAbi(16)); // a newer one + Assert.Equal(16u, Box3DSoaNative.ymir_box3d_soa_get_abi_version()); + Box3DSoaWorld.RequireNativeAbi(16); + Assert.Throws(() => Box3DSoaWorld.RequireNativeAbi(15)); // an older native under this managed build + Assert.Throws(() => Box3DSoaWorld.RequireNativeAbi(17)); // a newer one } [Fact] - public void Only_a_live_motor_joint_can_be_destroyed() + public void Every_joint_kind_destroys_through_one_path() { using var world = new Box3DSoaWorld(capacity: 4, gravityY: 0f, jointCapacity: 3); var pin = world.AddStaticBox(1, (0f, 5f, 0f), (0.1f, 0.1f, 0.1f)); @@ -183,16 +183,24 @@ public sealed class Box3DSoaGrabTests var hinge = world.AddJoint(Box3DSoaJointKind.Revolute, pin, sphere, (0f, 5f, 0f), (0f, 0f, 1f)); var weld = world.AddJoint(Box3DSoaJointKind.Weld, anchor, sphere, (1f, 5f, 0f), (0f, 0f, 1f)); var motor = world.AddMotorJoint(anchor, sphere, (1f, 5f, 0f), (5f, 0.7f, 100f), (0f, 0f, 0f)); - Assert.Throws(() => world.DestroyJoint(hinge)); - Assert.Throws(() => world.DestroyJoint(weld)); Assert.Throws(() => world.DestroyJoint(new Box3DSoaJointHandle(-1, 0))); Assert.Throws(() => world.DestroyJoint(new Box3DSoaJointHandle(3, 0))); + Assert.Throws(() => world.DestroyJoint(new Box3DSoaJointHandle(hinge.Lane, hinge.Generation + 1))); // stale var view = world.View; Assert.Equal((uint)Box3DSoaJointKind.Revolute, view.JointKind[hinge]); - world.DestroyJoint(motor); - Assert.Throws(() => world.DestroyJoint(motor)); // already gone - world.Step(Dt); // the live joints still step - Assert.Equal((uint)Box3DSoaJointKind.Weld, view.JointKind[weld]); + foreach (var joint in new[] { hinge, weld, motor }) + { + world.DestroyJoint(joint); + Assert.Equal((uint)Box3DSoaJointKind.Removed, view.JointKind[joint]); + Assert.Equal(-1, view.JointBodyA[joint]); + Assert.Equal(-1, view.JointBodyB[joint]); + Assert.Equal(joint.Generation + 1, view.JointGeneration[joint]); + Assert.Equal(0f, view.JointAngle[joint]); + Assert.Equal(0f, view.JointOriginX[joint]); + Assert.Throws(() => world.DestroyJoint(joint)); // already gone + } + world.Step(Dt); + Assert.Equal((uint)Box3DSoaJointKind.Removed, view.JointKind[weld]); } [Fact]