diff --git a/src/BadDay.Cli/BadDay.Cli.csproj b/src/BadDay.Cli/BadDay.Cli.csproj
index 26132d4..7e03e07 100644
--- a/src/BadDay.Cli/BadDay.Cli.csproj
+++ b/src/BadDay.Cli/BadDay.Cli.csproj
@@ -8,5 +8,6 @@
+
diff --git a/src/BadDay.Cli/Program.cs b/src/BadDay.Cli/Program.cs
index da55d74..5aa767d 100644
--- a/src/BadDay.Cli/Program.cs
+++ b/src/BadDay.Cli/Program.cs
@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Globalization;
using BadDay.Core;
+using BadDay.Ymir;
using Lodur;
namespace BadDay.Cli;
@@ -10,6 +11,7 @@ public static class Program
{
private const string Usage = """
baddaysim run --seed N [options] one run, full timeline
+ baddaysim run --seed N --body [options] the same run on the Ymir ragdoll, with the hand-out time
baddaysim sweep --seed N --count M [options] seeds N..N+M-1, break-time summary
options (all numbers, seconds unless noted):
@@ -46,6 +48,18 @@ public static class Program
SampleEverySeconds = Num(opts, "every", 1),
};
+ if (args[0] == "run" && opts.ContainsKey("body"))
+ {
+ var b = BodyPainBox.Run(p with { TickSeconds = BodyPainBox.TickSeconds }, seed);
+ output.Write(b.Run.Render());
+ output.WriteLine(b.Run.Summary());
+ output.WriteLine(b.HandOutMs is { } out_
+ ? $"hand out at {out_} ms, {out_ - b.Run.TimeToBreakMs} ms after the will broke; {b.Excursions} excursions from the box before the break"
+ : $"hand still in the box at {b.Run.EndMs} ms; {b.Excursions} excursions from the box before the break");
+ return 0;
+ }
+ if (opts.ContainsKey("body")) throw new ArgumentException("--body applies to run");
+
if (args[0] == "run")
{
var r = PainBoxSimulation.Run(p, seed);
@@ -70,10 +84,12 @@ public static class Program
private static Dictionary ParseOptions(string[] args)
{
var o = new Dictionary();
- for (int i = 1; i < args.Length; i += 2)
+ for (int i = 1; i < args.Length;)
{
+ if (args[i] == "--body") { o["body"] = ""; i++; continue; } // the one flag
if (!args[i].StartsWith("--") || i + 1 >= args.Length) throw new ArgumentException($"bad option near '{args[i]}'");
o[args[i][2..]] = args[i + 1];
+ i += 2;
}
return o;
}
diff --git a/src/BadDay.Core/PainBoxSession.cs b/src/BadDay.Core/PainBoxSession.cs
index 12762a6..88bba0f 100644
--- a/src/BadDay.Core/PainBoxSession.cs
+++ b/src/BadDay.Core/PainBoxSession.cs
@@ -18,6 +18,7 @@ public sealed class PainBoxSession
private readonly List _timeline = new();
private readonly long _maxTick;
private readonly long _sampleEvery;
+ private readonly int _handRegion;
private double _stimulus;
private long _breakTick = -1;
private int _flinches;
@@ -25,9 +26,11 @@ public sealed class PainBoxSession
private long _endTick;
/// The stimulus volume the hand is held in; its intensity is the session's to write.
+ /// The region of the hand in the box; the timeline's stimulus column is the stimulus there.
public PainBoxSession(PainBoxParameters p, ulong seed, BoxStimulus box, int regionCount,
- ReadOnlySpan holdPose, ReadOnlySpan withdrawPose)
+ ReadOnlySpan holdPose, ReadOnlySpan withdrawPose, int handRegion = 0)
{
+ _handRegion = handRegion;
_p = p;
var rng = new SplitMix64(seed);
// Per-seed traits, drawn first and in fixed order so the stream never shifts.
@@ -84,7 +87,7 @@ public sealed class PainBoxSession
long tick = TickIndex;
_box.Write(tick, ref _sources[0]);
var events = Actor.Tick(x, y, z, _sources, strengths, targets);
- _stimulus = Actor.Stimulus(0);
+ _stimulus = Actor.Stimulus(_handRegion);
if ((events & TickEvents.Flinch) != 0)
{
diff --git a/src/BadDay.Ymir/BadDay.Ymir.csproj b/src/BadDay.Ymir/BadDay.Ymir.csproj
new file mode 100644
index 0000000..1bba383
--- /dev/null
+++ b/src/BadDay.Ymir/BadDay.Ymir.csproj
@@ -0,0 +1,13 @@
+
+
+
+ net10.0
+ enable
+ disable
+ BadDay.Ymir
+
+
+
+
+
+
diff --git a/src/BadDay.Ymir/BodyPainBox.cs b/src/BadDay.Ymir/BodyPainBox.cs
new file mode 100644
index 0000000..7c96db2
--- /dev/null
+++ b/src/BadDay.Ymir/BodyPainBox.cs
@@ -0,0 +1,108 @@
+using System;
+using System.Collections.Generic;
+using BadDay.Core;
+using Lodur;
+using Lodur.Body.Ymir;
+using Ymir.Box3D;
+
+namespace BadDay.Ymir;
+
+/// Where the right hand was after a step, and whether it was inside the box.
+public readonly record struct HandSample(float X, float Y, float Z, bool InBox);
+
+///
+/// The body run's result: the same timeline and outcome as the headless run, plus what only a body has.
+/// 's EndMs is the hand-out time when the hand came out.
+///
+/// First step at or after the will broke with the hand point outside the box; null if it never came out.
+/// Times the hand left the box between the end of the ramp and the break.
+/// The hand after every step; the index is the tick.
+public sealed record BodyPainBoxResult(
+ PainBoxResult Run, long? HandOutMs, int Excursions, IReadOnlyList Hand);
+
+///
+/// The pain box on the body: BDS creates the Ymir world (a ground, a static stool box welded to the pelvis
+/// because the PD ragdoll cannot balance for 15 s, the ragdoll, the box volume around the right hand's hold
+/// point) and steps it. Per step: the box writes its intensity, the body reports its hand points, Lodur's Actor
+/// decides, the ragdoll is driven, the world steps. BDS reaches the puppet only through
+/// , and .
+///
+public static class BodyPainBox
+{
+ /// The ragdoll's gains are tuned at 120 Hz.
+ public const double TickSeconds = 1.0 / 120.0;
+
+ /// Centre of the box and the right hand's hold point.
+ public static readonly (float X, float Y, float Z) HoldPoint = (-0.21f, 1.19f, 0.46f);
+
+ private const float BoxHalf = 0.12f;
+
+ public static BodyPainBoxResult Run(PainBoxParameters p, ulong seed)
+ {
+ if (Math.Abs(p.TickSeconds - TickSeconds) > 1e-12)
+ {
+ throw new ArgumentException($"The body run steps at 1/120 s; got TickSeconds {p.TickSeconds}.");
+ }
+
+ using var world = new Box3DSoaWorld(capacity: HumanoidRagdoll.LimbCount + 2, jointCapacity: HumanoidRagdoll.MuscleCount + 1);
+ world.AddStaticBox(1, (0f, -0.5f, 0f), (20f, 0.5f, 20f), friction: 0.8f);
+ var stool = world.AddStaticBox(2, (0f, 1.11f, -0.115f), (0.05f, 0.05f, 0.025f), friction: 0.8f);
+ var ragdoll = HumanoidRagdoll.Build(world, (0f, 0f), firstStableId: 100);
+ world.AddJoint(Box3DSoaJointKind.Weld, stool, ragdoll.BodyLane(Limb.Pelvis), (0f, 1.11f, -0.09f), (0f, 1f, 0f));
+
+ // Hold: the right arm raised and bent so the hand sits in the box. Withdraw: the arm pulled back and folded.
+ var hold = new Quat[HumanoidRagdoll.MuscleCount];
+ var withdraw = new Quat[HumanoidRagdoll.MuscleCount];
+ Array.Fill(hold, Quat.Identity);
+ Array.Fill(withdraw, Quat.Identity);
+ hold[(int)Muscle.ShoulderR] = Quat.FromAxisAngle(1f, 0f, 0f, -1.0f);
+ hold[(int)Muscle.ElbowR] = Quat.FromAxisAngle(0f, 0f, 1f, -0.6f);
+ withdraw[(int)Muscle.ShoulderR] = Quat.FromAxisAngle(1f, 0f, 0f, 0.3f);
+ withdraw[(int)Muscle.ElbowR] = Quat.FromAxisAngle(0f, 0f, 1f, -2.0f);
+
+ var volume = new BoxStimulus(HoldPoint.X, HoldPoint.Y, HoldPoint.Z, BoxHalf, BoxHalf, BoxHalf);
+ var session = new PainBoxSession(p, seed, volume, HumanoidRagdoll.RegionCount, hold, withdraw, (int)Region.HandR);
+ var x = new float[HumanoidRagdoll.RegionCount];
+ var y = new float[HumanoidRagdoll.RegionCount];
+ var z = new float[HumanoidRagdoll.RegionCount];
+ var strengths = new float[HumanoidRagdoll.MuscleCount];
+ var targets = new Quat[HumanoidRagdoll.MuscleCount];
+
+ var samples = new List();
+ long rampEnd = (long)Math.Round(p.BoxRampSeconds / p.TickSeconds);
+ long? handOut = null;
+ int excursions = 0;
+ bool wasIn = false;
+ long breakTick = -1;
+
+ while (!session.Finished)
+ {
+ if (session.Disabled()) break;
+ long tick = session.TickIndex;
+ ragdoll.ReadRegions(x, y, z);
+ var events = session.Tick(x, y, z, strengths, targets);
+ ragdoll.Drive(strengths, targets);
+ world.Step((float)p.TickSeconds, 4);
+
+ if ((events & TickEvents.WillBroke) != 0) breakTick = tick;
+ ragdoll.ReadRegions(x, y, z);
+ var hand = (X: x[(int)Region.HandR], Y: y[(int)Region.HandR], Z: z[(int)Region.HandR]);
+ var inBox = session.Box.Contains(hand.X, hand.Y, hand.Z);
+ samples.Add(new HandSample(hand.X, hand.Y, hand.Z, inBox));
+
+ if (breakTick < 0)
+ {
+ if (tick > rampEnd && wasIn && !inBox) excursions++;
+ wasIn = inBox;
+ }
+ else if (!inBox)
+ {
+ handOut = session.Ms(tick);
+ session.EndAt(tick);
+ }
+ }
+
+ if (handOut is null && session.Withdrew) session.EndAt(session.TickIndex - 1);
+ return new BodyPainBoxResult(session.Result(), handOut, excursions, samples);
+ }
+}
diff --git a/tests/BadDay.Tests/BadDay.Tests.csproj b/tests/BadDay.Tests/BadDay.Tests.csproj
index 5bb6e2b..7be9fe5 100644
--- a/tests/BadDay.Tests/BadDay.Tests.csproj
+++ b/tests/BadDay.Tests/BadDay.Tests.csproj
@@ -10,5 +10,6 @@
+
diff --git a/tests/BadDay.Tests/Program.cs b/tests/BadDay.Tests/Program.cs
index c42c1f2..b0e9bcd 100644
--- a/tests/BadDay.Tests/Program.cs
+++ b/tests/BadDay.Tests/Program.cs
@@ -3,12 +3,15 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using BadDay.Core;
+using BadDay.Ymir;
namespace BadDay.Tests;
public static class Program
{
private static readonly PainBoxParameters Default = new();
+ private static readonly PainBoxParameters OnBody = new() { TickSeconds = BodyPainBox.TickSeconds };
+ private const int Hz = 120;
private static readonly (string Name, Action Body)[] Tests =
{
@@ -107,6 +110,56 @@ public static class Program
Check(s.Results[0].Seed == 1 && s.Results[24].Seed == 25, "seeds not consecutive");
}),
+ // The body: the same pain box on the Ymir ragdoll, through BodyPainBox. Hands are read from body lanes.
+ ("body: the same seed twice in one process gives the same timeline and hand-out time", () =>
+ {
+ var a = BodyPainBox.Run(OnBody, 1);
+ var b = BodyPainBox.Run(OnBody, 1);
+ Check(a.Run.Render() == b.Run.Render() && a.HandOutMs == b.HandOutMs && a.Excursions == b.Excursions, "body runs differ");
+ Check(a.Hand.Count == b.Hand.Count && a.Hand[^1] == b.Hand[^1], "hand tracks differ");
+ }),
+
+ ("body: the hand stays in the box until the will breaks, a flinch moves it, the broken will pulls it out within 300 ms", () =>
+ {
+ var r = BodyPainBox.Run(OnBody, 1);
+ Check(r.Run.Outcome == Outcome.WillBroke && r.Run.TimeToBreakMs is not null, $"outcome {r.Run.Outcome}");
+ long broke = r.Run.TimeToBreakMs!.Value;
+ Check(r.Excursions == 0, $"{r.Excursions} excursions before the break");
+ int breakTick = Tick(broke);
+ for (int i = 2 * Hz; i < breakTick; i++) Check(r.Hand[i].InBox, $"hand left the box at tick {i}, will broke at {breakTick}");
+
+ var flinches = r.Run.Timeline.Where(e => e.Kind == EntryKind.Flinch).Select(e => Tick(e.TimeMs)).ToList();
+ Check(flinches.Count > 0, "no flinch");
+ bool moved = flinches.Any(f => r.Hand.Skip(f).Take(Hz / 5 + 1).Any(h => Dist(h) > 0.023f));
+ Check(moved, "no flinch moved the hand over 2.3 cm from the hold point");
+
+ Check(r.HandOutMs is { } o && o - broke <= 300, $"hand out at {r.HandOutMs}, will broke at {broke}");
+ Check(r.Run.EndMs == r.HandOutMs, "the run should end when the hand is out");
+ }),
+
+ ("body: switching the box off at 5 s ends the run with the will intact and the hand still in the box", () =>
+ {
+ var r = BodyPainBox.Run(OnBody with { BoxDisableAtSeconds = 5.0 }, 1);
+ Check(r.Run.Outcome == Outcome.BoxDisabled && r.Run.TimeToBreakMs is null, $"outcome {r.Run.Outcome}");
+ Check(r.HandOutMs is null && r.Hand[^1].InBox, "the hand left the box");
+ Check(r.Run.Timeline.Last().Resolve > 0.0, "resolve should be above zero");
+ }),
+
+ ("body: holding costs strain and strain shows: hand speed before the break exceeds hand speed after the ramp", () =>
+ {
+ var r = BodyPainBox.Run(OnBody, 1);
+ int breakTick = Tick(r.Run.TimeToBreakMs!.Value);
+ double early = SpeedRms(r, 2 * Hz, 4 * Hz), late = SpeedRms(r, breakTick - 2 * Hz, breakTick);
+ Check(late > early, $"speed RMS {late:F4} m/s in the 2 s before the break, {early:F4} m/s in the 2 s after the ramp");
+ }),
+
+ ("body: a box at intensity 0 for 10 s: no flinch, no break, the hand holds still", () =>
+ {
+ var r = BodyPainBox.Run(OnBody with { BoxIntensity = 0.0, MaxSeconds = 10.0 }, 1);
+ Check(r.Run.Outcome == Outcome.Endured && r.Run.Flinches == 0 && r.Run.TimeToBreakMs is null, r.Run.Summary());
+ for (int i = 3 * Hz; i < r.Hand.Count; i++) Check(Dist(r.Hand[i]) <= 0.023f, $"drift {Dist(r.Hand[i]):F3} m at tick {i}");
+ }),
+
("CLI run is deterministic and sweep reports a break-time line", () =>
{
string Run(params string[] a) { var w = new StringWriter(); BadDay.Cli.Program.Execute(a, w); return w.ToString(); }
@@ -119,6 +172,25 @@ public static class Program
}),
};
+ private static int Tick(long ms) => (int)Math.Round(ms * Hz / 1000.0);
+
+ private static float Dist(HandSample h)
+ {
+ var c = BodyPainBox.HoldPoint;
+ return MathF.Sqrt((h.X - c.X) * (h.X - c.X) + (h.Y - c.Y) * (h.Y - c.Y) + (h.Z - c.Z) * (h.Z - c.Z));
+ }
+
+ private static double SpeedRms(BodyPainBoxResult r, int from, int to)
+ {
+ double sum = 0;
+ for (int i = from + 1; i < to; i++)
+ {
+ double dx = r.Hand[i].X - r.Hand[i - 1].X, dy = r.Hand[i].Y - r.Hand[i - 1].Y, dz = r.Hand[i].Z - r.Hand[i - 1].Z;
+ sum += (dx * dx + dy * dy + dz * dz) * Hz * Hz;
+ }
+ return Math.Sqrt(sum / (to - from - 1));
+ }
+
public static int Main()
{
int failed = 0;