diff --git a/docs/soa-world.md b/docs/soa-world.md
index 65d5e96..45de22e 100644
--- a/docs/soa-world.md
+++ b/docs/soa-world.md
@@ -35,13 +35,16 @@ separate native object (`ymir_box3d_soa.c`, ABI 4) so the Aetheria session's con
- Contact lanes (read-only, rewritten by every `step`): `last_substep_force` (N: the final sub-step's
`normalImpulse` summed over the body's touching manifold points, times sub-steps over dt; 0 for a body
touching nothing; the weight at rest; NOT an impact measure, it sees one sub-step and misses an impact
- step by a large factor either way), `contact_count` (touching contacts) and `material_id` (the body's
+ step by 0.02x to 1.9x), `contact_count` (touching contacts) and `material_id` (the body's
`material_id`, also set as the shape's Box3D user material). Box3D exposes no whole-step impulse:
`totalNormalImpulse` adds the accumulated impulse once per solve and once per relax pass (so it reads twice
- the weight at rest, and on impacts is no fixed multiple of the step's impulse). Impact severity is the hit
- fact's `impulse_estimate` (N s): `approach_speed * (1 + e) / (1/mA + 1/mB)`, e the larger shape restitution
- and only above Box3D's restitution threshold, a static body's 1/m taken as 0. Exact for a hit through both
- centres of mass, an upper bound otherwise (rotation is ignored).
+ the weight at rest, and on impacts is no fixed multiple of the step's impulse). For impact severity see the
+ hit fact. `impulse_estimate` (N s) is `approach_speed * (1 + e) /
+ (1/mA + 1/mB)`, e the larger shape restitution and only above Box3D's restitution threshold, a static
+ body's 1/m taken as 0. It is exact for a centre-to-centre hit between free bodies, can over-count when one
+ collision yields several hit facts (box corners, tilted capsules: they sum), and undercounts heavily for
+ jointed bodies (it takes the limb's own mass, not the joint-coupled effective mass). It is not a bound.
+ A mass-aware per-body impulse that holds for jointed bodies is being designed.
- Contact facts: every shape is created with contact and hit events on (Box3D's default is off). After each
`step`, `ymir_box3d_soa_get_facts` returns pointers and counts into buffers fixed at create (`fact_capacity`
per kind, no allocation in the step): Begin and End `{lane_a, lane_b, stable_id_a, stable_id_b}` and Hit
@@ -70,7 +73,7 @@ lanes agreeing, an off-axis hinge, a limit and a weld, a spherical joint leaving
torque lanes (exact acceleration, cleared after the step, static bodies ignoring them, NaN refusing the
step), capsule and box rest heights with parent and limb lanes, refusals, and bit-identical repeat runs of
a torque-driven jointed body. `Box3DSoaContactTests`: a drop gives one begin and one hit (speed within 5%
-of sqrt(2gh), point and normal), lifting gives one end, resting `last_substep_force` is the weight, the hit `impulse_estimate` matches the ball's momentum change
+of sqrt(2gh), point and normal), lifting gives one end, resting `last_substep_force` is the weight, the hit `impulse_estimate` (a free sphere on a slab, and two free spheres at mass ratios 1:1, 1:10, 10:1) matches the ball's momentum change
(plus gravity's share) at 60 and 240 Hz, 1 and 4 sub-steps, restitution 0 and 0.5, dropped counters resetting each step, materials on
hits, facts lasting one step, overflow counted, per-step byte determinism of facts and contact lanes, and
no allocation across 100 steps with fact reads.
diff --git a/native/Ymir.Box3D/ymir_box3d_soa.c b/native/Ymir.Box3D/ymir_box3d_soa.c
index 3184ca4..135cc41 100644
--- a/native/Ymir.Box3D/ymir_box3d_soa.c
+++ b/native/Ymir.Box3D/ymir_box3d_soa.c
@@ -154,7 +154,8 @@ typedef struct ymir_box3d_soa_view
// Contact lanes, read-only, refreshed every step. last_substep_force is N: the sum over the body's touching
// contact manifold points of the final sub-step's normalImpulse * sub_steps / dt (0 for a body touching
// nothing). It is the load a body carries at rest and NOT an impact measure: it sees one sub-step, so it
- // reads 0.3x to 7x the true force on an impact step. Impact severity is the hit fact's impulse_estimate.
+ // reads 0.02x to 1.9x the true force on an impact step. For impact severity see the hit fact's impulse_estimate and
+ // the limits stated there.
// contact_count is the number of touching contacts. material_id is the body's desc material_id.
const float* last_substep_force;
const int32_t* contact_count;
@@ -182,8 +183,11 @@ typedef struct ymir_box3d_soa_hit_fact
float approach_speed;
// The normal impulse (N s) this collision must deliver, from the approach speed, the two bodies' masses
// and the pair's restitution: v (1 + e) / (1/mA + 1/mB), with e applying only above Box3D's restitution
- // threshold and a static body's 1/m taken as 0. Exact for a hit through both centres of mass (a sphere on
- // a plane); an upper bound otherwise, since rotation only lowers the effective mass's inverse.
+ // threshold and a static body's 1/m taken as 0.
+ // Exact for a centre-to-centre hit between free bodies (a sphere on a plane, two spheres). It can over-count
+ // when one collision yields several hit facts (box corners, tilted capsules), which sum, and it undercounts
+ // heavily for jointed bodies (it takes the limb's own mass, not the joint-coupled effective mass). It is not
+ // a bound either way. A mass-aware per-body impulse that holds for jointed bodies is being designed.
float impulse_estimate;
uint64_t material_a;
uint64_t material_b;
@@ -338,6 +342,16 @@ YMIR_EXPORT uint32_t YMIR_CALL ymir_box3d_soa_get_joint_desc_size(void)
return (uint32_t)sizeof(ymir_box3d_soa_joint_desc);
}
+YMIR_EXPORT uint32_t YMIR_CALL ymir_box3d_soa_get_touch_fact_size(void)
+{
+ return (uint32_t)sizeof(ymir_box3d_soa_touch_fact);
+}
+
+YMIR_EXPORT uint32_t YMIR_CALL ymir_box3d_soa_get_hit_fact_size(void)
+{
+ return (uint32_t)sizeof(ymir_box3d_soa_hit_fact);
+}
+
YMIR_EXPORT uint32_t YMIR_CALL ymir_box3d_soa_get_facts_size(void)
{
return (uint32_t)sizeof(ymir_box3d_soa_facts);
diff --git a/src/Ymir.Box3D/Box3DSoaWorld.cs b/src/Ymir.Box3D/Box3DSoaWorld.cs
index c16d833..6c7b7a0 100644
--- a/src/Ymir.Box3D/Box3DSoaWorld.cs
+++ b/src/Ymir.Box3D/Box3DSoaWorld.cs
@@ -143,7 +143,7 @@ public readonly struct Box3DSoaHit
public readonly float NormalX, NormalY, NormalZ;
/// Metres per second, always positive.
public readonly float ApproachSpeed;
- /// Normal impulse (N s) the collision must deliver: v (1 + e) / (1/mA + 1/mB); exact for a hit through both centres of mass, an upper bound otherwise.
+ /// Normal impulse (N s) the collision must deliver: v (1 + e) / (1/mA + 1/mB). Exact for a centre-to-centre hit between free bodies; it can over-count when one collision yields several hit facts (box corners, tilted capsules), which sum, and it undercounts heavily for jointed bodies (it takes the limb's own mass). It is not a bound. A mass-aware per-body impulse that holds for jointed bodies is being designed.
public readonly float ImpulseEstimate;
public readonly ulong MaterialA;
public readonly ulong MaterialB;
@@ -205,6 +205,14 @@ internal static unsafe partial class Box3DSoaNative
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
internal static partial uint ymir_box3d_soa_get_facts_size();
+ [LibraryImport(Library)]
+ [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
+ internal static partial uint ymir_box3d_soa_get_touch_fact_size();
+
+ [LibraryImport(Library)]
+ [UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
+ internal static partial uint ymir_box3d_soa_get_hit_fact_size();
+
[LibraryImport(Library)]
[UnmanagedCallConv(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
internal static partial int ymir_box3d_soa_create(int capacity, int jointCapacity, float gx, float gy, float gz, int factCapacity, float hitThreshold, out nint world);
@@ -362,7 +370,9 @@ public sealed class Box3DSoaWorld : IDisposable
Box3DSoaNative.ymir_box3d_soa_get_view_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf() ||
Box3DSoaNative.ymir_box3d_soa_get_body_desc_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf() ||
Box3DSoaNative.ymir_box3d_soa_get_joint_desc_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf() ||
- Box3DSoaNative.ymir_box3d_soa_get_facts_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf())
+ Box3DSoaNative.ymir_box3d_soa_get_facts_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf() ||
+ Box3DSoaNative.ymir_box3d_soa_get_touch_fact_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf() ||
+ Box3DSoaNative.ymir_box3d_soa_get_hit_fact_size() != (uint)System.Runtime.CompilerServices.Unsafe.SizeOf())
{
throw new InvalidOperationException("The native Ymir Box3D SoA ABI does not match this build.");
}
diff --git a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaContactTests.cs b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaContactTests.cs
index 531c95d..550492a 100644
--- a/tests/Ymir.Box3D.Soa.Tests/Box3DSoaContactTests.cs
+++ b/tests/Ymir.Box3D.Soa.Tests/Box3DSoaContactTests.cs
@@ -213,6 +213,56 @@ public sealed class Box3DSoaContactTests
Assert.InRange(hit.ImpulseEstimate, truth * 0.92f, truth * 1.08f);
}
+ [Theory]
+ [InlineData(1f, 1f, 0f)]
+ [InlineData(1f, 10f, 0f)]
+ [InlineData(10f, 1f, 0f)]
+ [InlineData(1f, 1f, 0.5f)]
+ [InlineData(1f, 10f, 0.5f)]
+ [InlineData(10f, 1f, 0.5f)]
+ public void The_hit_impulse_estimate_of_two_free_bodies_uses_both_masses(float densityA, float densityB, float restitution)
+ {
+ // No gravity and nothing else touching: the impulse the pair exchanges is exactly m_A (v_A after - before).
+ const int hz = 120;
+ using var world = new Box3DSoaWorld(capacity: 4, gravityY: 0f);
+ var a = world.AddSphere(10, (-1f, 0f, 0f), Radius, densityA, velocity: (3f, 0f, 0f), restitution: restitution);
+ world.AddSphere(11, (1f, 0f, 0f), Radius, densityB, velocity: (-3f, 0f, 0f), restitution: restitution);
+ var massA = BallMass * densityA;
+ var vBefore = world.View.VelocityX[a];
+ var hits = new List();
+ for (var i = 0; i < hz / 2; i++)
+ {
+ world.Step(1f / hz);
+ hits.AddRange(world.Facts.Hit.ToArray());
+ }
+ var hit = Assert.Single(hits);
+ Assert.InRange(hit.ApproachSpeed, 5.8f, 6.2f);
+ var truth = MathF.Abs(massA * (world.View.VelocityX[a] - vBefore));
+ Assert.InRange(hit.ImpulseEstimate, truth * 0.95f, truth * 1.05f);
+ }
+
+ [Fact]
+ public void Restitution_is_ignored_below_the_restitution_threshold_even_for_a_hit()
+ {
+ // Hit threshold 0.1 m/s sits below Box3D's restitution threshold (1 m/s): a 0.5 m/s touch is a hit that
+ // does not bounce, so its impulse is m v, not m v (1 + e).
+ using var bouncy = new Box3DSoaWorld(capacity: 4, gravityY: -G, hitThreshold: 0.1f);
+ bouncy.AddStaticBox(GroundId, (0f, -0.5f, 0f), (50f, 0.5f, 50f));
+ bouncy.AddSphere(BallId, (0f, Radius + 0.0125f, 0f), Radius, density: 1f, restitution: 0.5f);
+ Box3DSoaHit hit = default;
+ var hits = 0;
+ for (var i = 0; i < 60; i++)
+ {
+ bouncy.Step(Dt);
+ hits += bouncy.Facts.Hit.Length;
+ if (bouncy.Facts.Hit.Length > 0) hit = bouncy.Facts.Hit[0];
+ }
+ Assert.Equal(1, hits);
+ Assert.InRange(hit.ApproachSpeed, 0.15f, 0.9f); // above the hit threshold, below the restitution threshold
+ Assert.InRange(hit.ImpulseEstimate, BallMass * hit.ApproachSpeed * 0.99f, BallMass * hit.ApproachSpeed * 1.01f);
+ Assert.True(MathF.Abs(bouncy.View.VelocityY[1]) < 0.1f, "the ball did not bounce");
+ }
+
private static Box3DSoaWorld Pile()
{
var world = new Box3DSoaWorld(capacity: 32, gravityY: -G);