diff --git a/src/Lodur.Body.Ymir/Ragdoll.cs b/src/Lodur.Body.Ymir/Ragdoll.cs index 11a824b..dac829d 100644 --- a/src/Lodur.Body.Ymir/Ragdoll.cs +++ b/src/Lodur.Body.Ymir/Ragdoll.cs @@ -25,6 +25,8 @@ namespace Lodur.Body.Ymir private readonly Box3DSoaConstraintHandle[] _sensors; private readonly float[] _ask, _taken; private bool _hasSensors; + // LowestOpening's last answer (-1 before the first): the opening it keeps until another's rim lies LowestHysteresis lower. + private int _lowest = -1; private readonly MuscleGains[] _gains; // Subtree mass and mass-weighted position per part: scratch rebuilt every Drive, never read across ticks. private readonly float[] _mass, _momentX, _momentY, _momentZ; @@ -324,9 +326,13 @@ namespace Lodur.Body.Ymir return taken; } + /// The margin (m) by which another opening's rim must lie below the current lowest one's before changes its answer: face down the two rims are 0.2 mm apart and a few degrees of head pitch would swap them. + public const float LowestHysteresis = 0.005f; + /// /// The plan airway opening whose rim lies lowest along (the largest drop of , the first on a tie): where liquid leaves - /// (ruling bds-drain-head-decides, the head's pose alone decides). Refuses a zero . Allocates nothing. + /// (ruling bds-drain-head-decides, the head's pose alone decides). The answer keeps the previous call's opening until another's drop exceeds that opening's by , so a head lying face down + /// does not swap nose and mouth with every degree of pitch; the first call (or a ragdoll with no previous answer) takes the largest drop. Refuses a zero . Allocates nothing. /// public int LowestOpening((float X, float Y, float Z) down) { @@ -334,9 +340,11 @@ namespace Lodur.Body.Ymir var deepest = DeepestSump(dx, dy, dz); var best = 0; var bestDrop = float.NegativeInfinity; + var keptDrop = float.NegativeInfinity; for (var k = 0; k < _sensors.Length; k++) { var drop = OpeningDrop(k, dx, dy, dz, deepest); + if (k == _lowest) keptDrop = drop; if (drop > bestDrop) { bestDrop = drop; @@ -344,6 +352,8 @@ namespace Lodur.Body.Ymir } } + if (_lowest >= 0 && bestDrop <= keptDrop + LowestHysteresis) best = _lowest; + _lowest = best; return best; } diff --git a/tests/Lodur.Tests/AirwayTests.cs b/tests/Lodur.Tests/AirwayTests.cs index c9eb13d..5d6d80c 100644 --- a/tests/Lodur.Tests/AirwayTests.cs +++ b/tests/Lodur.Tests/AirwayTests.cs @@ -347,14 +347,27 @@ public sealed class AirwayTests Assert.Equal(0.0, doll.TakeDrawn(-1.0)); } + [Fact] + public void A_wet_airway_takes_nothing_for_an_ask_of_zero_below_zero_or_not_a_number() + { + var (world, doll) = Pooled((Mouth, R)); + using var _ = world; + var before = Read(doll).Pooled[Mouth]; + Assert.Equal(DropVolume, before, 9); + foreach (var ask in new[] { 0.0, -1e-6, -DropVolume, double.NaN, double.NegativeInfinity }) + { + Assert.Equal(0.0, doll.TakeDrawn(ask)); + world.Step(Dt, 4); + Assert.Equal(before, Read(doll).Pooled[Mouth]); + } + } + [Fact] public void Liquid_leaves_by_the_lowest_opening() { using var world = ParticleWorld(Ragdoll.Needs(Plan).Sensors); var doll = Ragdoll.Build(world, Plan, (0f, 0f), firstStableId: 100); Assert.Equal(Mouth, doll.LowestOpening((0f, -1f, 0f))); - var prone = DropOf(doll, (0f, 0f, 1f)); // face down the two rims are 0.2 mm apart (the nose's lower): the opening is the larger drop, not a fixed one - Assert.Equal(prone[Nose] > prone[Mouth] ? Nose : Mouth, doll.LowestOpening((0f, 0f, 1f))); Assert.Equal(Mouth, doll.LowestOpening((0f, 0f, -1f))); Assert.Equal(Nose, doll.LowestOpening((0f, 1f, 0f))); @@ -380,6 +393,50 @@ public sealed class AirwayTests Assert.NotEmpty(seen); } + [Fact] + public void Face_down_the_opening_is_the_larger_drop_at_first_and_then_held_through_the_pitch_sweep() + { + using var world = ParticleWorld(Ragdoll.Needs(Plan).Sensors); + var doll = Ragdoll.Build(world, Plan, (0f, 0f), firstStableId: 100); + var prone = DropOf(doll, (0f, 0f, 1f)); // the two rims are 0.2 mm apart (the nose's lower); a fresh ragdoll takes the larger + Assert.True(prone[Nose] > prone[Mouth] && prone[Nose] - prone[Mouth] < Ragdoll.LowestHysteresis, $"prone drops {prone[Mouth]} {prone[Nose]}"); + Assert.Equal(Nose, doll.LowestOpening((0f, 0f, 1f))); + + // down (0, sin t, cos t): pitch t from face down. + float Sweep(Ragdoll d, float from, float to, float step, int expectFirst) + { + Assert.Equal(expectFirst, d.LowestOpening((0f, MathF.Sin(from * MathF.PI / 180f), MathF.Cos(from * MathF.PI / 180f)))); + for (var deg = from; deg * MathF.Sign(to - from) <= to * MathF.Sign(to - from); deg += step) + if (d.LowestOpening((0f, MathF.Sin(deg * MathF.PI / 180f), MathF.Cos(deg * MathF.PI / 180f))) != expectFirst) return deg; + return float.NaN; + } + + using var upWorld = ParticleWorld(Ragdoll.Needs(Plan).Sensors); + using var downWorld = ParticleWorld(Ragdoll.Needs(Plan).Sensors); + var up = Ragdoll.Build(upWorld, Plan, (0f, 0f), firstStableId: 100); + var down = Ragdoll.Build(downWorld, Plan, (0f, 0f), firstStableId: 100); + // Pitching toward the nose, a ragdoll with no memory swaps Mouth for Nose at -5.8 degrees (Soul's sweep, 0.05 degree steps); with the margin it holds Mouth past face down (measured: to +10.4) + // and pitching back it holds Nose past face down (measured: to -26.3), and each still lands the change inside 40 degrees. + var flipUp = Sweep(up, -40f, 40f, 0.05f, Mouth); + var flipDown = Sweep(down, 40f, -40f, -0.05f, Nose); + Assert.True(flipUp > 0f && flipUp < 40f, $"held Mouth to {flipUp} degrees"); + Assert.True(flipDown < 0f && flipDown > -40f, $"held Nose to {flipDown} degrees"); + } + + [Fact] + public void Two_openings_with_equal_drops_take_the_first() + { + var head = new PartDef("Head", Side.Center, PartShape.Box, (0.1f, 0.1f, 0.1f), (0f, 1f, 0f), Quat.Identity, 1000f, 0.5f, 0f); + var a = Plan.AirwayOpenings[0] with { Part = 0 }; + var twin = a with { Label = "Twin" }; + var plan = new BodyPlan([head], [], [], [a, twin], [new AirwaySumpDef("Sump", 0, (0f, 0f, 0f))]); + var (bodies, joints, _) = Ragdoll.Needs(plan); + using var world = new Box3DSoaWorld(bodies, gravityY: 0f, jointCapacity: joints); + var doll = Ragdoll.Build(world, plan, (0f, 0f), firstStableId: 100); + Assert.Equal(0, doll.LowestOpening((0f, -1f, 0f))); + Assert.Equal(0, doll.LowestOpening((0f, 1f, 0f))); + } + [Fact] public void TakeDrawn_allocates_nothing() {