SoA joint origin lanes: world position of the joint frame origin on body A, ABI 8
The view published each joint's frame orientation but not its origin; a client cannot infer the anchor from geometry. joint_origin_x/y/z (lanes 12-14, appended) are body A's transform applied to the frame origin kept at add_joint, refreshed every step. Destroyed joints read 0 through the existing tombstone clear. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
be21de1959
commit
037c9da9c2
5 changed files with 70 additions and 19 deletions
|
|
@ -8,11 +8,11 @@ Box3D decides every physical outcome; Ymir creates bodies, steps the world and e
|
|||
`ymir_box3d_session_*` (`ymir_box3d.c`) is the planar Aetheria session: gravity is zero, Y is locked,
|
||||
only yaw rotates (`motionLocks.linearY/angularX/angularZ`), and state leaves by `copy_bodies` into an
|
||||
array of structs. A body cannot fall or tumble there, and every read is a copy. The SoA world is a
|
||||
separate native object (`ymir_box3d_soa.c`, ABI 7) so the Aetheria session's contract is untouched.
|
||||
separate native object (`ymir_box3d_soa.c`, ABI 8) so the Aetheria session's contract is untouched.
|
||||
|
||||
## Contract
|
||||
|
||||
- Native (ABI 7): `ymir_box3d_soa_create(capacity, joint_capacity, gravity, fact_capacity, hit_threshold) /
|
||||
- Native (ABI 8): `ymir_box3d_soa_create(capacity, joint_capacity, gravity, fact_capacity, hit_threshold) /
|
||||
add_body / add_joint / step / get_view / get_facts / set_anchor_target / destroy_joint / free_joint_slots / apply_impulse / cast_ray_closest / destroy`. `add_body` takes one descriptor (sphere, capsule, box, static box, anchor; position,
|
||||
rotation, velocity, size, density, friction, restitution, rolling resistance, material id, parent, limb). Capacities are fixed at creation,
|
||||
so lane pointers never move.
|
||||
|
|
@ -29,7 +29,7 @@ separate native object (`ymir_box3d_soa.c`, ABI 7) so the Aetheria session's con
|
|||
(revolute, spherical, weld). The joint frame has its z axis on the axis (the hinge axis); the creation
|
||||
pose is the joint's zero. Optional limits: revolute angle, spherical swing cone and twist. Joint lanes:
|
||||
`joint_kind`, `joint_body_a/b`, `joint_angle` (Box3D's revolute angle or spherical twist), `joint_frame`
|
||||
(world orientation of frame A, quaternion), `joint_rotation` (frame B relative to frame A, quaternion,
|
||||
(world orientation of frame A, quaternion), `joint_origin` (world position of frame A's origin, the anchor), `joint_rotation` (frame B relative to frame A, quaternion,
|
||||
w >= 0) and `joint_angular_velocity` (B minus A, in frame A). A consumer can compute a joint torque from
|
||||
these and write it as equal and opposite torques on the two body lanes.
|
||||
- Contact lanes (read-only, rewritten by every `step`): `contact_impulse` (N s: the normal impulse the
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ enum
|
|||
YMIR_SOA_JOINT_REMOVED = 4 // a destroyed joint's lane: never accepted by add_joint
|
||||
};
|
||||
|
||||
#define YMIR_SOA_ABI_VERSION 7u
|
||||
#define YMIR_SOA_ABI_VERSION 8u
|
||||
|
||||
// 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
|
||||
|
|
@ -168,6 +168,10 @@ typedef struct ymir_box3d_soa_view
|
|||
const float* joint_angular_velocity_x;
|
||||
const float* joint_angular_velocity_y;
|
||||
const float* joint_angular_velocity_z;
|
||||
// World position of the joint frame origin on body A (the joint anchor), refreshed every step like joint_frame.
|
||||
const float* joint_origin_x;
|
||||
const float* joint_origin_y;
|
||||
const float* joint_origin_z;
|
||||
// Contact lanes, read-only, refreshed every step. contact_impulse is N s: the sum over the body's touching
|
||||
// contact manifold points of stepNormalImpulse, the normal impulse the contacts delivered to the body over
|
||||
// the whole step (0 for a body touching nothing). Exact, normal channel only: Box3D exposes no tangent
|
||||
|
|
@ -242,7 +246,7 @@ typedef struct ymir_box3d_soa_ray_hit
|
|||
#define YMIR_SOA_WRITE_LANES 6
|
||||
#define YMIR_SOA_CONTACT_IMPULSE_LANE (YMIR_SOA_STATE_LANES + YMIR_SOA_WRITE_LANES)
|
||||
#define YMIR_SOA_BODY_FLOAT_LANES (YMIR_SOA_CONTACT_IMPULSE_LANE + 1)
|
||||
#define YMIR_SOA_JOINT_FLOAT_LANES 12
|
||||
#define YMIR_SOA_JOINT_FLOAT_LANES 15
|
||||
|
||||
typedef struct ymir_box3d_soa_world
|
||||
{
|
||||
|
|
@ -268,6 +272,7 @@ typedef struct ymir_box3d_soa_world
|
|||
int32_t* joint_body_a;
|
||||
int32_t* joint_body_b;
|
||||
b3Quat* joint_local_a; // joint frame in body A, kept so the state is read without asking Box3D
|
||||
b3Vec3* joint_local_origin_a; // the frame origin in body A, kept for the same reason
|
||||
b3Quat* joint_local_b;
|
||||
float* joint_lanes; // YMIR_SOA_JOINT_FLOAT_LANES arrays of `joint_capacity` floats, lane-major
|
||||
uint32_t fact_capacity;
|
||||
|
|
@ -323,6 +328,13 @@ static void ymir_soa_refresh(ymir_box3d_soa_world* world, int index)
|
|||
}
|
||||
}
|
||||
|
||||
static b3Transform ymir_soa_body_transform(b3BodyId body)
|
||||
{
|
||||
b3Pos p = b3Body_GetPosition(body);
|
||||
b3Transform t = {{p.x, p.y, p.z}, b3Body_GetRotation(body)};
|
||||
return t;
|
||||
}
|
||||
|
||||
// Joint state from the bodies: the world orientation of frame A, frame B relative to frame A (as a
|
||||
// quaternion with w >= 0, the shorter arc) and the relative angular velocity in frame A. The angle
|
||||
// lane is Box3D's own: the revolute angle or the spherical twist angle, zero for a weld.
|
||||
|
|
@ -331,6 +343,7 @@ static void ymir_soa_refresh_joint(ymir_box3d_soa_world* world, int j)
|
|||
int a = world->joint_body_a[j];
|
||||
int b = world->joint_body_b[j];
|
||||
b3Quat qa = b3MulQuat(b3Body_GetRotation(world->bodies[a]), world->joint_local_a[j]);
|
||||
b3Vec3 origin = b3TransformPoint(ymir_soa_body_transform(world->bodies[a]), world->joint_local_origin_a[j]);
|
||||
b3Quat qb = b3MulQuat(b3Body_GetRotation(world->bodies[b]), world->joint_local_b[j]);
|
||||
b3Quat rel = b3InvMulQuat(qa, qb);
|
||||
if (rel.s < 0.0f)
|
||||
|
|
@ -349,7 +362,7 @@ static void ymir_soa_refresh_joint(ymir_box3d_soa_world* world, int j)
|
|||
angle = b3SphericalJoint_GetTwistAngle(world->joints[j]);
|
||||
}
|
||||
const float values[YMIR_SOA_JOINT_FLOAT_LANES] = {
|
||||
angle, qa.v.x, qa.v.y, qa.v.z, qa.s, rel.v.x, rel.v.y, rel.v.z, rel.s, w.x, w.y, w.z};
|
||||
angle, qa.v.x, qa.v.y, qa.v.z, qa.s, rel.v.x, rel.v.y, rel.v.z, rel.s, w.x, w.y, w.z, origin.x, origin.y, origin.z};
|
||||
for (int lane = 0; lane < YMIR_SOA_JOINT_FLOAT_LANES; ++lane)
|
||||
{
|
||||
ymir_soa_joint_lane(world, lane)[j] = values[lane];
|
||||
|
|
@ -414,6 +427,7 @@ static void ymir_soa_free(ymir_box3d_soa_world* world)
|
|||
free(world->joint_body_a);
|
||||
free(world->joint_body_b);
|
||||
free(world->joint_local_a);
|
||||
free(world->joint_local_origin_a);
|
||||
free(world->joint_local_b);
|
||||
free(world->joint_lanes);
|
||||
free(world->begin_facts);
|
||||
|
|
@ -464,6 +478,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create(
|
|||
world->joint_body_a = (int32_t*)calloc(m, sizeof(int32_t));
|
||||
world->joint_body_b = (int32_t*)calloc(m, sizeof(int32_t));
|
||||
world->joint_local_a = (b3Quat*)calloc(m, sizeof(b3Quat));
|
||||
world->joint_local_origin_a = (b3Vec3*)calloc(m, sizeof(b3Vec3));
|
||||
world->joint_local_b = (b3Quat*)calloc(m, sizeof(b3Quat));
|
||||
world->joint_lanes = (float*)calloc(m * YMIR_SOA_JOINT_FLOAT_LANES, sizeof(float));
|
||||
world->fact_capacity = (uint32_t)fact_capacity;
|
||||
|
|
@ -475,7 +490,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create(
|
|||
world->end_facts == NULL || world->hit_facts == NULL || world->contact_scratch == NULL ||
|
||||
world->bodies == NULL || world->stable_id == NULL || world->kind == NULL || world->parent == NULL ||
|
||||
world->limb == NULL || world->size == NULL || world->lanes == NULL || world->joints == NULL || world->joint_kind == NULL ||
|
||||
world->joint_body_a == NULL || world->joint_body_b == NULL || world->joint_local_a == NULL ||
|
||||
world->joint_body_a == NULL || world->joint_body_b == NULL || world->joint_local_a == NULL || world->joint_local_origin_a == NULL ||
|
||||
world->joint_local_b == NULL || world->joint_lanes == NULL || world->joint_removed_at == NULL ||
|
||||
world->joint_generation == NULL)
|
||||
{
|
||||
|
|
@ -666,13 +681,6 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_add_body(
|
|||
return YMIR_SOA_OK;
|
||||
}
|
||||
|
||||
static b3Transform ymir_soa_body_transform(b3BodyId body)
|
||||
{
|
||||
b3Pos p = b3Body_GetPosition(body);
|
||||
b3Transform t = {{p.x, p.y, p.z}, b3Body_GetRotation(body)};
|
||||
return t;
|
||||
}
|
||||
|
||||
// The lane a new joint takes: the lowest tombstone whose destroying step has completed, else the next fresh
|
||||
// slot, else -1. A destroyed lane is not reused within the step that destroyed it, so lanes a caller read
|
||||
// that step stay meaningful.
|
||||
|
|
@ -802,6 +810,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_add_joint(
|
|||
world->joint_body_a[j] = d->body_a;
|
||||
world->joint_body_b[j] = d->body_b;
|
||||
world->joint_local_a[j] = local_a.q;
|
||||
world->joint_local_origin_a[j] = local_a.p;
|
||||
world->joint_local_b[j] = local_b.q;
|
||||
ymir_soa_refresh_joint(world, j);
|
||||
if (index != NULL)
|
||||
|
|
@ -1163,6 +1172,9 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_get_view(const ymir_box3d_soa_world
|
|||
.joint_angular_velocity_x = jb + 9 * m,
|
||||
.joint_angular_velocity_y = jb + 10 * m,
|
||||
.joint_angular_velocity_z = jb + 11 * m,
|
||||
.joint_origin_x = jb + 12 * m,
|
||||
.joint_origin_y = jb + 13 * m,
|
||||
.joint_origin_z = jb + 14 * m,
|
||||
.contact_impulse = base + YMIR_SOA_CONTACT_IMPULSE_LANE * n,
|
||||
.contact_count = world->contact_count,
|
||||
.material_id = world->material_id,
|
||||
|
|
|
|||
|
|
@ -132,6 +132,9 @@ internal unsafe struct Box3DSoaViewNative
|
|||
public float* JointAngularVelocityX;
|
||||
public float* JointAngularVelocityY;
|
||||
public float* JointAngularVelocityZ;
|
||||
public float* JointOriginX;
|
||||
public float* JointOriginY;
|
||||
public float* JointOriginZ;
|
||||
public float* ContactImpulse;
|
||||
public int* ContactCount;
|
||||
public ulong* MaterialId;
|
||||
|
|
@ -404,6 +407,10 @@ public readonly unsafe struct Box3DSoaView
|
|||
public ReadOnlySpan<float> JointAngularVelocityX => Lane(_v.JointAngularVelocityX, JointCount);
|
||||
public ReadOnlySpan<float> JointAngularVelocityY => Lane(_v.JointAngularVelocityY, JointCount);
|
||||
public ReadOnlySpan<float> JointAngularVelocityZ => Lane(_v.JointAngularVelocityZ, JointCount);
|
||||
/// <summary>World position of the joint frame origin on body A (the joint anchor); at creation it is the anchor add_joint was given.</summary>
|
||||
public ReadOnlySpan<float> JointOriginX => Lane(_v.JointOriginX, JointCount);
|
||||
public ReadOnlySpan<float> JointOriginY => Lane(_v.JointOriginY, JointCount);
|
||||
public ReadOnlySpan<float> JointOriginZ => Lane(_v.JointOriginZ, JointCount);
|
||||
|
||||
/// <summary>Address of a lane, for tests and interop that need to prove nothing was copied.</summary>
|
||||
public nint PositionYAddress => (nint)_v.PositionY;
|
||||
|
|
@ -417,7 +424,7 @@ public readonly unsafe struct Box3DSoaView
|
|||
/// </summary>
|
||||
public sealed class Box3DSoaWorld : IDisposable
|
||||
{
|
||||
private const uint ExpectedAbi = 7;
|
||||
private const uint ExpectedAbi = 8;
|
||||
private nint _handle;
|
||||
|
||||
/// <summary>
|
||||
|
|
|
|||
|
|
@ -95,12 +95,14 @@ public sealed class Box3DSoaGrabTests
|
|||
var joint = world.AddMotorJoint(anchor, grabbed, at, (5f, 0.7f, strong), (0f, 0f, 0f));
|
||||
Assert.Equal(0, joint);
|
||||
world.Step(Dt);
|
||||
Assert.Equal(at.Item2, world.View.JointOriginY[joint], 0.05f); // live, the origin is the grab point
|
||||
world.DestroyJoint(joint);
|
||||
var jv = world.View; // joint count is as of when the view was taken
|
||||
// A destroyed lane is a tombstone this step and is not reused within it.
|
||||
Assert.Equal((uint)Box3DSoaJointKind.Removed, jv.JointKind[joint]);
|
||||
Assert.Equal((-1, -1), (jv.JointBodyA[joint], jv.JointBodyB[joint]));
|
||||
Assert.Equal(0f, jv.JointFrameW[joint]);
|
||||
Assert.Equal((0f, 0f, 0f), (jv.JointOriginX[joint], jv.JointOriginY[joint], jv.JointOriginZ[joint]));
|
||||
Assert.Equal(0, world.FreeJointSlots);
|
||||
Assert.Throws<InvalidOperationException>(() => world.AddMotorJoint(anchor, grabbed, at, (5f, 0.7f, strong), (0f, 0f, 0f)));
|
||||
world.Step(Dt);
|
||||
|
|
@ -165,10 +167,10 @@ public sealed class Box3DSoaGrabTests
|
|||
[Fact]
|
||||
public void The_managed_side_refuses_a_native_library_of_another_abi_in_either_direction()
|
||||
{
|
||||
Assert.Equal(7u, Box3DSoaNative.ymir_box3d_soa_get_abi_version());
|
||||
Box3DSoaWorld.RequireNativeAbi(7);
|
||||
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(6)); // an older native under this managed build
|
||||
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(8)); // a newer one
|
||||
Assert.Equal(8u, Box3DSoaNative.ymir_box3d_soa_get_abi_version());
|
||||
Box3DSoaWorld.RequireNativeAbi(8);
|
||||
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(7)); // an older native under this managed build
|
||||
Assert.Throws<InvalidOperationException>(() => Box3DSoaWorld.RequireNativeAbi(9)); // a newer one
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
|
|
@ -59,6 +59,36 @@ public sealed class Box3DSoaJointTests
|
|||
Assert.Equal(-1, view.Parent[anchor]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Joint_origin_lane_equals_the_anchor_at_creation_and_follows_body_a()
|
||||
{
|
||||
// Two dynamic bodies, no gravity, joined off-centre: A at the origin, anchor 0.5 m out along X and 0.25 m up.
|
||||
using var world = new Box3DSoaWorld(capacity: 2, gravityY: 0f, jointCapacity: 1);
|
||||
var a = world.AddBox(1, (0f, 0f, 0f), (0.2f, 0.2f, 0.2f), 1f);
|
||||
var b = world.AddBox(2, (1f, 0.25f, 0f), (0.2f, 0.2f, 0.2f), 1f, parent: a);
|
||||
var anchor = (0.5f, 0.25f, 0f);
|
||||
var j = world.AddJoint(Box3DSoaJointKind.Spherical, a, b, anchor, (0f, 0f, 1f));
|
||||
var v = world.View;
|
||||
Assert.Equal(anchor.Item1, v.JointOriginX[j], 1e-5f);
|
||||
Assert.Equal(anchor.Item2, v.JointOriginY[j], 1e-5f);
|
||||
Assert.Equal(0f, v.JointOriginZ[j], 1e-5f);
|
||||
|
||||
// Kick body A off-centre so it both moves and turns; the lane is A's pose applied to the creation offset.
|
||||
world.ApplyImpulse(a, (0f, 1f, 0f), (-0.1f, 0f, 0f));
|
||||
for (var i = 0; i < 60; i++) world.Step(Dt);
|
||||
var (qx, qy, qz, qw) = (v.RotationX[a], v.RotationY[a], v.RotationZ[a], v.RotationW[a]);
|
||||
var (ox, oy, oz) = anchor; // body A started at the origin, unrotated: the creation offset is the anchor
|
||||
var (tx, ty, tz) = (2f * (qy * oz - qz * oy), 2f * (qz * ox - qx * oz), 2f * (qx * oy - qy * ox)); // 2 (q.v x o)
|
||||
var expectedX = v.PositionX[a] + ox + qw * tx + (qy * tz - qz * ty);
|
||||
var expectedY = v.PositionY[a] + oy + qw * ty + (qz * tx - qx * tz);
|
||||
var expectedZ = v.PositionZ[a] + oz + qw * tz + (qx * ty - qy * tx);
|
||||
Assert.True(MathF.Abs(qz) > 0.05f && v.PositionY[a] > 0.2f, "body A moved and turned");
|
||||
Assert.Equal(expectedX, v.JointOriginX[j], 1e-4f);
|
||||
Assert.Equal(expectedY, v.JointOriginY[j], 1e-4f);
|
||||
Assert.Equal(expectedZ, v.JointOriginZ[j], 1e-4f);
|
||||
Assert.True(MathF.Abs(v.JointOriginY[j] - anchor.Item2) > 0.1f, "not a copy of the creation anchor");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_off_axis_hinge_gets_a_frame_rotated_onto_its_axis()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue