Cut 5b-2: Hybrid B centroidal GRF + 2nd-order joint mech locomotion across all 7 walkers

This commit is contained in:
Meta Vens 2026-12-05 06:37:26 +01:00
commit dee2e738d8
9 changed files with 3077 additions and 431 deletions

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using Godot;
using RustToDust.Core;
using RustToDust.Ymir;
namespace RustToDust.Client;
@ -23,8 +24,9 @@ public readonly record struct VehicleRigDriveInput(
/// - Wheeled: wheel spin (<c>v / r</c>), front-axle steering, subtle chassis suspension roll/pitch.
/// - Turrets/Torsos/Arms: yaw tracking (<c>joint.turret_yaw</c>, <c>joint.torso_yaw</c>) &amp; arm pitch.
/// - VTOL: rotor spin scaled by <c>EngineThermalPerf</c> + lift demand, nacelle tilt with acceleration.
/// - Walkers/Mechs: procedural leg stride returning to neutral rest when stopped, Manticore tail aiming,
/// and Breaker's damped oleo strut + 2-link scissor torque links + telescoping recess rams.
/// - Walkers/Mechs: pure FK posing from Hybrid B <see cref="WalkerVisualSnapshot"/> with per-segment
/// rest-basis alignment (<c>Q_offset</c>), Manticore tail aiming, and Breaker's real GRF-driven
/// oleo strut + 2-link scissor torque links + telescoping recess rams.
/// </summary>
public static class VehicleRigAnimator
{
@ -73,10 +75,27 @@ public static class VehicleRigAnimator
public readonly List<RamBinding> Rams = new();
}
public sealed class WalkerFootSample
public sealed class SegmentBinding
{
public required Node3D[] ChainFromModelRoot;
public required Vector3[] LocalPoints;
public required Node3D Node;
public required Transform3D RestLocal;
public required Node3D[] IntermediateBefore;
public required Basis QOffset;
}
public sealed class WalkerLegBinding
{
public SegmentBinding? Coxa;
public required SegmentBinding Thigh;
public required SegmentBinding Shin;
public SegmentBinding? Cannon;
public required SegmentBinding Foot;
}
public sealed class WalkerVisualRig
{
public required WalkerGeometrySpec Geom;
public required WalkerLegBinding[] Legs;
}
public sealed class RigState
@ -87,15 +106,12 @@ public static class VehicleRigAnimator
public float ArmPitchRad;
public float RotorSpinRad;
public float NacelleTiltRad;
public float StridePhaseRad;
public float StrideAmp;
public float PrevForwardSpeed;
public float PrevVelY;
public float ChassisPitchRad;
public float ChassisRollRad;
public float ElapsedSeconds;
public BreakerMechRig? BreakerRig;
public List<WalkerFootSample>? FootSamples;
public WalkerVisualRig? WalkerRig;
}
public static void Update(
@ -105,6 +121,25 @@ public static class VehicleRigAnimator
Basis worldBasis,
float dt,
bool snap = false)
{
var input = BuildContenderInput(c, velocity, dt);
Update(visual, in input, worldBasis, snap);
}
public static void Update(
VehicleVisualInstance visual,
ContenderState c,
Vector3 velocity,
Basis worldBasis,
float dt,
in WalkerVisualSnapshot walkerSnap,
bool snap = false)
{
var input = BuildContenderInput(c, velocity, dt);
Update(visual, in input, worldBasis, in walkerSnap, snap);
}
private static VehicleRigDriveInput BuildContenderInput(ContenderState c, Vector3 velocity, float dt)
{
float aimYawWorld = c.Yaw;
float dx = c.AimX - c.X;
@ -125,7 +160,7 @@ public static class VehicleRigAnimator
}
}
var input = new VehicleRigDriveInput(
return new VehicleRigDriveInput(
Velocity: velocity,
Yaw: c.Yaw,
AimYawWorld: aimYawWorld,
@ -136,8 +171,6 @@ public static class VehicleRigAnimator
EngineThermalPerf: c.Grid.ComputeEngineThermalPerformance(),
DeltaTime: dt,
IsGrounded: !c.Grid.Spec.IsVtol);
Update(visual, in input, worldBasis, snap);
}
public static void Update(
@ -145,6 +178,29 @@ public static class VehicleRigAnimator
in VehicleRigDriveInput input,
Basis worldBasis,
bool snap = false)
{
UpdateCore(visual, in input, worldBasis, hasWalkerSnap: false, in DefaultSnapshot, snap);
}
public static void Update(
VehicleVisualInstance visual,
in VehicleRigDriveInput input,
Basis worldBasis,
in WalkerVisualSnapshot walkerSnap,
bool snap = false)
{
UpdateCore(visual, in input, worldBasis, hasWalkerSnap: true, in walkerSnap, snap);
}
private static readonly WalkerVisualSnapshot DefaultSnapshot = default;
private static void UpdateCore(
VehicleVisualInstance visual,
in VehicleRigDriveInput input,
Basis worldBasis,
bool hasWalkerSnap,
in WalkerVisualSnapshot walkerSnap,
bool snap)
{
float dt = Sanitize(input.DeltaTime, 0f, 0f, 0.1f);
if (!snap && dt <= 0f)
@ -170,7 +226,6 @@ public static class VehicleRigAnimator
float forwardSpeed = vel.Dot(fwd);
float lateralSpeed = vel.Dot(right);
float horizSpeed = MathF.Sqrt(forwardSpeed * forwardSpeed + lateralSpeed * lateralSpeed);
float relYaw = MatchRules.NormalizeAngle(aimYawWorld - yaw);
float aimPitchRad = Mathf.DegToRad(aimPitchDeg);
@ -194,7 +249,14 @@ public static class VehicleRigAnimator
break;
case MobilityKind.Walker:
UpdateWalker(visual, st, horizSpeed, forwardSpeed, vel.Y, steerCmd, relYaw, aimPitchRad, dt, blendMed);
if (hasWalkerSnap)
{
UpdateWalkerFromSnapshot(visual, st, in walkerSnap, relYaw, aimPitchRad, dt, blendMed);
}
else if (string.Equals(visual.ModelId, "manticore", StringComparison.OrdinalIgnoreCase))
{
AnimateManticoreTail(visual, st, relYaw, aimPitchRad, blendMed);
}
break;
case MobilityKind.Tracked:
@ -203,7 +265,6 @@ public static class VehicleRigAnimator
}
st.PrevForwardSpeed = forwardSpeed;
st.PrevVelY = vel.Y;
}
private static void UpdateTurretAndTorso(
@ -257,7 +318,6 @@ public static class VehicleRigAnimator
float dt,
float blend)
{
// Determine average wheel radius from wheel joint Y height in grounded GLB
float radiusSum = 0f;
int radiusCount = 0;
foreach (var (jid, j) in visual.Joints)
@ -296,7 +356,6 @@ public static class VehicleRigAnimator
}
}
// Subtle chassis suspension pitch & roll
float accel = dt > 1e-4f ? (forwardSpeed - st.PrevForwardSpeed) / dt : 0f;
float targetPitch = Math.Clamp(-accel * 0.0022f, -0.05f, 0.05f);
float targetRoll = Math.Clamp(lateralSpeed * 0.0045f + steerCmd * MathF.Min(1f, MathF.Abs(forwardSpeed) / 14f) * 0.035f, -0.06f, 0.06f);
@ -366,196 +425,348 @@ public static class VehicleRigAnimator
}
}
private static void UpdateWalker(
private static void UpdateWalkerFromSnapshot(
VehicleVisualInstance visual,
RigState st,
float horizSpeed,
float forwardSpeed,
float velY,
float steerCmd,
in WalkerVisualSnapshot snap,
float relYaw,
float aimPitchRad,
float dt,
float blend)
{
EnsureWalkerFootSamples(visual, st);
float targetAmp = Math.Clamp(horizSpeed / 6.5f + MathF.Abs(steerCmd) * 0.32f, 0f, 1f);
st.StrideAmp += (targetAmp - st.StrideAmp) * blend;
float strideDir = forwardSpeed >= -0.35f ? 1f : -1f;
float phaseRate = (2.8f + horizSpeed * 0.48f + MathF.Abs(steerCmd) * 1.4f) * strideDir;
st.StridePhaseRad = MathF.IEEERemainder(st.StridePhaseRad + phaseRate * dt, MathF.Tau);
float phiL = st.StridePhaseRad;
float phiR = st.StridePhaseRad + MathF.PI;
string id = visual.ModelId.ToLowerInvariant();
switch (id)
{
case "breaker":
AnimateBipedLeg(visual, "LegL.Hip", "LegL.Shin", "LegL.Foot", phiL, st.StrideAmp, signX: -1f);
AnimateBipedLeg(visual, "LegR.Hip", "LegR.Shin", "LegR.Foot", phiR, st.StrideAmp, signX: -1f);
ApplyPelvisBob(visual, "Pelvis", st.StridePhaseRad, st.StrideAmp, bobScale: 0.06f, rollScale: 0.024f);
StepBreakerMechanisms(visual, st, phiL, phiR, velY, dt);
ApplyWalkerFloorClearance(visual, st, "Pelvis");
break;
case "paladin":
AnimateBipedVehicleX(visual, "LegL.Upper", "LegL.Lower", "LegL.Ankle", phiL, st.StrideAmp, hipAmp: 0.22f, kneeLiftAmp: 0.28f, kneeForwardFlexAmp: 0.08f);
AnimateBipedVehicleX(visual, "LegR.Upper", "LegR.Lower", "LegR.Ankle", phiR, st.StrideAmp, hipAmp: 0.22f, kneeLiftAmp: 0.28f, kneeForwardFlexAmp: 0.08f);
ApplyPelvisBob(visual, "Pelvis", st.StridePhaseRad, st.StrideAmp, bobScale: 0.07f, rollScale: 0.020f);
ApplyWalkerFloorClearance(visual, st, "Pelvis");
break;
case "raptor":
AnimateBipedVehicleX(visual, "LegL.Thigh", "LegL.Shin", "LegL.Cannon", phiL, st.StrideAmp, hipAmp: 0.20f, kneeLiftAmp: 0.34f, kneeForwardFlexAmp: 0.22f);
AnimateBipedVehicleX(visual, "LegR.Thigh", "LegR.Shin", "LegR.Cannon", phiR, st.StrideAmp, hipAmp: 0.20f, kneeLiftAmp: 0.34f, kneeForwardFlexAmp: 0.22f);
ApplyPelvisBob(visual, "Pelvis", st.StridePhaseRad, st.StrideAmp, bobScale: 0.06f, rollScale: 0.020f);
ApplyWalkerFloorClearance(visual, st, "Pelvis");
break;
case "spectre":
AnimateBipedVehicleX(visual, "LegL.Thigh", "LegL.Shin", "LegL.Cannon", phiL, st.StrideAmp, hipAmp: 0.22f, kneeLiftAmp: 0.32f, kneeForwardFlexAmp: 0f);
AnimateBipedVehicleX(visual, "LegR.Thigh", "LegR.Shin", "LegR.Cannon", phiR, st.StrideAmp, hipAmp: 0.22f, kneeLiftAmp: 0.32f, kneeForwardFlexAmp: 0f);
ApplyPelvisBob(visual, "Pelvis", st.StridePhaseRad, st.StrideAmp, bobScale: 0.06f, rollScale: 0.020f);
ApplyWalkerFloorClearance(visual, st, "Pelvis");
break;
case "kestrel":
// Kestrel leg meshes are baked under Pelvis; animate Pelvis stride bob/sway with floor clearance
ApplyPelvisBob(visual, "Pelvis", st.StridePhaseRad, st.StrideAmp, bobScale: 0.07f, rollScale: 0.024f);
ApplyWalkerFloorClearance(visual, st, "Pelvis");
break;
case "manticore":
AnimateManticoreLeg(visual, "LegFL.S1", "LegFL.S2", "FootFL", phiL, st.StrideAmp, isFront: true);
AnimateManticoreLeg(visual, "LegBR.S1", "LegBR.S2", "FootBR", phiL, st.StrideAmp, isFront: false);
AnimateManticoreLeg(visual, "LegFR.S1", "LegFR.S2", "FootFR", phiR, st.StrideAmp, isFront: true);
AnimateManticoreLeg(visual, "LegBL.S1", "LegBL.S2", "FootBL", phiR, st.StrideAmp, isFront: false);
ApplyPelvisBob(visual, "Hull", st.StridePhaseRad, st.StrideAmp, bobScale: 0.04f, rollScale: 0.018f);
ApplyWalkerFloorClearance(visual, st, "Hull");
AnimateManticoreTail(visual, st, relYaw, aimPitchRad, blend);
break;
case "bastion":
AnimateBastionLeg(visual, "Thigh.fl", "Shin.fl", "Ankle.fl", phiL, st.StrideAmp, isFront: true);
AnimateBastionLeg(visual, "Thigh.rr", "Shin.rr", "Ankle.rr", phiL, st.StrideAmp, isFront: false);
AnimateBastionLeg(visual, "Thigh.fr", "Shin.fr", "Ankle.fr", phiR, st.StrideAmp, isFront: true);
AnimateBastionLeg(visual, "Thigh.rl", "Shin.rl", "Ankle.rl", phiR, st.StrideAmp, isFront: false);
ApplyPelvisBob(visual, "Trunk", st.StridePhaseRad, st.StrideAmp, bobScale: 0.05f, rollScale: 0.012f);
ApplyWalkerFloorClearance(visual, st, "Trunk");
break;
}
}
private static float SwingLift(float phase) => MathF.Max(0f, MathF.Cos(phase));
private static void AnimateBipedLeg(
VehicleVisualInstance visual,
string hipName,
string shinName,
string footName,
float phase,
float amp,
float signX)
{
float hipAng = MathF.Sin(phase) * 0.30f * amp;
float kneeAng = MathF.Max(0f, -MathF.Cos(phase)) * 0.38f * amp;
float footAng = -(hipAng * 0.55f + kneeAng * 0.45f);
RotateVehicleAxis(visual, hipName, Vector3.Right, hipAng * signX);
RotateVehicleAxis(visual, shinName, Vector3.Right, kneeAng * signX);
RotateVehicleAxis(visual, footName, Vector3.Right, footAng * signX);
}
private static void AnimateBipedVehicleX(
VehicleVisualInstance visual,
string thighName,
string shinName,
string footName,
float phase,
float amp,
float hipAmp,
float kneeLiftAmp,
float kneeForwardFlexAmp)
{
float hipAng = -MathF.Sin(phase) * hipAmp * amp;
float kneeAng = (SwingLift(phase) * kneeLiftAmp + MathF.Max(0f, MathF.Sin(phase)) * kneeForwardFlexAmp) * amp;
float footAng = -(hipAng + kneeAng);
RotateVehicleAxis(visual, thighName, Vector3.Right, hipAng);
RotateVehicleAxis(visual, shinName, Vector3.Right, kneeAng);
RotateVehicleAxis(visual, footName, Vector3.Right, footAng);
}
private static void AnimateManticoreLeg(
VehicleVisualInstance visual,
string s1Name,
string s2Name,
string footName,
float phase,
float amp,
bool isFront)
{
float s2Ang = -MathF.Sin(phase) * 0.22f * amp;
float liftSign = isFront ? -1f : 1f;
float s1Ang = SwingLift(phase) * 0.26f * amp * liftSign;
float footAng = -(s1Ang + s2Ang);
RotateVehicleAxis(visual, s1Name, Vector3.Right, s1Ang);
RotateVehicleAxis(visual, s2Name, Vector3.Right, s2Ang);
RotateVehicleAxis(visual, footName, Vector3.Right, footAng);
}
private static void AnimateBastionLeg(
VehicleVisualInstance visual,
string thighName,
string shinName,
string ankleName,
float phase,
float amp,
bool isFront)
{
float thAng;
float shAng;
if (isFront)
{
thAng = -MathF.Sin(phase) * 0.16f * amp;
shAng = SwingLift(phase) * 0.14f * amp;
}
else
{
thAng = (MathF.Max(0f, -MathF.Sin(phase)) * 0.14f + SwingLift(phase) * 0.10f) * amp;
shAng = (-MathF.Sin(phase) * 0.16f - SwingLift(phase) * 0.16f) * amp;
}
float anAng = -(thAng + shAng);
RotateVehicleAxis(visual, thighName, Vector3.Right, thAng);
RotateVehicleAxis(visual, shinName, Vector3.Right, shAng);
RotateVehicleAxis(visual, ankleName, Vector3.Right, anAng);
}
private static void ApplyPelvisBob(
VehicleVisualInstance visual,
string rootNodeName,
float phase,
float amp,
float bobScale,
float rollScale = 0.024f)
{
if (visual.GetNamedNode(rootNodeName) is not { } node ||
!visual.TryGetRestLocal(rootNodeName, out var rest))
var rig = st.WalkerRig ??= InitWalkerRig(visual);
if (rig is null)
{
return;
}
float bobY = MathF.Abs(MathF.Sin(phase)) * bobScale * amp;
float swayRoll = MathF.Cos(phase) * rollScale * amp;
Basis roll = new Basis(Vector3.Back, swayRoll);
node.Transform = new Transform3D(roll * rest.Basis, rest.Origin + new Vector3(0f, bobY, 0f));
var geom = rig.Geom;
var bodyRotSys = snap.ComRot.LengthSquared() < 1e-6f
? System.Numerics.Quaternion.Identity
: System.Numerics.Quaternion.Normalize(snap.ComRot);
var bodyRotInv = System.Numerics.Quaternion.Conjugate(bodyRotSys);
int count = Math.Min(rig.Legs.Length, snap.LegCount);
for (int i = 0; i < count; i++)
{
var legRig = rig.Legs[i];
var legSpec = geom.Legs[i];
var ang = snap.LegAngles[i];
if (!float.IsFinite(ang.HipPitch) || !float.IsFinite(ang.HipRoll) ||
!float.IsFinite(ang.KneeFlex) || !float.IsFinite(ang.HockFlex) ||
!float.IsFinite(ang.AnklePitch))
{
continue;
}
Vector3 footNormModel = ToGodot(System.Numerics.Vector3.Transform(snap.FootNormal[i], bodyRotInv));
footNormModel = footNormModel.LengthSquared() > 1e-6f ? footNormModel.Normalized() : Vector3.Up;
Vector3 r0 = ToGodot(legSpec.LegRight0);
Vector3 f0 = ToGodot(legSpec.LegFwd0);
Basis curModelBasis = Basis.Identity;
if (snap.IsArachnidUp)
{
float coxaYaw = ang.HipRoll;
Vector3 legOut = (f0 * MathF.Cos(coxaYaw) + r0 * MathF.Sin(coxaYaw)).Normalized();
Vector3 legRight = (r0 * MathF.Cos(coxaYaw) - f0 * MathF.Sin(coxaYaw)).Normalized();
if (legRig.Coxa is { } coxaSeg)
{
Basis coxaBasis = BasisFromDownAndRight(-legOut, Vector3.Up.Cross(legOut)) * coxaSeg.QOffset;
curModelBasis = ApplySegmentModelBasis(coxaSeg, curModelBasis, coxaBasis);
}
Vector3 s1Dir = (legOut * MathF.Cos(ang.HipPitch) + Vector3.Up * MathF.Sin(ang.HipPitch)).Normalized();
float s2Elev = ang.HipPitch - ang.KneeFlex;
Vector3 s2Dir = (legOut * MathF.Cos(s2Elev) + Vector3.Up * MathF.Sin(s2Elev)).Normalized();
Basis thighBasis = BasisFromDownAndRight(s1Dir, legRight) * legRig.Thigh.QOffset;
curModelBasis = ApplySegmentModelBasis(legRig.Thigh, curModelBasis, thighBasis);
Basis shinBasis = BasisFromDownAndRight(s2Dir, legRight) * legRig.Shin.QOffset;
curModelBasis = ApplySegmentModelBasis(legRig.Shin, curModelBasis, shinBasis);
float lat = Math.Clamp(footNormModel.Dot(legRight), -0.85f, 0.85f);
float planarMag = MathF.Sqrt(MathF.Max(0.01f, 1f - lat * lat));
Vector3 footUp = (legRight * lat + (Vector3.Up * MathF.Cos(ang.AnklePitch) + legOut * MathF.Sin(ang.AnklePitch)) * planarMag).Normalized();
Basis footBasis = BasisFromDownAndRight(-footUp, legRight) * legRig.Foot.QOffset;
ApplySegmentModelBasis(legRig.Foot, curModelBasis, footBasis);
}
else
{
float hipRoll = ang.HipRoll;
Vector3 legRight = (r0 * MathF.Cos(hipRoll) + Vector3.Up * MathF.Sin(hipRoll)).Normalized();
Vector3 downTilted = (-Vector3.Up * MathF.Cos(hipRoll) + r0 * MathF.Sin(hipRoll)).Normalized();
Vector3 thighDir = (downTilted * MathF.Cos(ang.HipPitch) + f0 * MathF.Sin(ang.HipPitch)).Normalized();
float shinAngle = ang.HipPitch - ang.KneeFlex;
Vector3 shinDir = (downTilted * MathF.Cos(shinAngle) + f0 * MathF.Sin(shinAngle)).Normalized();
Basis thighBasis = BasisFromDownAndRight(thighDir, legRight) * legRig.Thigh.QOffset;
curModelBasis = ApplySegmentModelBasis(legRig.Thigh, curModelBasis, thighBasis);
Basis shinBasis = BasisFromDownAndRight(shinDir, legRight) * legRig.Shin.QOffset;
curModelBasis = ApplySegmentModelBasis(legRig.Shin, curModelBasis, shinBasis);
if (snap.IsDigitigrade && legRig.Cannon is { } cannonSeg)
{
float cannonAngle = shinAngle + ang.HockFlex;
Vector3 cannonDir = (downTilted * MathF.Cos(cannonAngle) + f0 * MathF.Sin(cannonAngle)).Normalized();
Basis cannonBasis = BasisFromDownAndRight(cannonDir, legRight) * cannonSeg.QOffset;
curModelBasis = ApplySegmentModelBasis(cannonSeg, curModelBasis, cannonBasis);
}
float lat = Math.Clamp(footNormModel.Dot(legRight), -0.85f, 0.85f);
float planarMag = MathF.Sqrt(MathF.Max(0.01f, 1f - lat * lat));
Vector3 footUp = (legRight * lat + (Vector3.Up * MathF.Cos(ang.AnklePitch) + f0 * MathF.Sin(ang.AnklePitch)) * planarMag).Normalized();
Basis footBasis = BasisFromDownAndRight(-footUp, legRight) * legRig.Foot.QOffset;
ApplySegmentModelBasis(legRig.Foot, curModelBasis, footBasis);
}
}
string id = visual.ModelId;
if (string.Equals(id, "manticore", StringComparison.OrdinalIgnoreCase))
{
AnimateManticoreTail(visual, st, relYaw, aimPitchRad, blend);
}
else if (string.Equals(id, "breaker", StringComparison.OrdinalIgnoreCase))
{
StepBreakerLinkages(visual, snap.LoadG[0], snap.LoadG[1], dt);
}
}
private static Basis ApplySegmentModelBasis(SegmentBinding seg, Basis curBasisInModel, Basis targetBasisInModel)
{
for (int i = 0; i < seg.IntermediateBefore.Length; i++)
{
curBasisInModel *= seg.IntermediateBefore[i].Transform.Basis.Orthonormalized();
}
Basis localBasis = curBasisInModel.Inverse() * targetBasisInModel;
seg.Node.Transform = new Transform3D(localBasis, seg.RestLocal.Origin);
return targetBasisInModel;
}
private static WalkerVisualRig? InitWalkerRig(VehicleVisualInstance visual)
{
var geom = WalkerGeometrySpec.GetForChassis(visual.ModelId);
string id = visual.ModelId.ToLowerInvariant();
var legs = new WalkerLegBinding[geom.LegCount];
for (int i = 0; i < geom.LegCount; i++)
{
string? coxaName = null;
string thighName, shinName, footName;
string? cannonName = null;
switch (id)
{
case "breaker":
{
string s = i == 0 ? "L" : "R";
thighName = $"Leg{s}.Hip";
shinName = $"Leg{s}.Shin";
footName = $"Leg{s}.Foot";
break;
}
case "paladin":
{
string s = i == 0 ? "L" : "R";
thighName = $"Leg{s}.Upper";
shinName = $"Leg{s}.Lower";
footName = $"Leg{s}.Ankle";
break;
}
case "raptor":
case "exemplar_frame":
{
string s = i == 0 ? "L" : "R";
thighName = $"Leg{s}.Thigh";
shinName = $"Leg{s}.Shin";
cannonName = $"Leg{s}.Cannon";
footName = $"Foot{s}";
break;
}
case "spectre":
{
string s = i == 0 ? "L" : "R";
thighName = $"Leg{s}.Thigh";
shinName = $"Leg{s}.Shin";
cannonName = $"Leg{s}.Cannon";
footName = $"Leg{s}.Ankle";
break;
}
case "kestrel":
{
string s = i == 0 ? "L" : "R";
thighName = $"Leg{s}.Thigh";
shinName = $"Leg{s}.Shin";
footName = $"Leg{s}.Foot";
break;
}
case "manticore":
{
string q = i switch { 0 => "FL", 1 => "FR", 2 => "BL", _ => "BR" };
coxaName = $"Leg{q}.Coxa";
thighName = $"Leg{q}.S1";
shinName = $"Leg{q}.S2";
footName = $"Foot{q}";
break;
}
case "bastion":
{
string q = i switch { 0 => "fl", 1 => "fr", 2 => "rl", _ => "rr" };
thighName = $"Thigh.{q}";
shinName = $"Shin.{q}";
footName = $"Ankle.{q}";
break;
}
default:
return null;
}
if (visual.GetNamedNode(thighName) is not { } thighNode ||
visual.GetNamedNode(shinName) is not { } shinNode ||
visual.GetNamedNode(footName) is not { } footNode ||
!visual.TryGetRestLocal(thighName, out var restThigh) ||
!visual.TryGetRestLocal(shinName, out var restShin) ||
!visual.TryGetRestLocal(footName, out var restFoot) ||
!visual.TryGetRestModel(thighName, out var modelThigh) ||
!visual.TryGetRestModel(shinName, out var modelShin) ||
!visual.TryGetRestModel(footName, out var modelFoot))
{
return null;
}
Node3D? cannonNode = null;
Transform3D restCannon = Transform3D.Identity;
Transform3D modelCannon = Transform3D.Identity;
if (cannonName is not null)
{
if (visual.GetNamedNode(cannonName) is not { } cn ||
!visual.TryGetRestLocal(cannonName, out restCannon) ||
!visual.TryGetRestModel(cannonName, out modelCannon))
{
return null;
}
cannonNode = cn;
}
Node3D? coxaNode = null;
Transform3D restCoxa = Transform3D.Identity;
Transform3D modelCoxa = Transform3D.Identity;
if (coxaName is not null &&
visual.GetNamedNode(coxaName) is { } cxn &&
visual.TryGetRestLocal(coxaName, out restCoxa) &&
visual.TryGetRestModel(coxaName, out modelCoxa))
{
coxaNode = cxn;
}
Vector3 dirRestThigh = (modelShin.Origin - modelThigh.Origin).Normalized();
Vector3 shinEndOrigin = cannonNode is not null ? modelCannon.Origin : modelFoot.Origin;
Vector3 dirRestShin = (shinEndOrigin - modelShin.Origin).Normalized();
Vector3 dirRestCannon = cannonNode is not null
? (modelFoot.Origin - modelCannon.Origin).Normalized()
: Vector3.Down;
Vector3 legRightRest = geom.IsArachnidUp
? dirRestThigh.Cross(Vector3.Up).Normalized()
: ToGodot(geom.Legs[i].LegRight0);
SegmentBinding? coxaBinding = null;
if (coxaNode is not null)
{
Vector3 rad0 = new(modelFoot.Origin.X - modelThigh.Origin.X, 0f, modelFoot.Origin.Z - modelThigh.Origin.Z);
rad0 = rad0.LengthSquared() > 1e-6f ? rad0.Normalized() : ToGodot(geom.Legs[i].LegFwd0);
Basis fCoxaRest = BasisFromDownAndRight(-rad0, Vector3.Up.Cross(rad0));
coxaBinding = new SegmentBinding
{
Node = coxaNode,
RestLocal = restCoxa,
IntermediateBefore = BuildIntermediateChain(visual.ModelRoot, coxaNode),
QOffset = fCoxaRest.Inverse() * modelCoxa.Basis.Orthonormalized()
};
}
var thighBinding = new SegmentBinding
{
Node = thighNode,
RestLocal = restThigh,
IntermediateBefore = BuildIntermediateChain(coxaNode ?? visual.ModelRoot, thighNode),
QOffset = BasisFromDownAndRight(dirRestThigh, legRightRest).Inverse() * modelThigh.Basis.Orthonormalized()
};
var shinBinding = new SegmentBinding
{
Node = shinNode,
RestLocal = restShin,
IntermediateBefore = BuildIntermediateChain(thighNode, shinNode),
QOffset = BasisFromDownAndRight(dirRestShin, legRightRest).Inverse() * modelShin.Basis.Orthonormalized()
};
SegmentBinding? cannonBinding = null;
if (cannonNode is not null)
{
cannonBinding = new SegmentBinding
{
Node = cannonNode,
RestLocal = restCannon,
IntermediateBefore = BuildIntermediateChain(shinNode, cannonNode),
QOffset = BasisFromDownAndRight(dirRestCannon, legRightRest).Inverse() * modelCannon.Basis.Orthonormalized()
};
}
var footBinding = new SegmentBinding
{
Node = footNode,
RestLocal = restFoot,
IntermediateBefore = BuildIntermediateChain(cannonNode ?? shinNode, footNode),
QOffset = BasisFromDownAndRight(Vector3.Down, legRightRest).Inverse() * modelFoot.Basis.Orthonormalized()
};
legs[i] = new WalkerLegBinding
{
Coxa = coxaBinding,
Thigh = thighBinding,
Shin = shinBinding,
Cannon = cannonBinding,
Foot = footBinding
};
}
return new WalkerVisualRig
{
Geom = geom,
Legs = legs
};
}
private static Node3D[] BuildIntermediateChain(Node3D ancestorExclusive, Node3D childExclusive)
{
var list = new List<Node3D>(4);
for (Node? cur = childExclusive.GetParent(); cur is Node3D n3d && !ReferenceEquals(cur, ancestorExclusive); cur = cur.GetParent())
{
list.Add(n3d);
}
list.Reverse();
return list.ToArray();
}
private static Basis BasisFromDownAndRight(Vector3 downDir, Vector3 rightX)
{
Vector3 upY = -downDir.Normalized();
Vector3 x = rightX - upY * rightX.Dot(upY);
x = x.LengthSquared() > 1e-6f ? x.Normalized() : Vector3.Right;
Vector3 z = x.Cross(upY).Normalized();
x = upY.Cross(z).Normalized();
return new Basis(x, upY, z);
}
private static Vector3 ToGodot(System.Numerics.Vector3 v) => new(v.X, v.Y, v.Z);
private static void AnimateManticoreTail(
VehicleVisualInstance visual,
RigState st,
@ -591,12 +802,14 @@ public static class VehicleRigAnimator
}
}
private static void StepBreakerMechanisms(
/// <summary>
/// Steps Breaker's real oleo gas-spring struts, 2-link scissor torque knees, and telescoping recess rams
/// from real Newtonian GRF per-leg load factors (<c>grfN / (0.5f * weightN)</c>).
/// </summary>
public static void StepBreakerLinkages(
VehicleVisualInstance visual,
RigState st,
float phiL,
float phiR,
float velY,
float leftLoadG,
float rightLoadG,
float dt)
{
VehicleMechanismsRecord? mech = visual.Package.Mechanisms;
@ -605,15 +818,15 @@ public static class VehicleRigAnimator
return;
}
RigState st = visual.RigState;
BreakerMechRig rig = st.BreakerRig ??= InitBreakerRig(visual, mech);
if (!rig.Initialized)
{
return;
}
float accelY = dt > 1e-4f ? Math.Clamp((velY - st.PrevVelY) / dt, -25f, 25f) : 0f;
float strideLoadL = 1.0f + 0.42f * MathF.Sin(phiL) * st.StrideAmp + MathF.Max(-0.6f, accelY * 0.05f);
float strideLoadR = 1.0f + 0.42f * MathF.Sin(phiR) * st.StrideAmp + MathF.Max(-0.6f, accelY * 0.05f);
float strideLoadL = Math.Clamp(leftLoadG, 0f, 3.5f);
float strideLoadR = Math.Clamp(rightLoadG, 0f, 3.5f);
// 1. Oleo gas-spring + asymmetric orifice damping (Brokkr mechrig.Model.strut_step)
if (dt > 0f)
@ -664,7 +877,7 @@ public static class VehicleRigAnimator
Vector3 bInShin = forkInShin * sb.BInOleoFork;
Vector3 apexInShin = SolveScissorApex(sb.AInShin, bInShin, sb.OutInShin, sb.Arm);
Vector3 dirUp = (apexInShin - sb.AInShin);
Vector3 dirUp = apexInShin - sb.AInShin;
if (dirUp.LengthSquared() > 1e-8f)
{
Quaternion qUp = ShortestArcQuat(sb.DirUp0InShin, dirUp.Normalized());
@ -673,7 +886,7 @@ public static class VehicleRigAnimator
}
Vector3 apexInFork = forkInShin.Inverse() * apexInShin;
Vector3 dirLow = (apexInFork - sb.BInOleoFork);
Vector3 dirLow = apexInFork - sb.BInOleoFork;
if (dirLow.LengthSquared() > 1e-8f)
{
Quaternion qLow = ShortestArcQuat(sb.DirLow0InOleoFork, dirLow.Normalized());
@ -780,7 +993,6 @@ public static class VehicleRigAnimator
foreach (var r in mech.Rams)
{
// Name: "LegL.RecessRam0" -> lug "LegL.RamLugA0", oleo "LegL.Oleo", foot "LegL.Foot"
string side = r.Name.StartsWith("LegR", StringComparison.Ordinal) ? "R" : "L";
char idxChar = r.Name[^1];
string lugName = $"Leg{side}.RamLugA{idxChar}";
@ -850,190 +1062,6 @@ public static class VehicleRigAnimator
j.Node.Transform = new Transform3D(delta * j.RestLocal.Basis, j.RestLocal.Origin);
}
private static void RotateVehicleAxis(VehicleVisualInstance visual, string nodeName, Vector3 vehicleAxis, float angleRad)
{
if (visual.GetNamedNode(nodeName) is not { } node ||
!visual.TryGetRestLocal(nodeName, out var rest))
{
return;
}
Vector3 axisInParent = vehicleAxis;
if (node.GetParent() is Node3D p3 && visual.TryGetRestModel(p3, out var pModel))
{
axisInParent = (pModel.Basis.Orthonormalized().Inverse() * vehicleAxis).Normalized();
}
Basis delta = new Basis(axisInParent, angleRad);
node.Transform = new Transform3D(delta * rest.Basis, rest.Origin);
}
private static void EnsureWalkerFootSamples(VehicleVisualInstance visual, RigState st)
{
if (st.FootSamples is not null)
{
return;
}
var list = new List<WalkerFootSample>();
float cutoffY = visual.Template.MinY + 0.75f;
var parts = visual.StaticParts;
for (int i = 0; i < parts.Count; i++)
{
var part = parts[i];
if (!GodotObject.IsInstanceValid(part.Pivot))
{
continue;
}
// Build chain from ModelRoot -> part.Pivot
var chain = new List<Node3D>();
Transform3D toModel = Transform3D.Identity;
for (Node? cur = part.Pivot; cur is Node3D n3d && !ReferenceEquals(cur, visual.ModelRoot); cur = cur.GetParent())
{
chain.Add(n3d);
toModel = n3d.Transform * toModel;
}
chain.Reverse();
Aabb aabb = part.Mesh.GetAabb();
float aabbMinY = float.PositiveInfinity;
for (int c = 0; c < 8; c++)
{
Vector3 corner = aabb.Position + new Vector3(
(c & 1) != 0 ? aabb.Size.X : 0f,
(c & 2) != 0 ? aabb.Size.Y : 0f,
(c & 4) != 0 ? aabb.Size.Z : 0f);
float wy = (toModel * corner).Y;
if (wy < aabbMinY)
{
aabbMinY = wy;
}
}
if (aabbMinY >= cutoffY)
{
continue;
}
var bestPts = new Vector3[FootSupportDirs.Length];
var bestDots = new float[FootSupportDirs.Length];
for (int d = 0; d < FootSupportDirs.Length; d++)
{
bestDots[d] = float.NegativeInfinity;
}
bool anyVert = false;
int surfCount = part.Mesh.GetSurfaceCount();
for (int s = 0; s < surfCount; s++)
{
var arrays = part.Mesh.SurfaceGetArrays(s);
if (arrays.Count <= (int)Mesh.ArrayType.Vertex ||
arrays[(int)Mesh.ArrayType.Vertex].VariantType != Variant.Type.PackedVector3Array)
{
continue;
}
Vector3[] verts = arrays[(int)Mesh.ArrayType.Vertex].AsVector3Array();
for (int v = 0; v < verts.Length; v++)
{
Vector3 localPt = verts[v];
Vector3 mPt = toModel * localPt;
for (int d = 0; d < FootSupportDirs.Length; d++)
{
float score = mPt.Dot(FootSupportDirs[d]);
if (score > bestDots[d])
{
bestDots[d] = score;
bestPts[d] = localPt;
}
}
anyVert = true;
}
}
if (anyVert)
{
var unique = new List<Vector3>(FootSupportDirs.Length);
for (int d = 0; d < bestPts.Length; d++)
{
bool dup = false;
for (int u = 0; u < unique.Count; u++)
{
if ((unique[u] - bestPts[d]).LengthSquared() < 1e-6f)
{
dup = true;
break;
}
}
if (!dup)
{
unique.Add(bestPts[d]);
}
}
list.Add(new WalkerFootSample
{
ChainFromModelRoot = chain.ToArray(),
LocalPoints = unique.ToArray()
});
}
}
st.FootSamples = list;
}
private static readonly Vector3[] FootSupportDirs =
{
new(0f, -1f, 0f),
new(0.45f, -1f, 0f),
new(-0.45f, -1f, 0f),
new(0f, -1f, 0.45f),
new(0f, -1f, -0.45f),
new(0.45f, -1f, 0.45f),
new(0.45f, -1f, -0.45f),
new(-0.45f, -1f, 0.45f),
new(-0.45f, -1f, -0.45f),
};
private static void ApplyWalkerFloorClearance(VehicleVisualInstance visual, RigState st, string rootNodeName)
{
var samples = st.FootSamples;
if (samples is null || samples.Count == 0 || visual.GetNamedNode(rootNodeName) is not { } rootNode)
{
return;
}
float minModelY = float.PositiveInfinity;
for (int i = 0; i < samples.Count; i++)
{
var s = samples[i];
Transform3D toModel = Transform3D.Identity;
for (int k = 0; k < s.ChainFromModelRoot.Length; k++)
{
toModel *= s.ChainFromModelRoot[k].Transform;
}
for (int p = 0; p < s.LocalPoints.Length; p++)
{
float y = (toModel * s.LocalPoints[p]).Y;
if (y < minModelY)
{
minModelY = y;
}
}
}
float restMinY = visual.Template.MinY;
if (float.IsFinite(minModelY) && minModelY < restMinY)
{
Vector3 liftInModel = new Vector3(0f, restMinY - minModelY, 0f);
Vector3 liftInParent = liftInModel;
if (rootNode.GetParent() is Node3D p3 && visual.TryGetRestModel(p3, out var pModel))
{
liftInParent = pModel.Basis.Orthonormalized().Inverse() * liftInModel;
}
rootNode.Transform = new Transform3D(rootNode.Transform.Basis, rootNode.Transform.Origin + liftInParent);
}
}
private static Quaternion ShortestArcQuat(Vector3 from, Vector3 to)
{
float dot = Math.Clamp(from.Dot(to), -1f, 1f);