From bd443efd3e405ad7d2074342424e7bd3313c56dd Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 26 Nov 2026 06:43:51 +0100 Subject: [PATCH] Soa world: a per-body Mass lane from Box3D and a read-only Gravity (ABI 19) Co-Authored-By: Claude Sonnet 5.5 --- docs/soa-world.md | 6 +- native/Ymir.Box3D/ymir_box3d_soa.c | 12 +++- src/Ymir.Box3D/Box3DSoaWorld.cs | 9 ++- .../Ymir.Box3D.Soa.Tests/Box3DSoaGrabTests.cs | 8 +-- .../Box3DSoaWorldTests.cs | 55 +++++++++++++++++++ 5 files changed, 80 insertions(+), 10 deletions(-) diff --git a/docs/soa-world.md b/docs/soa-world.md index 1c47445..7a18368 100644 --- a/docs/soa-world.md +++ b/docs/soa-world.md @@ -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 18) so the Aetheria session's contract is untouched. +separate native object (`ymir_box3d_soa.c`, ABI 19) so the Aetheria session's contract is untouched. ## Contract -- Native (ABI 18): `ymir_box3d_soa_create(capacity, joint_capacity, gravity, fact_capacity, hit_threshold, particle_capacities*) / +- Native (ABI 19): `ymir_box3d_soa_create(capacity, joint_capacity, gravity, fact_capacity, hit_threshold, particle_capacities*) / create_particles / destroy_particles / create_distances / create_bends / create_sensors / attach(n, desc*) / set_pinned(n, handles, pinned) / remove / set_material / free_slots / add_body / add_joint / step / get_view / get_facts / set_anchor_target / destroy_joint / set_joint_thresholds / remove_body / free_joint_slots / free_body_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, @@ -46,7 +46,7 @@ separate native object (`ymir_box3d_soa.c`, ABI 18) so the Aetheria session's co average: heavy balls in a pile alternate between about 1.5 and 0.5 m g dt from step to step, so a consumer must not threshold a single step; average over steps or use `contact_impulse / dt` as a load estimate over a window), `contact_count` - (touching contacts) and `material_id` (the body's `material_id`, also set as the shape's Box3D user + (touching contacts), `mass` (kg, Box3D's `b3Body_GetMass` copied once at create; 0 for static, kinematic, anchor and removed lanes) and `material_id` (the body's `material_id`, also set as the shape's Box3D user material). `stepNormalImpulse` is the GameCult Box3D fork's observed field (see `box3d-parity.md`); it is the normal channel only, since Box3D exposes no tangent impulse. Sleep is off in SoA worlds, so manifolds hold no stale values; if sleep is ever enabled, zero the lanes of sleeping bodies. The hit fact's `impulse` diff --git a/native/Ymir.Box3D/ymir_box3d_soa.c b/native/Ymir.Box3D/ymir_box3d_soa.c index fb5048e..5c04007 100644 --- a/native/Ymir.Box3D/ymir_box3d_soa.c +++ b/native/Ymir.Box3D/ymir_box3d_soa.c @@ -62,7 +62,7 @@ enum YMIR_SOA_JOINT_REMOVED = 4 // a retired joint's lane (destroy_joint): never accepted by add_joint }; -#define YMIR_SOA_ABI_VERSION 18u +#define YMIR_SOA_ABI_VERSION 19u // 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 @@ -213,6 +213,8 @@ typedef struct ymir_box3d_soa_view const float* contact_impulse; const int32_t* contact_count; const uint64_t* material_id; + // mass is kg from Box3D at create (b3Body_GetMass): 0 for static, kinematic and anchor bodies and for removed lanes. + const float* mass; // Per body lane: counts the removals of that lane. remove_body refuses a handle whose generation is not the lane's, so a stale // removal never takes a later body in a reused lane. A removed body's lane reads kind REMOVED and every other lane 0 (stable_id 0, parent -1). const uint32_t* body_generation; @@ -460,6 +462,7 @@ typedef struct ymir_box3d_soa_world int32_t* limb; int32_t* contact_count; uint64_t* material_id; + float* mass; uint32_t* body_generation; uint64_t* body_removed_at; // step_count when the lane was removed; reusable once a step has passed float* size; // 3 arrays of `capacity` floats, lane-major; written once in add_body @@ -644,6 +647,7 @@ static void ymir_soa_free(ymir_box3d_soa_world* world) free(world->limb); free(world->contact_count); free(world->material_id); + free(world->mass); free(world->body_generation); free(world->body_removed_at); free(world->size); @@ -715,6 +719,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create( world->limb = (int32_t*)calloc(n, sizeof(int32_t)); world->contact_count = (int32_t*)calloc(n, sizeof(int32_t)); world->material_id = (uint64_t*)calloc(n, sizeof(uint64_t)); + world->mass = (float*)calloc(n, sizeof(float)); world->body_generation = (uint32_t*)calloc(n, sizeof(uint32_t)); world->body_removed_at = (uint64_t*)calloc(n, sizeof(uint64_t)); world->size = (float*)calloc(n * 3, sizeof(float)); @@ -751,7 +756,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_create( return YMIR_SOA_OUT_OF_MEMORY; } } - if (world->contact_count == NULL || world->material_id == NULL || world->body_generation == NULL || world->body_removed_at == NULL || world->begin_facts == NULL || + if (world->contact_count == NULL || world->material_id == NULL || world->mass == NULL || world->body_generation == NULL || world->body_removed_at == NULL || world->begin_facts == NULL || world->end_facts == NULL || world->hit_facts == NULL || world->overload_facts == NULL || world->contact_scratch == NULL || world->body_removed_facts == NULL || world->body_removed_pending == NULL || world->joint_removed_facts == NULL || world->joint_removed_pending == NULL || world->retired_shapes == NULL || @@ -974,6 +979,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_add_body( world->limb[i] = d->limb; world->contact_count[i] = 0; world->material_id[i] = ymir_soa_is_anchor(d->kind) ? 0 : d->material_id; + world->mass[i] = ymir_soa_is_dynamic(d->kind) ? b3Body_GetMass(body) : 0.0f; // the shapes exist above, so Box3D's mass is final /* Publish only the components the kind uses; unused ones read 0 whatever the caller passed. */ int used = ymir_soa_is_anchor(d->kind) ? 0 : d->kind == YMIR_SOA_KIND_SPHERE ? 1 : d->kind == YMIR_SOA_KIND_CAPSULE ? 2 : 3; for (int k = 0; k < 3; k++) @@ -1229,6 +1235,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_remove_body(ymir_box3d_soa_world* w world->limb[lane] = 0; world->contact_count[lane] = 0; world->material_id[lane] = 0; + world->mass[lane] = 0.0f; world->body_removed_at[lane] = world->step_count; world->body_generation[lane] += 1; for (int k = 0; k < 3; ++k) @@ -1910,6 +1917,7 @@ YMIR_EXPORT int32_t YMIR_CALL ymir_box3d_soa_get_view(const ymir_box3d_soa_world .contact_impulse = base + YMIR_SOA_CONTACT_IMPULSE_LANE * n, .contact_count = world->contact_count, .material_id = world->material_id, + .mass = world->mass, .body_generation = world->body_generation, .joint_generation = world->joint_generation, .particle_impulse_x = base + (YMIR_SOA_PARTICLE_IMPULSE_LANE + 0) * n, diff --git a/src/Ymir.Box3D/Box3DSoaWorld.cs b/src/Ymir.Box3D/Box3DSoaWorld.cs index af83211..ad72b62 100644 --- a/src/Ymir.Box3D/Box3DSoaWorld.cs +++ b/src/Ymir.Box3D/Box3DSoaWorld.cs @@ -332,6 +332,7 @@ internal unsafe struct Box3DSoaViewNative public float* ContactImpulse; public int* ContactCount; public ulong* MaterialId; + public float* Mass; public uint* BodyGeneration; public uint* JointGeneration; public float* ParticleImpulseX; @@ -771,6 +772,8 @@ public readonly unsafe struct Box3DSoaView public ReadOnlySpan ContactCount => Lane(_v.ContactCount, Count); /// The material id the body was added with (0 by default). public ReadOnlySpan MaterialId => Lane(_v.MaterialId, Count); + /// kg, from Box3D at create (density and shape); 0 for static, kinematic, anchor and removed lanes. + public ReadOnlySpan Mass => Lane(_v.Mass, Count); /// Per body lane, the number of times it has been removed. A body's handle is its lane and the generation read when it was added; /// refuses a handle from an earlier occupant of the lane. is the high-water mark of lanes /// ever used: a lane with kind is free (reused once a step has completed). @@ -966,9 +969,12 @@ public readonly unsafe struct Box3DSoaView /// public sealed class Box3DSoaWorld : IDisposable { - private const uint ExpectedAbi = 18; + private const uint ExpectedAbi = 19; private nint _handle; + /// The gravity (m/s^2) this world was created with; native has no setter, so this copy cannot disagree. + public (float X, float Y, float Z) Gravity { get; } + /// /// bounds each kind of per-step contact fact (default 4 x capacity; 0 or less is refused); /// is the approach speed in m/s above which a touch is a hit (Box3D's default, 1). @@ -1000,6 +1006,7 @@ public sealed class Box3DSoaWorld : IDisposable var caps = particles.GetValueOrDefault(); Check(Box3DSoaNative.ymir_box3d_soa_create(capacity, jointCapacity, gravityX, gravityY, gravityZ, factCapacity ?? checked(capacity * 4), hitThreshold, particles.HasValue ? &caps : null, out _handle)); + Gravity = (gravityX, gravityY, gravityZ); } /// Fails closed when the native library is not the ABI this build expects, in either direction. diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaGrabTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaGrabTests.cs index 6718dc4..cdae92b 100644 --- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaGrabTests.cs +++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaGrabTests.cs @@ -167,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(18u, Box3DSoaNative.ymir_box3d_soa_get_abi_version()); - Box3DSoaWorld.RequireNativeAbi(18); - Assert.Throws(() => Box3DSoaWorld.RequireNativeAbi(17)); // an older native under this managed build - Assert.Throws(() => Box3DSoaWorld.RequireNativeAbi(19)); // a newer one + Assert.Equal(19u, Box3DSoaNative.ymir_box3d_soa_get_abi_version()); + Box3DSoaWorld.RequireNativeAbi(19); + Assert.Throws(() => Box3DSoaWorld.RequireNativeAbi(18)); // an older native under this managed build + Assert.Throws(() => Box3DSoaWorld.RequireNativeAbi(20)); // a newer one } [Fact] diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWorldTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWorldTests.cs index b3534b2..fbb2a6b 100644 --- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWorldTests.cs +++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaWorldTests.cs @@ -206,4 +206,59 @@ public sealed unsafe class Box3DSoaWorldTests var view = default(Box3DSoaView); Assert.Throws(() => view.PositionY.Length); } + + // Box3D's own mass is the owner; the formulas below live here only as the oracle of it. + [Fact] + public void Mass_is_density_times_the_shapes_volume() + { + const float rho = 800f, r = 0.3f, h = 0.45f; + using var world = new Box3DSoaWorld(capacity: 4); + var sphere = world.AddSphere(1, (0f, 1f, 0f), r, rho); + var capsule = world.AddCapsule(2, (3f, 1f, 0f), r, h, rho); + var box = world.AddBox(3, (6f, 1f, 0f), (0.2f, 0.3f, 0.5f), rho); + var view = world.View; + Assert.Equal(rho * 4f / 3f * MathF.PI * r * r * r, view.Mass[sphere], rho * 4f / 3f * MathF.PI * r * r * r * 1e-5f); + var capsuleVolume = MathF.PI * r * r * 2f * h + 4f / 3f * MathF.PI * r * r * r; + Assert.Equal(rho * capsuleVolume, view.Mass[capsule], rho * capsuleVolume * 1e-5f); + var boxVolume = 8f * 0.2f * 0.3f * 0.5f; + Assert.Equal(rho * boxVolume, view.Mass[box], rho * boxVolume * 1e-5f); + } + + [Fact] + public void Static_and_removed_lanes_read_zero_mass() + { + using var world = new Box3DSoaWorld(capacity: 4); + var ground = world.AddStaticBox(1, (0f, -0.5f, 0f), (5f, 0.5f, 5f)); + var ball = world.AddSphere(2, (0f, 2f, 0f), 0.5f, 1000f); + var view = world.View; + Assert.Equal(0f, view.Mass[ground]); + Assert.True(view.Mass[ball] > 0f); + world.RemoveBody(ball); + Assert.Equal(0f, view.Mass[ball]); + } + + [Fact] + public void A_hung_capsules_joint_carries_mass_times_gravity() + { + using var world = new Box3DSoaWorld(capacity: 4, jointCapacity: 2); + var pin = world.AddStaticBox(1, (0f, 5f, 0f), (0.1f, 0.1f, 0.1f)); + var capsule = world.AddCapsule(2, (0f, 4f, 0f), 0.05f, 0.4f, 1000f); + var joint = world.AddJoint(Box3DSoaJointKind.Spherical, pin, capsule, (0f, 5f, 0f), (0f, 0f, 1f)); + var view = world.View; + for (var i = 0; i < 180; i++) world.Step(Dt); + var g = world.Gravity; + var weight = view.Mass[capsule] * MathF.Sqrt(g.X * g.X + g.Y * g.Y + g.Z * g.Z); + var load = MathF.Sqrt(view.JointConstraintForceX[joint] * view.JointConstraintForceX[joint] + + view.JointConstraintForceY[joint] * view.JointConstraintForceY[joint] + + view.JointConstraintForceZ[joint] * view.JointConstraintForceZ[joint]); + Assert.Equal(weight, load, weight * 0.01f); + } + + [Fact] + public void Gravity_reads_back_the_constructor_arguments() + { + using (var world = new Box3DSoaWorld(capacity: 1)) Assert.Equal((0f, -10f, 0f), world.Gravity); + using (var world = new Box3DSoaWorld(capacity: 1, gravityX: 0f, gravityY: 0f, gravityZ: 0f)) Assert.Equal((0f, 0f, 0f), world.Gravity); + using (var world = new Box3DSoaWorld(capacity: 1, gravityX: 1f, gravityY: -9.81f, gravityZ: 2f)) Assert.Equal((1f, -9.81f, 2f), world.Gravity); + } }