diff --git a/docs/connect-rungs-map.md b/docs/connect-rungs-map.md
index b67859a..0d3d592 100644
--- a/docs/connect-rungs-map.md
+++ b/docs/connect-rungs-map.md
@@ -30,8 +30,8 @@ All probes in detached worktrees `F:\aeth-wt\connect\{Ymir,Lodur,BadDaySimulator
`docs/soa-world.md` "Not done"): body lanes (stable id, kind, parent, limb, position, rotation quaternion,
linear and angular velocity), write lanes (force, torque), joint lanes. **No contact facts, no sensor shapes,
no overlap or ray queries, no body removal.** Box3D compiles `sensor.c` but Ymir's SoA ABI does not
- surface sensors. So the hand-in-box test is geometry over the body lanes: the ragdoll has no hand body; the
- hand point is the right lower-arm capsule tip, centre + R * (0, -(halfLength + radius), 0) = (0, -0.14, 0).
+ surface sensors. So the hand-in-box test is geometry over the body lanes: the hand point is the
+ palm body's centre (the ragdoll has palm, thumb and fingers bodies per arm).
The box is a game stimulus volume (layer 1 "named game stimuli"), not a physics body.
3. **The SoA world is internal.** `Box3DSoaWorld`, `Box3DSoaView` and the kind enums are `internal`, visible to
`Ymir.Core`, `Ymir.Box3D.Soa.Tests`, `Lodur.Body.Ymir` and `Lodur.Tests` by `InternalsVisibleTo`
@@ -77,7 +77,7 @@ so `content-is-cultcache` is not engaged yet (follow-up `lodur:follow_up:drive-p
|---|---|---|---|
| Nerve, reflex and drive parameters (`NerveParameters`, `WithdrawalParameters`, `DriveParameters`) | Nothing persistent: C# records with defaults in Lodur.Core; a per-actor instance is a value | Built once when the consumer creates an actor (per-seed traits folded in as `Gain`, `DrainTrait`), immutable for the actor's life | Lodur owns the schema and defaults; the consumer (BDS scenario) owns per-actor values and the trait draw |
| Actor (nervous state: afferent rings, drive state, goal) | The consumer's handle; no id inside Lodur | Created with the scenario, ticked once per physics step, never persisted, dies with the scenario | Lodur.Core alone writes it; nothing else writes drive state |
-| Puppet definition (the 13-body humanoid: shapes, joints, gains, regions) | Code in Lodur.Body.Ymir; world stable ids `firstStableId..+12` passed by the caller | Built into the caller's world once; no removal (Ymir has none) | Lodur.Body.Ymir owns geometry and gains; BDS owns placement and stable-id allocation |
+| Puppet definition (the 19-body humanoid: shapes, joints, gains, regions) | Code in Lodur.Body.Ymir; world stable ids `firstStableId..+18` passed by the caller | Built into the caller's world once; no removal (Ymir has none) | Lodur.Body.Ymir owns geometry and gains; BDS owns placement and stable-id allocation |
| Stimulus source (the pain box) | BDS object, scenario-local; not a world body | Placed by the scenario; intensity written every tick from its schedule (ramp, noise, disable time) | BDS writes intensity and on/off; Lodur perception only reads it and decides containment and transduction |
| Restraint (stool weld) | BDS scene object: one static box + one weld joint in the world | Created with the scenario | BDS (a game object acting on the body through the world, like a grab; not a layer above Lodur) |
| Scenario run | (seed, parameters) | One run, headless or on the body | BDS; the timeline is output, not content |
diff --git a/docs/first-slice.md b/docs/first-slice.md
index ff4ff7e..a823ab6 100644
--- a/docs/first-slice.md
+++ b/docs/first-slice.md
@@ -7,9 +7,9 @@ Ymir's structure-of-arrays world. Nothing above layer 5 exists yet.
- `Lodur.Core` (`netstandard2.1`, no references): `Quat`, `MuscleGains`, `PdMuscle.Torque`. A PD law on one
joint: the child's rotation relative to the parent and their relative angular velocity in, a torque out.
- Strength is a multiplier on every gain; zero is limp and returns exactly zero. Swing (joint frame x, y) and
+ Strength is a multiplier on every gain; zero is limp and returns exactly zero, except that a muscle with `Passive` keeps that fraction of its damping (tissue viscosity). Swing (joint frame x, y) and
twist (z) have separate gains.
-- `Lodur.Body.Ymir` (`net8.0`, references Ymir.Box3D): `HumanoidRagdoll` builds 13 bodies and 12 joints in a
+- `Lodur.Body.Ymir` (`net8.0`, references Ymir.Box3D): `HumanoidRagdoll` builds 19 bodies (palm, thumb and fingers per arm) and 18 joints in a
caller-owned `Box3DSoaWorld`: capsule limbs, box pelvis, torso and feet, a sphere head, ball joints at
spine, neck, shoulders and hips, hinges at elbows, knees and ankles. `Drive(strengths, targets)` writes
each muscle's torque onto the world's torque lanes (equal and opposite on child and parent). The strength
diff --git a/src/Lodur.Body.Ymir/HumanoidRagdoll.cs b/src/Lodur.Body.Ymir/HumanoidRagdoll.cs
index c932a00..d8e888f 100644
--- a/src/Lodur.Body.Ymir/HumanoidRagdoll.cs
+++ b/src/Lodur.Body.Ymir/HumanoidRagdoll.cs
@@ -6,7 +6,8 @@ namespace Lodur.Body.Ymir
/// The ragdoll's joints, in strength-array and target-array order.
public enum Muscle
{
- Spine, Neck, ShoulderL, ShoulderR, ElbowL, ElbowR, HipL, HipR, KneeL, KneeR, AnkleL, AnkleR
+ Spine, Neck, ShoulderL, ShoulderR, ElbowL, ElbowR, HipL, HipR, KneeL, KneeR, AnkleL, AnkleR,
+ WristL, WristR, ThumbL, ThumbR, FingersL, FingersR
}
/// The body regions perception senses at, in the order of the point arrays fills.
@@ -18,26 +19,28 @@ namespace Lodur.Body.Ymir
/// The ragdoll's bodies, also the limb label written into the world's limb lane.
public enum Limb
{
- Pelvis, Torso, Head, UpperArmL, UpperArmR, LowerArmL, LowerArmR, ThighL, ThighR, ShinL, ShinR, FootL, FootR
+ Pelvis, Torso, Head, UpperArmL, UpperArmR, LowerArmL, LowerArmR, ThighL, ThighR, ShinL, ShinR, FootL, FootR,
+ PalmL, PalmR, ThumbL, ThumbR, FingersL, FingersR
}
///
- /// A 13-body humanoid built in a caller-owned Ymir SoA world (invariant one-physics-world): capsule limbs,
- /// box pelvis, torso and feet, a sphere head, spherical joints at spine, neck, shoulders and hips, hinges at
- /// elbows, knees and ankles. Facing +z, left is +x, standing on y = 0 at the creation pose, which is also
+ /// A 19-body humanoid built in a caller-owned Ymir SoA world (invariant one-physics-world): capsule limbs,
+ /// box pelvis, torso, feet and palms, a sphere head, a thumb capsule and a fingers box per hand, 18 joints: spherical at
+ /// spine, neck, shoulders, hips, wrists and thumbs, hinges at elbows, knees, ankles and knuckles. Facing +z, left is +x, standing on y = 0 at the creation pose, which is also
/// every joint's zero. The game owns the physics world: Build adds bodies to the caller's world and Lodur never creates or steps one. is the muscle layer: it turns caller-owned strengths and target
/// rotations into torques on the world's torque lanes and does nothing else to the physics.
///
public sealed class HumanoidRagdoll
{
- public const int MuscleCount = 12;
- public const int LimbCount = 13;
+ public const int MuscleCount = 18;
+ public const int LimbCount = 19;
public const int RegionCount = 3;
private const float Density = 1000f;
private const float Friction = 0.8f;
- private const float LowerArmRadius = 0.04f;
- private const float LowerArmHalf = 0.10f;
+ /// The fingers' creation curl at the knuckle, radians: the relaxed resting hand (question hand-rest-curl).
+ private const float RestCurl = 0.35f;
+ private const float Thumb30 = 30f * MathF.PI / 180f; // the thumb's metacarpal, out of the palm plane
private const float HeadRolling = 0.1f; // a bare sphere on the ground rolls on, so the fallen head never settles
private readonly Box3DSoaWorld _world;
@@ -56,7 +59,7 @@ namespace Lodur.Body.Ymir
/// Body and joint capacity a world needs to hold one ragdoll (plus whatever else the caller adds).
public static (int Bodies, int Joints) Needs => (LimbCount, MuscleCount);
- /// Builds the ragdoll standing with its feet on y = 0 at (x, z used). Stable ids are firstStableId.. + 12.
+ /// Builds the ragdoll standing with its feet on y = 0 at (x, z used). Stable ids are firstStableId.. + 18.
public static HumanoidRagdoll Build(Box3DSoaWorld world, (float X, float Z) origin, ulong firstStableId)
{
var r = new HumanoidRagdoll(world);
@@ -78,12 +81,31 @@ namespace Lodur.Body.Ymir
foreach (var (side, sx) in new[] { (0, 1f), (1, -1f) })
{
var upperArm = Capsule(Limb.UpperArmL + side, torso, sx * 0.21f, 1.395f, 0.045f, 0.11f);
- var lowerArm = Capsule(Limb.LowerArmL + side, upperArm, sx * 0.21f, 1.10f, LowerArmRadius, LowerArmHalf);
+ var lowerArm = Capsule(Limb.LowerArmL + side, upperArm, sx * 0.21f, 1.10f, 0.04f, 0.10f);
var thigh = Capsule(Limb.ThighL + side, pelvis, sx * 0.09f, 0.79f, 0.07f, 0.18f);
var shin = Capsule(Limb.ShinL + side, thigh, sx * 0.09f, 0.31f, 0.06f, 0.17f);
Box(Limb.FootL + side, shin, sx * 0.09f, 0.04f, 0.05f, 0.05f, 0.04f, 0.12f);
}
+ // Hands, appended after the base bodies. The palm faces -sx with the thumb forward (+z); the thumb capsule
+ // lies along its metacarpal axis, 30 degrees out of the palm plane; the fingers are created at the rest curl.
+ var cmcAxis = (0f, -MathF.Cos(Thumb30), MathF.Sin(Thumb30));
+ foreach (var (side, sx) in new[] { (0, 1f), (1, -1f) })
+ {
+ var lowerArm = r._bodyLane[(int)Limb.LowerArmL + side];
+ var palm = Box(Limb.PalmL + side, lowerArm, sx * 0.21f, 0.930f, 0f, 0.014f, 0.045f, 0.040f);
+ var thumbSpin = Quat.FromAxisAngle(1f, 0f, 0f, -Thumb30);
+ r._bodyLane[(int)Limb.ThumbL + side] = world.AddCapsule(id++,
+ (ox + sx * 0.206f + 0.05f * cmcAxis.Item1, 0.955f + 0.05f * cmcAxis.Item2, oz + 0.03f + 0.05f * cmcAxis.Item3),
+ 0.011f, 0.03f, Density, (thumbSpin.X, thumbSpin.Y, thumbSpin.Z, thumbSpin.W),
+ friction: Friction, parent: palm, limb: (int)Limb.ThumbL + side);
+ var curl = Quat.FromAxisAngle(0f, 0f, sx, -RestCurl);
+ r._bodyLane[(int)Limb.FingersL + side] = world.AddBox(id++,
+ (ox + sx * 0.21f - sx * MathF.Sin(RestCurl) * 0.0425f, 0.885f - MathF.Cos(RestCurl) * 0.0425f, oz),
+ (0.011f, 0.0425f, 0.038f), Density, (curl.X, curl.Y, curl.Z, curl.W),
+ friction: Friction, parent: palm, limb: (int)Limb.FingersL + side);
+ }
+
void Joint(Muscle m, Box3DSoaJointKind kind, Limb parent, Limb child, float x, float y, float z,
(float, float, float) axis, MuscleGains gains, float lower = 0f, float upper = 0f, float cone = 0f)
{
@@ -101,12 +123,23 @@ namespace Lodur.Body.Ymir
for (var side = 0; side < 2; side++)
{
var sx = side == 0 ? 1f : -1f;
- Joint(Muscle.ShoulderL + side, Box3DSoaJointKind.Spherical, Limb.Torso, Limb.UpperArmL + side, sx * 0.21f, 1.55f, 0f, up, new MuscleGains(20f, 1f, 1f, 0.02f), -1f, 1f, 1.5f);
- Joint(Muscle.ElbowL + side, Box3DSoaJointKind.Revolute, Limb.UpperArmL + side, Limb.LowerArmL + side, sx * 0.21f, 1.24f, 0f, across, new MuscleGains(0f, 0f, 12f, 0.6f), -2.4f, 0f);
+ Joint(Muscle.ShoulderL + side, Box3DSoaJointKind.Spherical, Limb.Torso, Limb.UpperArmL + side, sx * 0.21f, 1.55f, 0f, up, new MuscleGains(20f, 1.5f, 1f, 0.02f), -1f, 1f, 1.5f); // damping carries the hand's mass (hands-gains: 1 and 0.6 before)
+ Joint(Muscle.ElbowL + side, Box3DSoaJointKind.Revolute, Limb.UpperArmL + side, Limb.LowerArmL + side, sx * 0.21f, 1.24f, 0f, across, new MuscleGains(0f, 0f, 12f, 0.9f), -2.4f, 0f);
Joint(Muscle.HipL + side, Box3DSoaJointKind.Spherical, Limb.Pelvis, Limb.ThighL + side, sx * 0.09f, 1.04f, 0f, up, new MuscleGains(600f, 3f, 10f, 0.15f), -0.5f, 0.5f, 1.2f);
Joint(Muscle.KneeL + side, Box3DSoaJointKind.Revolute, Limb.ThighL + side, Limb.ShinL + side, sx * 0.09f, 0.54f, 0f, across, new MuscleGains(0f, 0f, 300f, 8f), 0f, 2.4f);
Joint(Muscle.AnkleL + side, Box3DSoaJointKind.Revolute, Limb.ShinL + side, Limb.FootL + side, sx * 0.09f, 0.08f, 0f, across, new MuscleGains(0f, 0f, 100f, 0.5f), -0.6f, 0.6f);
}
+
+ // Hand gains are bounded by each body's own inertia, not the arm subtree's (explicit PD is stable only while
+ // damping * dt stays under the inertia it acts on; a wrist kd sized for the arm limit-cycles at once), and
+ // every hand joint keeps passive damping when limp so loose hands settle instead of ringing.
+ for (var side = 0; side < 2; side++)
+ {
+ var sx = side == 0 ? 1f : -1f;
+ Joint(Muscle.WristL + side, Box3DSoaJointKind.Spherical, Limb.LowerArmL + side, Limb.PalmL + side, sx * 0.21f, 0.975f, 0f, up, new MuscleGains(1f, 0.012f, 0.3f, 0.005f, 1f), -1.2f, 1.2f, 1.0f);
+ Joint(Muscle.ThumbL + side, Box3DSoaJointKind.Spherical, Limb.PalmL + side, Limb.ThumbL + side, sx * 0.206f, 0.955f, 0.03f, cmcAxis, new MuscleGains(0.06f, 0.001f, 0f, 0f, 1f), 0f, 0f, 0.7f); // twist locked: a free thumb spins
+ Joint(Muscle.FingersL + side, Box3DSoaJointKind.Revolute, Limb.PalmL + side, Limb.FingersL + side, sx * 0.21f, 0.885f, 0f, (0f, 0f, sx), new MuscleGains(0f, 0f, 0.3f, 0.004f, 1f), -1.6f + RestCurl, 0.35f + RestCurl);
+ }
return r;
}
@@ -141,9 +174,8 @@ namespace Lodur.Body.Ymir
}
///
- /// Writes each 's world point into the caller's arrays, one entry per region: a hand is the
- /// tip of its lower-arm capsule, the capsule's centre plus its rotation applied to (0, -(half length + radius), 0).
- /// The ragdoll has no hand body, so perception senses at this point. The head is the head body's centre. Allocates nothing.
+ /// Writes each 's world point into the caller's arrays, one entry per region: a hand is its
+ /// palm body's centre, the head the head body's centre. Allocates nothing.
///
public void ReadRegions(Span x, Span y, Span z)
{
@@ -155,12 +187,10 @@ namespace Lodur.Body.Ymir
var v = _world.View;
for (var r = 0; r < (int)Region.Head; r++)
{
- var i = _bodyLane[(int)(r == (int)Region.HandL ? Limb.LowerArmL : Limb.LowerArmR)];
- new Quat(v.RotationX[i], v.RotationY[i], v.RotationZ[i], v.RotationW[i])
- .Rotate(0f, -(LowerArmHalf + LowerArmRadius), 0f, out var dx, out var dy, out var dz);
- x[r] = v.PositionX[i] + dx;
- y[r] = v.PositionY[i] + dy;
- z[r] = v.PositionZ[i] + dz;
+ var i = _bodyLane[(int)(r == (int)Region.HandL ? Limb.PalmL : Limb.PalmR)];
+ x[r] = v.PositionX[i];
+ y[r] = v.PositionY[i];
+ z[r] = v.PositionZ[i];
}
var head = _bodyLane[(int)Limb.Head];
diff --git a/tests/Lodur.Tests/ConnectedPainBoxTests.cs b/tests/Lodur.Tests/ConnectedPainBoxTests.cs
index b0eea69..1b211f9 100644
--- a/tests/Lodur.Tests/ConnectedPainBoxTests.cs
+++ b/tests/Lodur.Tests/ConnectedPainBoxTests.cs
@@ -12,7 +12,8 @@ namespace Lodur.Tests;
internal sealed class ConnectedPainBox : IDisposable
{
public const double Dt = 1.0 / 120.0;
- public static readonly (float X, float Y, float Z) Box = (-0.21f, 1.19f, 0.46f);
+ /// Where the right palm settles at the hold pose (the box and the shoulder-right strength-1 pose are fixed; measured seed 1 from 3 s).
+ public static readonly (float X, float Y, float Z) Box = (-0.210f, 1.116f, 0.441f);
public readonly Box3DSoaWorld World;
public readonly HumanoidRagdoll Ragdoll;
@@ -102,14 +103,14 @@ public sealed class ConnectedPainBoxTests
MathF.Sqrt((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y) + (a.Z - b.Z) * (a.Z - b.Z));
[Fact]
- public void ReadRegions_puts_the_hands_at_the_lower_arm_tips_at_the_creation_pose()
+ public void ReadRegions_puts_the_hands_at_the_palms_at_the_creation_pose()
{
using var s = new RagdollScenario(seed: 1);
var x = new float[HumanoidRagdoll.RegionCount]; var y = new float[HumanoidRagdoll.RegionCount]; var z = new float[HumanoidRagdoll.RegionCount];
s.Ragdoll.ReadRegions(x, y, z);
Assert.Equal(0.21f, x[(int)Region.HandL], 3);
Assert.Equal(-0.21f, x[(int)Region.HandR], 3);
- Assert.Equal(0.96f, y[(int)Region.HandR], 3);
+ Assert.Equal(0.93f, y[(int)Region.HandR], 3);
Assert.Equal(0f, z[(int)Region.HandR], 3);
Assert.Equal(0f, x[(int)Region.Head], 3); // the head body's centre, within a millimetre
Assert.Equal(1.74f, y[(int)Region.Head], 3);
diff --git a/tests/Lodur.Tests/HandTests.cs b/tests/Lodur.Tests/HandTests.cs
new file mode 100644
index 0000000..a748aaa
--- /dev/null
+++ b/tests/Lodur.Tests/HandTests.cs
@@ -0,0 +1,192 @@
+using Lodur;
+using Lodur.Body.Ymir;
+using Ymir.Box3D;
+
+namespace Lodur.Tests;
+
+public sealed class HandTests
+{
+ private static readonly Limb[] HandBodies = { Limb.PalmL, Limb.PalmR, Limb.ThumbL, Limb.ThumbR, Limb.FingersL, Limb.FingersR };
+
+ /// FNV-1a 64 over the creation lanes of the 13 base bodies (world lanes 1..13) and the 12 base joints.
+ private static ulong BaseCreationHash(Box3DSoaWorld world)
+ {
+ var v = world.View;
+ var h = 14695981039346656037ul;
+ void U(ulong x) { for (var k = 0; k < 8; k++) { h ^= (byte)(x >> (8 * k)); h *= 1099511628211ul; } }
+ void F(float f) => U((uint)BitConverter.SingleToInt32Bits(f));
+ for (var i = 1; i <= 13; i++)
+ {
+ U(v.StableId[i]); U(v.Kind[i]); U((ulong)v.Parent[i]); U((ulong)v.Limb[i]);
+ F(v.Size0[i]); F(v.Size1[i]); F(v.Size2[i]);
+ F(v.PositionX[i]); F(v.PositionY[i]); F(v.PositionZ[i]);
+ F(v.RotationX[i]); F(v.RotationY[i]); F(v.RotationZ[i]); F(v.RotationW[i]);
+ }
+ for (var j = 0; j < 12; j++)
+ {
+ U(v.JointKind[j]); U((ulong)v.JointBodyA[j]); U((ulong)v.JointBodyB[j]);
+ F(v.JointFrameX[j]); F(v.JointFrameY[j]); F(v.JointFrameZ[j]); F(v.JointFrameW[j]);
+ F(v.JointOriginX[j]); F(v.JointOriginY[j]); F(v.JointOriginZ[j]);
+ }
+ return h;
+ }
+
+ /// Recorded at Lodur dedf391 (no hands); equal there and with the hands appended.
+ private const ulong BaseHash = 0xccf5cedb0df1b2e8ul;
+
+ [Fact]
+ public void Hands_are_appended_after_the_base_bodies_and_joints_and_leave_them_as_they_were()
+ {
+ using var s = new RagdollScenario(seed: 1);
+ var v = s.World.View;
+ var r = s.Ragdoll;
+ Assert.Equal(1 + 19, v.Count);
+ Assert.Equal(18, v.JointCount);
+ Assert.Equal(BaseHash, BaseCreationHash(s.World));
+ Assert.Equal(new ulong[] { 113, 114, 115, 116, 117, 118 }, Enumerable.Range(14, 6).Select(i => v.StableId[i]).ToArray());
+ Assert.All(HandBodies, l => Assert.InRange(r.BodyLane(l), 14, 19));
+ foreach (var m in new[] { Muscle.WristL, Muscle.WristR, Muscle.ThumbL, Muscle.ThumbR, Muscle.FingersL, Muscle.FingersR }) Assert.InRange(r.JointLane(m), 12, 17);
+ foreach (var side in new[] { 0, 1 })
+ {
+ var palm = r.BodyLane(Limb.PalmL + side);
+ var thumb = r.BodyLane(Limb.ThumbL + side);
+ var fingers = r.BodyLane(Limb.FingersL + side);
+ Assert.Equal(r.BodyLane(Limb.LowerArmL + side), v.Parent[palm]);
+ Assert.Equal(palm, v.Parent[thumb]);
+ Assert.Equal(palm, v.Parent[fingers]);
+ Assert.Equal(((uint)Box3DSoaKind.Box, (uint)Box3DSoaKind.Capsule, (uint)Box3DSoaKind.Box), (v.Kind[palm], v.Kind[thumb], v.Kind[fingers]));
+ Assert.Equal((0.014f, 0.045f, 0.040f), (v.Size0[palm], v.Size1[palm], v.Size2[palm]));
+ Assert.Equal((0.011f, 0.03f), (v.Size0[thumb], v.Size1[thumb]));
+ Assert.Equal((0.011f, 0.0425f, 0.038f), (v.Size0[fingers], v.Size1[fingers], v.Size2[fingers]));
+ Assert.Equal((r.BodyLane(Limb.LowerArmL + side), palm), (v.JointBodyA[r.JointLane(Muscle.WristL + side)], v.JointBodyB[r.JointLane(Muscle.WristL + side)]));
+ Assert.Equal((palm, thumb), (v.JointBodyA[r.JointLane(Muscle.ThumbL + side)], v.JointBodyB[r.JointLane(Muscle.ThumbL + side)]));
+ Assert.Equal((palm, fingers), (v.JointBodyA[r.JointLane(Muscle.FingersL + side)], v.JointBodyB[r.JointLane(Muscle.FingersL + side)]));
+ }
+ }
+
+ [Fact]
+ public void The_hand_region_is_the_palm_body_wherever_the_palm_goes()
+ {
+ using var s = new RagdollScenario(seed: 2);
+ Array.Clear(s.Strength);
+ s.Run(1.0f); // falling: the palms are nowhere near the creation pose
+ var x = new float[HumanoidRagdoll.RegionCount]; var y = new float[HumanoidRagdoll.RegionCount]; var z = new float[HumanoidRagdoll.RegionCount];
+ s.Ragdoll.ReadRegions(x, y, z);
+ var v = s.World.View;
+ foreach (var (region, limb) in new[] { (Region.HandL, Limb.PalmL), (Region.HandR, Limb.PalmR) })
+ {
+ var i = s.Ragdoll.BodyLane(limb);
+ Assert.Equal((v.PositionX[i], v.PositionY[i], v.PositionZ[i]), (x[(int)region], y[(int)region], z[(int)region]));
+ }
+ }
+
+ /// Runs , then one more second step by step; the fastest the limbs turn and move in that second.
+ private static (float Angular, float Linear) LastSecond(RagdollScenario s, float runSeconds, IEnumerable limbs)
+ {
+ s.Run(runSeconds);
+ float ang = 0f, lin = 0f;
+ var watched = limbs.ToArray();
+ for (var step = 0; step < 120; step++)
+ {
+ s.Run(RagdollScenario.Dt);
+ foreach (var l in watched)
+ {
+ var sp = s.Speeds(l);
+ ang = MathF.Max(ang, sp.Angular);
+ lin = MathF.Max(lin, sp.Linear);
+ }
+ }
+ return (ang, lin);
+ }
+
+ [Theory]
+ [InlineData(1ul)]
+ [InlineData(2ul)]
+ [InlineData(3ul)]
+ public void Hands_rest_standing_palm_thumb_and_fingers_alike(ulong seed)
+ {
+ using var s = new RagdollScenario(seed);
+ var (ang, _) = LastSecond(s, 5f, HandBodies);
+ Assert.True(ang < 0.05f, $"seed {seed}: a hand body (thumbs included: a free thumb twist spins) still turns at {ang} rad/s in the last second of 6");
+ }
+
+ [Fact]
+ public void Limp_hands_settle_instead_of_ringing()
+ {
+ for (ulong seed = 1; seed <= 20; seed++)
+ {
+ using var s = new RagdollScenario(seed);
+ Array.Clear(s.Strength);
+ var (ang, lin) = LastSecond(s, 7f, HandBodies);
+ Assert.True(ang < 1f && lin < 0.1f, $"seed {seed}: a limp hand still moves at {lin} m/s, {ang} rad/s in the last second of 8");
+ }
+ }
+
+ /// Distance between the two bodies' copies of a joint's anchor, each carried by its own body from creation.
+ private sealed class JointSeparation
+ {
+ private readonly (int A, int B, (float X, float Y, float Z) LocalA, (float X, float Y, float Z) LocalB)[] _joints;
+
+ public JointSeparation(Box3DSoaWorld world, IEnumerable jointLanes)
+ {
+ var v = world.View;
+ _joints = jointLanes.Select(j =>
+ {
+ int a = v.JointBodyA[j], b = v.JointBodyB[j];
+ return (a, b, Local(v, a, v.JointOriginX[j], v.JointOriginY[j], v.JointOriginZ[j]), Local(v, b, v.JointOriginX[j], v.JointOriginY[j], v.JointOriginZ[j]));
+ }).ToArray();
+ }
+
+ private static (float, float, float) Local(Box3DSoaView v, int i, float x, float y, float z)
+ {
+ new Quat(v.RotationX[i], v.RotationY[i], v.RotationZ[i], v.RotationW[i]).Conjugate()
+ .Rotate(x - v.PositionX[i], y - v.PositionY[i], z - v.PositionZ[i], out var lx, out var ly, out var lz);
+ return (lx, ly, lz);
+ }
+
+ private static (float X, float Y, float Z) World(Box3DSoaView v, int i, (float X, float Y, float Z) p)
+ {
+ new Quat(v.RotationX[i], v.RotationY[i], v.RotationZ[i], v.RotationW[i]).Rotate(p.X, p.Y, p.Z, out var x, out var y, out var z);
+ return (x + v.PositionX[i], y + v.PositionY[i], z + v.PositionZ[i]);
+ }
+
+ public float Max(Box3DSoaView v)
+ {
+ var max = 0f;
+ foreach (var (a, b, la, lb) in _joints)
+ {
+ var p = World(v, a, la); var q = World(v, b, lb);
+ max = MathF.Max(max, MathF.Sqrt((p.X - q.X) * (p.X - q.X) + (p.Y - q.Y) * (p.Y - q.Y) + (p.Z - q.Z) * (p.Z - q.Z)));
+ }
+ return max;
+ }
+ }
+
+ [Fact]
+ public void Hand_joints_hold_together_under_a_flail_as_well_as_the_body_joints_do()
+ {
+ using var s = new RagdollScenario(seed: 1);
+ var handJoints = new[] { Muscle.WristL, Muscle.WristR, Muscle.ThumbL, Muscle.ThumbR, Muscle.FingersL, Muscle.FingersR }.Select(m => s.Ragdoll.JointLane(m));
+ var sep = new JointSeparation(s.World, handJoints);
+ var worst = 0f;
+ for (var i = 0; i < 6 * 120; i++)
+ {
+ var strike = (i / 30) % 2 == 0;
+ foreach (var (sh, el) in new[] { (Muscle.ShoulderL, Muscle.ElbowL), (Muscle.ShoulderR, Muscle.ElbowR) })
+ {
+ s.Target[(int)sh] = Quat.FromAxisAngle(1f, 0f, 0f, strike ? -1.5f : -0.5f);
+ s.Target[(int)el] = Quat.FromAxisAngle(0f, 0f, 1f, strike ? 0f : -2.2f);
+ }
+ s.Target[(int)Muscle.FingersL] = s.Target[(int)Muscle.FingersR] = Quat.FromAxisAngle(0f, 0f, 1f, strike ? -1.5f : 0f);
+ s.Target[(int)Muscle.WristL] = s.Target[(int)Muscle.WristR] = Quat.FromAxisAngle(1f, 0f, 0f, strike ? 0.8f : -0.8f);
+ s.Run(RagdollScenario.Dt);
+ var v = s.World.View;
+ for (var b = 0; b < v.Count; b++)
+ {
+ Assert.True(float.IsFinite(v.PositionX[b] + v.PositionY[b] + v.PositionZ[b] + v.RotationW[b] + v.VelocityX[b] + v.AngularVelocityX[b]), $"body {b} is finite at step {i}");
+ }
+ worst = MathF.Max(worst, sep.Max(v));
+ }
+ Assert.True(worst <= 0.030f, $"a hand joint came apart by {worst * 1000f} mm");
+ }
+}