Fix Cut 3b Soul Pass #1 vehicle orientation, grounding, rig, halo, and debris findings

This commit is contained in:
Meta Vens 2026-12-04 21:08:50 +01:00
commit 3e90ba2dc3
6 changed files with 750 additions and 95 deletions

View file

@ -73,6 +73,12 @@ public static class VehicleRigAnimator
public readonly List<RamBinding> Rams = new();
}
public sealed class WalkerFootSample
{
public required Node3D[] ChainFromModelRoot;
public required Vector3[] LocalPoints;
}
public sealed class RigState
{
public float WheelSpinRad;
@ -89,6 +95,7 @@ public static class VehicleRigAnimator
public float ChassisRollRad;
public float ElapsedSeconds;
public BreakerMechRig? BreakerRig;
public List<WalkerFootSample>? FootSamples;
}
public static void Update(
@ -96,7 +103,8 @@ public static class VehicleRigAnimator
ContenderState c,
Vector3 velocity,
Basis worldBasis,
float dt)
float dt,
bool snap = false)
{
float aimYawWorld = c.Yaw;
float dx = c.AimX - c.X;
@ -129,15 +137,20 @@ public static class VehicleRigAnimator
DeltaTime: dt,
IsGrounded: !c.Grid.Spec.IsVtol);
Update(visual, in input, worldBasis);
Update(visual, in input, worldBasis, snap);
}
public static void Update(
VehicleVisualInstance visual,
in VehicleRigDriveInput input,
Basis worldBasis)
Basis worldBasis,
bool snap = false)
{
float dt = Sanitize(input.DeltaTime, 0f, 0f, 0.1f);
if (!snap && dt <= 0f)
{
return;
}
Vector3 vel = SanitizeVec(input.Velocity);
float yaw = Sanitize(input.Yaw, 0f, -MathF.Tau * 4f, MathF.Tau * 4f);
float aimYawWorld = Sanitize(input.AimYawWorld, yaw, -MathF.Tau * 4f, MathF.Tau * 4f);
@ -365,6 +378,8 @@ public static class VehicleRigAnimator
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;
@ -381,52 +396,61 @@ public static class VehicleRigAnimator
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.09f);
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":
AnimateLocalPitchLeg(visual, "LegL.Upper", "LegL.Lower", "LegL.Ankle", phiL, st.StrideAmp, hipSign: -1f, kneeSign: 1f);
AnimateLocalPitchLeg(visual, "LegR.Upper", "LegR.Lower", "LegR.Ankle", phiR, st.StrideAmp, hipSign: -1f, kneeSign: 1f);
ApplyPelvisBob(visual, "Pelvis", st.StridePhaseRad, st.StrideAmp, bobScale: 0.11f);
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":
AnimateLocalPitchLeg(visual, "LegL.Thigh", "LegL.Shin", "LegL.Cannon", phiL, st.StrideAmp, hipSign: 1f, kneeSign: -1f);
AnimateLocalPitchLeg(visual, "LegR.Thigh", "LegR.Shin", "LegR.Cannon", phiR, st.StrideAmp, hipSign: 1f, kneeSign: -1f);
ApplyPelvisBob(visual, "Pelvis", st.StridePhaseRad, st.StrideAmp, bobScale: 0.10f);
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":
AnimateLocalPitchLeg(visual, "LegL.Thigh", "LegL.Shin", "LegL.Cannon", phiL, st.StrideAmp, hipSign: 1f, kneeSign: -1f);
AnimateLocalPitchLeg(visual, "LegR.Thigh", "LegR.Shin", "LegR.Cannon", phiR, st.StrideAmp, hipSign: 1f, kneeSign: -1f);
ApplyPelvisBob(visual, "Pelvis", st.StridePhaseRad, st.StrideAmp, bobScale: 0.08f);
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 tilt/bob + Torso.pose counter-sway
ApplyPelvisBob(visual, "Pelvis", st.StridePhaseRad, st.StrideAmp, bobScale: 0.09f);
// 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":
AnimateLocalPitchLeg(visual, "LegFL.S1", "LegFL.S2", "FootFL", phiL, st.StrideAmp * 0.85f, hipSign: 1f, kneeSign: -1f);
AnimateLocalPitchLeg(visual, "LegBR.S1", "LegBR.S2", "FootBR", phiL, st.StrideAmp * 0.85f, hipSign: 1f, kneeSign: -1f);
AnimateLocalPitchLeg(visual, "LegFR.S1", "LegFR.S2", "FootFR", phiR, st.StrideAmp * 0.85f, hipSign: 1f, kneeSign: -1f);
AnimateLocalPitchLeg(visual, "LegBL.S1", "LegBL.S2", "FootBL", phiR, st.StrideAmp * 0.85f, hipSign: 1f, kneeSign: -1f);
ApplyPelvisBob(visual, "Hull", st.StridePhaseRad, st.StrideAmp, bobScale: 0.06f);
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":
AnimateLocalPitchLeg(visual, "Thigh.fl", "Shin.fl", "Ankle.fl", phiL, st.StrideAmp * 0.7f, hipSign: 1f, kneeSign: -1f);
AnimateLocalPitchLeg(visual, "Thigh.rr", "Shin.rr", "Ankle.rr", phiL, st.StrideAmp * 0.7f, hipSign: 1f, kneeSign: -1f);
AnimateLocalPitchLeg(visual, "Thigh.fr", "Shin.fr", "Ankle.fr", phiR, st.StrideAmp * 0.7f, hipSign: 1f, kneeSign: -1f);
AnimateLocalPitchLeg(visual, "Thigh.rl", "Shin.rl", "Ankle.rl", phiR, st.StrideAmp * 0.7f, hipSign: 1f, kneeSign: -1f);
ApplyPelvisBob(visual, "Trunk", st.StridePhaseRad, st.StrideAmp, bobScale: 0.07f);
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,
@ -445,23 +469,71 @@ public static class VehicleRigAnimator
RotateVehicleAxis(visual, footName, Vector3.Right, footAng * signX);
}
private static void AnimateLocalPitchLeg(
private static void AnimateBipedVehicleX(
VehicleVisualInstance visual,
string thighName,
string shinName,
string footName,
float phase,
float amp,
float hipSign,
float kneeSign)
float hipAmp,
float kneeLiftAmp,
float kneeForwardFlexAmp)
{
float hipAng = MathF.Sin(phase) * 0.28f * amp * hipSign;
float kneeAng = MathF.Max(0f, -MathF.Cos(phase)) * 0.34f * amp * kneeSign;
float footAng = -(hipAng + kneeAng) * 0.45f;
float hipAng = -MathF.Sin(phase) * hipAmp * amp;
float kneeAng = (SwingLift(phase) * kneeLiftAmp + MathF.Max(0f, MathF.Sin(phase)) * kneeForwardFlexAmp) * amp;
float footAng = -(hipAng + kneeAng);
RotateLocalX(visual, thighName, hipAng);
RotateLocalX(visual, shinName, kneeAng);
RotateLocalX(visual, footName, footAng);
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(
@ -469,7 +541,8 @@ public static class VehicleRigAnimator
string rootNodeName,
float phase,
float amp,
float bobScale)
float bobScale,
float rollScale = 0.024f)
{
if (visual.GetNamedNode(rootNodeName) is not { } node ||
!visual.TryGetRestLocal(rootNodeName, out var rest))
@ -477,8 +550,8 @@ public static class VehicleRigAnimator
return;
}
float bobY = (MathF.Abs(MathF.Sin(phase)) - 0.5f) * bobScale * amp;
float swayRoll = MathF.Cos(phase) * 0.032f * amp;
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));
}
@ -543,40 +616,43 @@ public static class VehicleRigAnimator
float strideLoadR = 1.0f + 0.42f * MathF.Sin(phiR) * st.StrideAmp + MathF.Max(-0.6f, accelY * 0.05f);
// 1. Oleo gas-spring + asymmetric orifice damping (Brokkr mechrig.Model.strut_step)
float stepDt = Math.Clamp(dt, 0.001f, 0.05f);
for (int i = 0; i < mech.Struts.Count; i++)
if (dt > 0f)
{
var sr = mech.Struts[i];
if (!rig.Struts.TryGetValue(sr.Name, out var ss))
float stepDt = Math.Clamp(dt, 0.001f, 0.05f);
for (int i = 0; i < mech.Struts.Count; i++)
{
continue;
}
float load = string.Equals(sr.Side, "R", StringComparison.OrdinalIgnoreCase) ? strideLoadR : strideLoadL;
float w = MathF.Tau * MathF.Max(0.1f, sr.Hertz);
float column = 1.15f * MathF.Max(0.02f, sr.Travel);
float h = stepDt / 16f;
for (int sub = 0; sub < 16; sub++)
{
float ratio = Math.Clamp(1f - ss.Compression / column, 0.08f, 2f);
float spring = w * w * column * (MathF.Pow(ratio, -1.3f) - 1f) / 1.3f;
float c = 2f * sr.DampingRatio * w * (ss.Velocity < 0f ? 1.3f : 1f);
ss.Velocity += h * (9.81f * load - spring - c * ss.Velocity);
ss.Compression += h * ss.Velocity;
if (ss.Compression < 0f || ss.Compression > sr.Travel)
var sr = mech.Struts[i];
if (!rig.Struts.TryGetValue(sr.Name, out var ss))
{
ss.Compression = Math.Clamp(ss.Compression, 0f, sr.Travel);
ss.Velocity = 0f;
continue;
}
}
if (rig.OleoNodes.TryGetValue(sr.Dof, out var oleo))
{
// LegL/R.Oleo local +Y points down toward the foot; compression slides up (-Y)
oleo.Node.Transform = new Transform3D(
oleo.RestLocal.Basis,
oleo.RestLocal.Origin + new Vector3(0f, -ss.Compression, 0f));
float load = string.Equals(sr.Side, "R", StringComparison.OrdinalIgnoreCase) ? strideLoadR : strideLoadL;
float w = MathF.Tau * MathF.Max(0.1f, sr.Hertz);
float column = 1.15f * MathF.Max(0.02f, sr.Travel);
float h = stepDt / 16f;
for (int sub = 0; sub < 16; sub++)
{
float ratio = Math.Clamp(1f - ss.Compression / column, 0.08f, 2f);
float spring = w * w * column * (MathF.Pow(ratio, -1.3f) - 1f) / 1.3f;
float c = 2f * sr.DampingRatio * w * (ss.Velocity < 0f ? 1.3f : 1f);
ss.Velocity += h * (9.81f * load - spring - c * ss.Velocity);
ss.Compression += h * ss.Velocity;
if (ss.Compression < 0f || ss.Compression > sr.Travel)
{
ss.Compression = Math.Clamp(ss.Compression, 0f, sr.Travel);
ss.Velocity = 0f;
}
}
if (rig.OleoNodes.TryGetValue(sr.Dof, out var oleo))
{
// LegL/R.Oleo local +Y points down toward the foot; compression slides up (-Y)
oleo.Node.Transform = new Transform3D(
oleo.RestLocal.Basis,
oleo.RestLocal.Origin + new Vector3(0f, -ss.Compression, 0f));
}
}
}
@ -791,15 +867,186 @@ public static class VehicleRigAnimator
node.Transform = new Transform3D(delta * rest.Basis, rest.Origin);
}
private static void RotateLocalX(VehicleVisualInstance visual, string nodeName, float angleRad)
private static void EnsureWalkerFootSamples(VehicleVisualInstance visual, RigState st)
{
if (visual.GetNamedNode(nodeName) is not { } node ||
!visual.TryGetRestLocal(nodeName, out var rest))
if (st.FootSamples is not null)
{
return;
}
Basis delta = new Basis(Vector3.Right, angleRad);
node.Transform = new Transform3D(rest.Basis * delta, rest.Origin);
var list = new List<WalkerFootSample>();
var stack = new List<Node3D>();
float cutoffY = visual.Template.MinY + 0.75f;
int count = visual.ModelRoot.GetChildCount();
for (int i = 0; i < count; i++)
{
CollectFootSamplesRecursive(visual.ModelRoot.GetChild(i), Transform3D.Identity, cutoffY, stack, list);
}
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 CollectFootSamplesRecursive(
Node cur,
Transform3D parentToModel,
float cutoffY,
List<Node3D> stack,
List<WalkerFootSample> list)
{
Transform3D toModel = parentToModel;
bool pushed = false;
if (cur is Node3D n3d)
{
toModel = parentToModel * n3d.Transform;
stack.Add(n3d);
pushed = true;
}
if (cur is MeshInstance3D mi && mi.Mesh is { } mesh)
{
Aabb aabb = 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)
{
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 = mesh.GetSurfaceCount();
for (int s = 0; s < surfCount; s++)
{
var arrays = 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 = stack.ToArray(),
LocalPoints = unique.ToArray()
});
}
}
}
int childCount = cur.GetChildCount();
for (int i = 0; i < childCount; i++)
{
CollectFootSamplesRecursive(cur.GetChild(i), toModel, cutoffY, stack, list);
}
if (pushed)
{
stack.RemoveAt(stack.Count - 1);
}
}
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)