BDS resolves joint, part and sense-point labels against the humanoid BodyPlan once, at build

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-25 00:39:08 +01:00
commit 3d6e735a47
3 changed files with 60 additions and 42 deletions

View file

@ -24,8 +24,8 @@ public sealed record BodyPainBoxResult(
/// <summary>
/// The pain box on the body: <see cref="BodyPainBoxSession"/> is the one owner of the Ymir world and its loop, and
/// <see cref="Run"/> steps one to the end. BDS reaches the puppet only through <see cref="HumanoidRagdoll.Build"/>,
/// <see cref="HumanoidRagdoll.ReadRegions"/> and <see cref="HumanoidRagdoll.Drive"/>.
/// <see cref="Run"/> steps one to the end. BDS reaches the puppet only through <see cref="Ragdoll.Build"/>,
/// <see cref="Ragdoll.ReadSensePoints"/> and <see cref="Ragdoll.Drive"/>.
/// </summary>
public static class BodyPainBox
{
@ -80,14 +80,15 @@ public sealed class BodyPainBoxSession : IDisposable
private readonly PainBoxParameters _p;
private readonly Box3DSoaWorld _world;
private readonly HumanoidRagdoll _ragdoll;
private readonly Ragdoll _ragdoll;
private readonly PainBoxSession _session;
private readonly Queue<BodyEvent> _events = new();
private readonly float[] _x = new float[HumanoidRagdoll.RegionCount];
private readonly float[] _y = new float[HumanoidRagdoll.RegionCount];
private readonly float[] _z = new float[HumanoidRagdoll.RegionCount];
private readonly float[] _strengths = new float[HumanoidRagdoll.MuscleCount];
private readonly Quat[] _targets = new Quat[HumanoidRagdoll.MuscleCount];
private readonly float[] _x;
private readonly float[] _y;
private readonly float[] _z;
private readonly float[] _strengths;
private readonly Quat[] _targets;
private readonly int _handR;
private readonly long _rampEnd;
private readonly long _settleTicks;
private readonly long _outHoldTicks;
@ -112,20 +113,29 @@ public sealed class BodyPainBoxSession : IDisposable
try
{
_ragdoll = SeatedPuppet.Build(_world);
var plan = _ragdoll.Plan;
int senses = plan.SensePoints.Count, joints = plan.Joints.Count;
_x = new float[senses];
_y = new float[senses];
_z = new float[senses];
_strengths = new float[joints];
_targets = new Quat[joints];
_handR = plan.SenseIndex("Hand", Side.Right);
int shoulderR = plan.JointIndex("Shoulder", Side.Right), elbowR = plan.JointIndex("Elbow", Side.Right);
// 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];
var hold = new Quat[joints];
var withdraw = new Quat[joints];
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);
hold[shoulderR] = Quat.FromAxisAngle(1f, 0f, 0f, -1.0f);
hold[elbowR] = Quat.FromAxisAngle(0f, 0f, 1f, -0.6f);
withdraw[shoulderR] = Quat.FromAxisAngle(1f, 0f, 0f, 0.3f);
withdraw[elbowR] = Quat.FromAxisAngle(0f, 0f, 1f, -2.0f);
var hp = BodyPainBox.HoldPoint;
var volume = new BoxStimulus(hp.X, hp.Y, hp.Z, BoxHalf, BoxHalf, BoxHalf);
_session = new PainBoxSession(p, seed, volume, HumanoidRagdoll.RegionCount, hold, withdraw, (int)Region.HandR);
_session = new PainBoxSession(p, seed, volume, senses, hold, withdraw, _handR);
}
catch
{
@ -181,7 +191,7 @@ public sealed class BodyPainBoxSession : IDisposable
var events = TickEvents.None;
if (ticked)
{
_ragdoll.ReadRegions(_x, _y, _z);
_ragdoll.ReadSensePoints(_x, _y, _z);
events = _session.Tick(_x, _y, _z, _strengths, _targets);
if (events != TickEvents.None) _events.Enqueue(new BodyEvent(tick, events));
}
@ -268,8 +278,8 @@ public sealed class BodyPainBoxSession : IDisposable
private HandSample ReadHand()
{
_ragdoll.ReadRegions(_x, _y, _z);
float hx = _x[(int)Region.HandR], hy = _y[(int)Region.HandR], hz = _z[(int)Region.HandR];
_ragdoll.ReadSensePoints(_x, _y, _z);
float hx = _x[_handR], hy = _y[_handR], hz = _z[_handR];
return new HandSample(hx, hy, hz, _session.Box.Contains(hx, hy, hz));
}
}

View file

@ -63,31 +63,33 @@ public static class OfficeBody
var needs = SeatedPuppet.Needs;
using var world = new Box3DSoaWorld(capacity: needs.Bodies, jointCapacity: needs.Joints);
var ragdoll = SeatedPuppet.Build(world);
var x = new float[HumanoidRagdoll.RegionCount];
var y = new float[HumanoidRagdoll.RegionCount];
var z = new float[HumanoidRagdoll.RegionCount];
ragdoll.ReadRegions(x, y, z);
int head = (int)Region.Head;
var plan = ragdoll.Plan;
var x = new float[plan.SensePoints.Count];
var y = new float[plan.SensePoints.Count];
var z = new float[plan.SensePoints.Count];
ragdoll.ReadSensePoints(x, y, z);
int head = plan.SenseIndex("Head");
int handLIdx = plan.SenseIndex("Hand", Side.Left), handRIdx = plan.SenseIndex("Hand", Side.Right);
// The desk's emitters sit where the head does and are sensed there.
var session = new StressSession(p with { Setting = p.Setting.Shifted(x[head], y[head], z[head]) }, seed,
HumanoidRagdoll.RegionCount, Poses, head);
plan.SensePoints.Count, Poses, head);
var strengths = new float[HumanoidRagdoll.MuscleCount];
var targets = new Quat[HumanoidRagdoll.MuscleCount];
var strengths = new float[plan.Joints.Count];
var targets = new Quat[plan.Joints.Count];
var handL = new List<BodyPoint>();
var handR = new List<BodyPoint>();
var pelvis = new List<BodyPoint>();
int pelvisLane = ragdoll.BodyLane(Limb.Pelvis);
int pelvisLane = ragdoll.BodyLane(plan.PartIndex("Pelvis"));
while (!session.Finished)
{
ragdoll.ReadRegions(x, y, z);
ragdoll.ReadSensePoints(x, y, z);
session.Tick(x, y, z, strengths, targets);
ragdoll.Drive(strengths, targets);
world.Step((float)p.TickSeconds, SeatedPuppet.Substeps);
ragdoll.ReadRegions(x, y, z);
handL.Add(new BodyPoint(x[(int)Region.HandL], y[(int)Region.HandL], z[(int)Region.HandL]));
handR.Add(new BodyPoint(x[(int)Region.HandR], y[(int)Region.HandR], z[(int)Region.HandR]));
ragdoll.ReadSensePoints(x, y, z);
handL.Add(new BodyPoint(x[handLIdx], y[handLIdx], z[handLIdx]));
handR.Add(new BodyPoint(x[handRIdx], y[handRIdx], z[handRIdx]));
var v = world.View;
pelvis.Add(new BodyPoint(v.PositionX[pelvisLane], v.PositionY[pelvisLane], v.PositionZ[pelvisLane]));
}
@ -114,20 +116,22 @@ public static class OfficeBody
private static OfficePoses BuildPoses()
{
Quat[] Pose() { var q = new Quat[HumanoidRagdoll.MuscleCount]; Array.Fill(q, Quat.Identity); return q; }
var plan = SeatedPuppet.Plan;
Quat[] Pose() { var q = new Quat[plan.Joints.Count]; Array.Fill(q, Quat.Identity); return q; }
var composed = Pose();
var keys = new[] { Pose(), Pose(), Pose(), Pose() };
// Key order: wind-up left, strike left, wind-up right, strike right. The arm not striking stays wound up.
foreach (var k in keys) { Arm(k, left: true, strike: false); Arm(k, left: false, strike: false); }
Arm(keys[1], left: true, strike: true);
Arm(keys[3], left: false, strike: true);
foreach (var k in keys) { Arm(plan, k, left: true, strike: false); Arm(plan, k, left: false, strike: false); }
Arm(plan, keys[1], left: true, strike: true);
Arm(plan, keys[3], left: false, strike: true);
return new OfficePoses(composed, Pose(), keys);
}
private static void Arm(Quat[] pose, bool left, bool strike)
private static void Arm(BodyPlan plan, Quat[] pose, bool left, bool strike)
{
pose[(int)(left ? Muscle.ShoulderL : Muscle.ShoulderR)] = Quat.FromAxisAngle(1f, 0f, 0f, strike ? -1.5f : -0.5f);
pose[(int)(left ? Muscle.ElbowL : Muscle.ElbowR)] = Quat.FromAxisAngle(0f, 0f, 1f, strike ? 0f : -2.2f);
var side = left ? Side.Left : Side.Right;
pose[plan.JointIndex("Shoulder", side)] = Quat.FromAxisAngle(1f, 0f, 0f, strike ? -1.5f : -0.5f);
pose[plan.JointIndex("Elbow", side)] = Quat.FromAxisAngle(0f, 0f, 1f, strike ? 0f : -2.2f);
}
}

View file

@ -1,3 +1,4 @@
using Lodur;
using Lodur.Body.Ymir;
using Ymir.Box3D;
@ -14,15 +15,18 @@ public static class SeatedPuppet
public const int Substeps = 4;
/// <summary>Bodies and joints a world needs for <see cref="Build"/>: the ragdoll, the ground and the stool, and the weld.</summary>
public static (int Bodies, int Joints) Needs => (HumanoidRagdoll.LimbCount + 2, HumanoidRagdoll.MuscleCount + 1);
public static (int Bodies, int Joints) Needs => (Ragdoll.Needs(Plan).Bodies + 2, Ragdoll.Needs(Plan).Joints + 1);
/// <summary>The humanoid plan every BDS body is built from; classes resolve their labels against it once.</summary>
public static BodyPlan Plan { get; } = BodyPlans.Humanoid();
/// <summary>Adds the ground, the stool, the ragdoll (stable ids from 100) and the pelvis weld to <paramref name="world"/>, which must be sized by <see cref="Needs"/>.</summary>
public static HumanoidRagdoll Build(Box3DSoaWorld world)
public static Ragdoll Build(Box3DSoaWorld world)
{
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));
var ragdoll = Ragdoll.Build(world, Plan, (0f, 0f), firstStableId: 100);
world.AddJoint(Box3DSoaJointKind.Weld, stool, ragdoll.BodyLane(Plan.PartIndex("Pelvis")), (0f, 1.11f, -0.09f), (0f, 1f, 0f));
return ragdoll;
}
}