diff --git a/src/Lodur.Body.Ymir/HumanoidRagdoll.cs b/src/Lodur.Body.Ymir/HumanoidRagdoll.cs index 8289dd3..6cd2316 100644 --- a/src/Lodur.Body.Ymir/HumanoidRagdoll.cs +++ b/src/Lodur.Body.Ymir/HumanoidRagdoll.cs @@ -12,7 +12,7 @@ namespace Lodur.Body.Ymir /// The body regions perception senses at, in the order of the point arrays fills. public enum Region { - HandL, HandR + HandL, HandR, Head } /// The ragdoll's bodies, also the limb label written into the world's limb lane. @@ -32,7 +32,7 @@ namespace Lodur.Body.Ymir { public const int MuscleCount = 12; public const int LimbCount = 13; - public const int RegionCount = 2; + public const int RegionCount = 3; private const float Density = 1000f; private const float Friction = 0.8f; @@ -148,7 +148,7 @@ 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. Allocates nothing. + /// The ragdoll has no hand body, so perception senses at this point. The head is the head body's centre. Allocates nothing. /// public void ReadRegions(Span x, Span y, Span z) { @@ -158,7 +158,7 @@ namespace Lodur.Body.Ymir } var v = _world.View; - for (var r = 0; r < RegionCount; r++) + 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]) @@ -167,6 +167,11 @@ namespace Lodur.Body.Ymir y[r] = v.PositionY[i] + dy; z[r] = v.PositionZ[i] + dz; } + + var head = _bodyLane[(int)Limb.Head]; + x[(int)Region.Head] = v.PositionX[head]; + y[(int)Region.Head] = v.PositionY[head]; + z[(int)Region.Head] = v.PositionZ[head]; } } } diff --git a/tests/Lodur.Tests/ConnectedPainBoxTests.cs b/tests/Lodur.Tests/ConnectedPainBoxTests.cs index 177e8fb..6bb7b9a 100644 --- a/tests/Lodur.Tests/ConnectedPainBoxTests.cs +++ b/tests/Lodur.Tests/ConnectedPainBoxTests.cs @@ -18,7 +18,7 @@ internal sealed class ConnectedPainBox : IDisposable public readonly HumanoidRagdoll Ragdoll; public readonly Actor Actor; public readonly BoxStimulus[] Sources = { new BoxStimulus(Box.X, Box.Y, Box.Z, 0.12f, 0.12f, 0.12f) }; - public readonly float[] X = new float[2], Y = new float[2], Z = new float[2]; + public readonly float[] X = new float[HumanoidRagdoll.RegionCount], Y = new float[HumanoidRagdoll.RegionCount], Z = new float[HumanoidRagdoll.RegionCount]; public readonly float[] Strength = new float[HumanoidRagdoll.MuscleCount]; public readonly Quat[] Target = new Quat[HumanoidRagdoll.MuscleCount]; private SplitMix64 _rng; @@ -82,12 +82,15 @@ public sealed class ConnectedPainBoxTests public void ReadRegions_puts_the_hands_at_the_lower_arm_tips_at_the_creation_pose() { using var s = new RagdollScenario(seed: 1); - var x = new float[2]; var y = new float[2]; var z = new float[2]; + 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(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); + Assert.Equal(0f, z[(int)Region.Head], 3); Assert.Throws(() => s.Ragdoll.ReadRegions(new float[1], y, z)); } @@ -95,7 +98,7 @@ public sealed class ConnectedPainBoxTests public void ReadRegions_allocates_nothing() { using var s = new RagdollScenario(seed: 1); - var x = new float[2]; var y = new float[2]; var z = new float[2]; + 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 before = GC.GetAllocatedBytesForCurrentThread(); for (var i = 0; i < 1000; i++) s.Ragdoll.ReadRegions(x, y, z);