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 <noreply@anthropic.com>
This commit is contained in:
parent
da5ef233a5
commit
32578bd109
3 changed files with 45 additions and 30 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ public enum Box3DSoaJointKind : uint
|
|||
Weld = 2,
|
||||
/// <summary>Position springs pulling body B's frame onto body A's, bounded by a maximum force and torque.</summary>
|
||||
Motor = 3,
|
||||
/// <summary>The kind lane of a destroyed joint (a tombstone): bodies read -1 and every other lane 0.</summary>
|
||||
/// <summary>The kind lane of a retired joint (a tombstone, from DestroyJoint): bodies read -1 and every other lane 0.</summary>
|
||||
Removed = 4
|
||||
}
|
||||
|
||||
|
|
@ -850,7 +850,7 @@ public readonly unsafe struct Box3DSoaView
|
|||
/// </summary>
|
||||
public sealed class Box3DSoaWorld : IDisposable
|
||||
{
|
||||
private const uint ExpectedAbi = 15;
|
||||
private const uint ExpectedAbi = 16;
|
||||
private nint _handle;
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -1009,11 +1009,11 @@ public sealed class Box3DSoaWorld : IDisposable
|
|||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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 <see cref="Box3DSoaJointKind.Removed"/>, bodies -1, every other lane 0. It is reused by a later AddJoint only
|
||||
/// once a <see cref="Step"/> 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 <see cref="Step"/> 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.
|
||||
/// </summary>
|
||||
public void DestroyJoint(Box3DSoaJointHandle joint)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(14)); // an older native under this managed build
|
||||
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(16)); // a newer one
|
||||
Assert.Equal(16u, Box3DSoaNative.ymir_box3d_soa_get_abi_version());
|
||||
Box3DSoaWorld.RequireNativeAbi(16);
|
||||
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(15)); // an older native under this managed build
|
||||
Assert.Throws<InvalidOperationException>(() => 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<InvalidOperationException>(() => world.DestroyJoint(hinge));
|
||||
Assert.Throws<InvalidOperationException>(() => world.DestroyJoint(weld));
|
||||
Assert.Throws<InvalidOperationException>(() => world.DestroyJoint(new Box3DSoaJointHandle(-1, 0)));
|
||||
Assert.Throws<InvalidOperationException>(() => world.DestroyJoint(new Box3DSoaJointHandle(3, 0)));
|
||||
Assert.Throws<InvalidOperationException>(() => 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<InvalidOperationException>(() => 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<InvalidOperationException>(() => world.DestroyJoint(joint)); // already gone
|
||||
}
|
||||
world.Step(Dt);
|
||||
Assert.Equal((uint)Box3DSoaJointKind.Removed, view.JointKind[weld]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
Loading…
Reference in a new issue