1077 lines
42 KiB
C#
1077 lines
42 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Godot;
|
|
using RustToDust.Core;
|
|
|
|
namespace RustToDust.Client;
|
|
|
|
public readonly record struct VehicleRigDriveInput(
|
|
Vector3 Velocity,
|
|
float Yaw,
|
|
float AimYawWorld,
|
|
float AimPitchDeg,
|
|
float SteerCmd,
|
|
float ThrottleCmd,
|
|
float VerticalThrottleCmd,
|
|
float EngineThermalPerf,
|
|
float DeltaTime,
|
|
bool IsGrounded = true);
|
|
|
|
/// <summary>
|
|
/// Drives real-time secondary mechanism animations on <see cref="VehicleVisualInstance"/>
|
|
/// from live physics state with strict NaN/Inf guards:
|
|
/// - 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>) & 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.
|
|
/// </summary>
|
|
public static class VehicleRigAnimator
|
|
{
|
|
public sealed class StrutState
|
|
{
|
|
public float Compression;
|
|
public float Velocity;
|
|
}
|
|
|
|
public sealed class ScissorBinding
|
|
{
|
|
public required Node3D UpperNode;
|
|
public required Node3D LowerNode;
|
|
public required Node3D ShinNode;
|
|
public required Node3D OleoNode;
|
|
public required Node3D OleoForkNode;
|
|
public required Transform3D RestLocalUp;
|
|
public required Transform3D RestLocalLow;
|
|
public required Vector3 AInShin;
|
|
public required Vector3 BInOleoFork;
|
|
public required Vector3 OutInShin;
|
|
public required float Arm;
|
|
public required Vector3 DirUp0InShin;
|
|
public required Vector3 DirLow0InOleoFork;
|
|
}
|
|
|
|
public sealed class RamBinding
|
|
{
|
|
public required Node3D RamNode;
|
|
public required Node3D LugNode;
|
|
public required Node3D OleoNode;
|
|
public required Node3D OleoForkNode;
|
|
public required Node3D FootNode;
|
|
public required Transform3D RestLocalRam;
|
|
public required Vector3 APosInLug;
|
|
public required Vector3 BPosInFoot;
|
|
public required Vector3 WantDir0InLug;
|
|
}
|
|
|
|
public sealed class BreakerMechRig
|
|
{
|
|
public bool Initialized;
|
|
public readonly Dictionary<string, StrutState> Struts = new(StringComparer.Ordinal);
|
|
public readonly Dictionary<string, (Node3D Node, Transform3D RestLocal)> OleoNodes = new(StringComparer.Ordinal);
|
|
public readonly List<ScissorBinding> Scissors = new();
|
|
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;
|
|
public float SteerRad;
|
|
public float TurretYawRad;
|
|
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 static void Update(
|
|
VehicleVisualInstance visual,
|
|
ContenderState c,
|
|
Vector3 velocity,
|
|
Basis worldBasis,
|
|
float dt,
|
|
bool snap = false)
|
|
{
|
|
float aimYawWorld = c.Yaw;
|
|
float dx = c.AimX - c.X;
|
|
float dz = c.AimZ - c.Z;
|
|
if (float.IsFinite(dx) && float.IsFinite(dz) && (dx * dx + dz * dz) > 0.01f)
|
|
{
|
|
aimYawWorld = MathF.Atan2(dx, dz);
|
|
}
|
|
|
|
float aimPitchDeg = 0f;
|
|
if (float.IsFinite(c.AimY))
|
|
{
|
|
float aimOriginY = MatchRules.GetObserverEyeY(c.Y, c.Grid.Spec.HalfHeight);
|
|
float horiz = MathF.Sqrt(dx * dx + dz * dz);
|
|
if (horiz > 0.1f)
|
|
{
|
|
aimPitchDeg = Mathf.RadToDeg(MathF.Atan2(c.AimY - aimOriginY, horiz));
|
|
}
|
|
}
|
|
|
|
var input = new VehicleRigDriveInput(
|
|
Velocity: velocity,
|
|
Yaw: c.Yaw,
|
|
AimYawWorld: aimYawWorld,
|
|
AimPitchDeg: aimPitchDeg,
|
|
SteerCmd: c.SteerCmd,
|
|
ThrottleCmd: c.ThrottleCmd,
|
|
VerticalThrottleCmd: c.VerticalThrottleCmd,
|
|
EngineThermalPerf: c.Grid.ComputeEngineThermalPerformance(),
|
|
DeltaTime: dt,
|
|
IsGrounded: !c.Grid.Spec.IsVtol);
|
|
|
|
Update(visual, in input, worldBasis, snap);
|
|
}
|
|
|
|
public static void Update(
|
|
VehicleVisualInstance visual,
|
|
in VehicleRigDriveInput input,
|
|
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);
|
|
float aimPitchDeg = Sanitize(input.AimPitchDeg, 0f, -85f, 85f);
|
|
float steerCmd = Sanitize(input.SteerCmd, 0f, -1f, 1f);
|
|
float throttleCmd = Sanitize(input.ThrottleCmd, 0f, -1f, 1f);
|
|
float vertCmd = Sanitize(input.VerticalThrottleCmd, 0f, -1f, 1f);
|
|
float enginePerf = Sanitize(input.EngineThermalPerf, 1f, 0f, 1f);
|
|
|
|
RigState st = visual.RigState;
|
|
st.ElapsedSeconds += dt;
|
|
|
|
Vector3 fwd = worldBasis.Z;
|
|
fwd.Y = 0f;
|
|
fwd = fwd.LengthSquared() > 1e-5f ? fwd.Normalized() : new Vector3(MathF.Sin(yaw), 0f, MathF.Cos(yaw));
|
|
Vector3 right = new(-fwd.Z, 0f, fwd.X);
|
|
|
|
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);
|
|
|
|
float blendFast = dt > 0f ? 1f - MathF.Exp(-dt * 10f) : 1f;
|
|
float blendMed = dt > 0f ? 1f - MathF.Exp(-dt * 7f) : 1f;
|
|
|
|
// 1. Turrets & torsos
|
|
UpdateTurretAndTorso(visual, st, relYaw, aimPitchRad, blendFast);
|
|
|
|
// 2. Mobility-specific drives
|
|
var mobility = visual.Package.Mobility;
|
|
switch (mobility)
|
|
{
|
|
case MobilityKind.Wheeled:
|
|
UpdateWheeled(visual, st, forwardSpeed, lateralSpeed, steerCmd, dt, blendFast);
|
|
break;
|
|
|
|
case MobilityKind.Vtol:
|
|
UpdateVtol(visual, st, forwardSpeed, throttleCmd, vertCmd, enginePerf, dt, blendMed);
|
|
break;
|
|
|
|
case MobilityKind.Walker:
|
|
UpdateWalker(visual, st, horizSpeed, forwardSpeed, vel.Y, steerCmd, relYaw, aimPitchRad, dt, blendMed);
|
|
break;
|
|
|
|
case MobilityKind.Tracked:
|
|
UpdateTrackedSuspension(visual, st, forwardSpeed, lateralSpeed, dt, blendMed);
|
|
break;
|
|
}
|
|
|
|
st.PrevForwardSpeed = forwardSpeed;
|
|
st.PrevVelY = vel.Y;
|
|
}
|
|
|
|
private static void UpdateTurretAndTorso(
|
|
VehicleVisualInstance visual,
|
|
RigState st,
|
|
float relYaw,
|
|
float aimPitchRad,
|
|
float blend)
|
|
{
|
|
if (visual.Joints.TryGetValue("joint.turret_yaw", out var turretJoint) ||
|
|
visual.Joints.TryGetValue("joint.torso_yaw", out turretJoint))
|
|
{
|
|
bool fullSpin = turretJoint.MinRad <= -MathF.PI * 0.99f && turretJoint.MaxRad >= MathF.PI * 0.99f;
|
|
float targetYaw = fullSpin
|
|
? relYaw
|
|
: Math.Clamp(relYaw, turretJoint.MinRad, turretJoint.MaxRad);
|
|
|
|
if (fullSpin)
|
|
{
|
|
float diff = MatchRules.NormalizeAngle(targetYaw - st.TurretYawRad);
|
|
st.TurretYawRad = MatchRules.NormalizeAngle(st.TurretYawRad + diff * blend);
|
|
}
|
|
else
|
|
{
|
|
st.TurretYawRad += (targetYaw - st.TurretYawRad) * blend;
|
|
}
|
|
|
|
SetJointAngle(turretJoint, st.TurretYawRad);
|
|
}
|
|
|
|
st.ArmPitchRad += (aimPitchRad - st.ArmPitchRad) * blend;
|
|
// Positive elevation (+aimPitchRad) rotates negatively around vehicle +X (raises +Z barrels toward +Y)
|
|
if (visual.Joints.TryGetValue("joint.arm_l_pitch", out var armL))
|
|
{
|
|
float clampedL = Math.Clamp(-st.ArmPitchRad, armL.MinRad, armL.MaxRad);
|
|
SetJointAngle(armL, clampedL);
|
|
}
|
|
if (visual.Joints.TryGetValue("joint.arm_r_pitch", out var armR))
|
|
{
|
|
float clampedR = Math.Clamp(-st.ArmPitchRad, armR.MinRad, armR.MaxRad);
|
|
SetJointAngle(armR, clampedR);
|
|
}
|
|
}
|
|
|
|
private static void UpdateWheeled(
|
|
VehicleVisualInstance visual,
|
|
RigState st,
|
|
float forwardSpeed,
|
|
float lateralSpeed,
|
|
float steerCmd,
|
|
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)
|
|
{
|
|
if (jid.StartsWith("joint.wheel_", StringComparison.Ordinal))
|
|
{
|
|
float y = j.RestLocal.Origin.Y;
|
|
if (y <= 0.05f && j.Node.GetParent() is Node3D p3)
|
|
{
|
|
y = p3.Transform.Origin.Y;
|
|
}
|
|
if (y > 0.1f)
|
|
{
|
|
radiusSum += y;
|
|
radiusCount++;
|
|
}
|
|
}
|
|
}
|
|
float wheelRadius = radiusCount > 0 ? radiusSum / radiusCount : 0.45f;
|
|
|
|
st.WheelSpinRad = MathF.IEEERemainder(st.WheelSpinRad + (forwardSpeed / MathF.Max(0.25f, wheelRadius)) * dt, MathF.Tau);
|
|
|
|
float targetSteer = -steerCmd * Mathf.DegToRad(28f);
|
|
st.SteerRad += (targetSteer - st.SteerRad) * blend;
|
|
|
|
foreach (var (jid, j) in visual.Joints)
|
|
{
|
|
if (jid.StartsWith("joint.wheel_", StringComparison.Ordinal))
|
|
{
|
|
SetJointAngle(j, st.WheelSpinRad);
|
|
}
|
|
else if (jid.StartsWith("joint.steer_", StringComparison.Ordinal))
|
|
{
|
|
float clampedSteer = Math.Clamp(st.SteerRad, j.MinRad, j.MaxRad);
|
|
SetJointAngle(j, clampedSteer);
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
st.ChassisPitchRad += (targetPitch - st.ChassisPitchRad) * blend;
|
|
st.ChassisRollRad += (targetRoll - st.ChassisRollRad) * blend;
|
|
|
|
if (visual.GetNamedNode("Chassis") is { } chassis && visual.TryGetRestLocal("Chassis", out var restChassis))
|
|
{
|
|
Basis tilt = new Basis(Vector3.Right, st.ChassisPitchRad) * new Basis(Vector3.Back, st.ChassisRollRad);
|
|
chassis.Transform = new Transform3D(tilt * restChassis.Basis, restChassis.Origin);
|
|
}
|
|
}
|
|
|
|
private static void UpdateTrackedSuspension(
|
|
VehicleVisualInstance visual,
|
|
RigState st,
|
|
float forwardSpeed,
|
|
float lateralSpeed,
|
|
float dt,
|
|
float blend)
|
|
{
|
|
float accel = dt > 1e-4f ? (forwardSpeed - st.PrevForwardSpeed) / dt : 0f;
|
|
float targetPitch = Math.Clamp(-accel * 0.0018f, -0.045f, 0.045f);
|
|
float targetRoll = Math.Clamp(lateralSpeed * 0.003f, -0.045f, 0.045f);
|
|
st.ChassisPitchRad += (targetPitch - st.ChassisPitchRad) * blend;
|
|
st.ChassisRollRad += (targetRoll - st.ChassisRollRad) * blend;
|
|
|
|
if (visual.GetNamedNode("Chassis") is { } chassis && visual.TryGetRestLocal("Chassis", out var restChassis))
|
|
{
|
|
Basis tilt = new Basis(Vector3.Right, st.ChassisPitchRad) * new Basis(Vector3.Back, st.ChassisRollRad);
|
|
chassis.Transform = new Transform3D(tilt * restChassis.Basis, restChassis.Origin);
|
|
}
|
|
}
|
|
|
|
private static void UpdateVtol(
|
|
VehicleVisualInstance visual,
|
|
RigState st,
|
|
float forwardSpeed,
|
|
float throttleCmd,
|
|
float vertCmd,
|
|
float enginePerf,
|
|
float dt,
|
|
float blend)
|
|
{
|
|
float rotorSpeed = (5.5f + 18.0f * enginePerf) *
|
|
(1f + 0.35f * MathF.Max(0f, vertCmd) + 0.16f * MathF.Abs(throttleCmd));
|
|
st.RotorSpinRad = MathF.IEEERemainder(st.RotorSpinRad + rotorSpeed * dt, MathF.Tau);
|
|
|
|
float targetTilt = throttleCmd * 0.42f + (forwardSpeed / 42f) * 0.28f;
|
|
st.NacelleTiltRad += (targetTilt - st.NacelleTiltRad) * blend;
|
|
|
|
foreach (var (jid, j) in visual.Joints)
|
|
{
|
|
if (jid.StartsWith("joint.rotor_", StringComparison.Ordinal))
|
|
{
|
|
float dir = (jid.Contains("_r", StringComparison.Ordinal) && !jid.Contains("_rl", StringComparison.Ordinal)) ||
|
|
jid.EndsWith("_rl", StringComparison.Ordinal)
|
|
? -1f
|
|
: 1f;
|
|
SetJointAngle(j, st.RotorSpinRad * dir);
|
|
}
|
|
else if (jid.StartsWith("joint.nacelle_", StringComparison.Ordinal))
|
|
{
|
|
float clampedTilt = Math.Clamp(st.NacelleTiltRad, j.MinRad, j.MaxRad);
|
|
SetJointAngle(j, clampedTilt);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void UpdateWalker(
|
|
VehicleVisualInstance visual,
|
|
RigState st,
|
|
float horizSpeed,
|
|
float forwardSpeed,
|
|
float velY,
|
|
float steerCmd,
|
|
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))
|
|
{
|
|
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));
|
|
}
|
|
|
|
private static void AnimateManticoreTail(
|
|
VehicleVisualInstance visual,
|
|
RigState st,
|
|
float relYaw,
|
|
float aimPitchRad,
|
|
float blend)
|
|
{
|
|
if (visual.Joints.TryGetValue("joint.tail_yaw", out var tailYaw))
|
|
{
|
|
float targetTailYaw = Math.Clamp(relYaw, tailYaw.MinRad, tailYaw.MaxRad);
|
|
st.TurretYawRad += (targetTailYaw - st.TurretYawRad) * blend;
|
|
SetJointAngle(tailYaw, st.TurretYawRad);
|
|
}
|
|
|
|
float idleSway = MathF.Sin(st.ElapsedSeconds * 2.1f) * 0.05f;
|
|
float pitchDrive = aimPitchRad * 0.5f + idleSway;
|
|
|
|
if (visual.Joints.TryGetValue("joint.tail_s1", out var s1))
|
|
{
|
|
SetJointAngle(s1, Math.Clamp(pitchDrive * 0.45f, s1.MinRad, s1.MaxRad));
|
|
}
|
|
if (visual.Joints.TryGetValue("joint.tail_s2", out var s2))
|
|
{
|
|
SetJointAngle(s2, Math.Clamp(-pitchDrive * 0.65f, s2.MinRad, s2.MaxRad));
|
|
}
|
|
if (visual.Joints.TryGetValue("joint.tail_s3", out var s3))
|
|
{
|
|
SetJointAngle(s3, Math.Clamp(-pitchDrive * 0.55f, s3.MinRad, s3.MaxRad));
|
|
}
|
|
if (visual.Joints.TryGetValue("joint.tail_elev", out var elev))
|
|
{
|
|
SetJointAngle(elev, Math.Clamp(-aimPitchRad * 0.85f, elev.MinRad, elev.MaxRad));
|
|
}
|
|
}
|
|
|
|
private static void StepBreakerMechanisms(
|
|
VehicleVisualInstance visual,
|
|
RigState st,
|
|
float phiL,
|
|
float phiR,
|
|
float velY,
|
|
float dt)
|
|
{
|
|
VehicleMechanismsRecord? mech = visual.Package.Mechanisms;
|
|
if (mech is null || mech.Struts.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
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);
|
|
|
|
// 1. Oleo gas-spring + asymmetric orifice damping (Brokkr mechrig.Model.strut_step)
|
|
if (dt > 0f)
|
|
{
|
|
float stepDt = Math.Clamp(dt, 0.001f, 0.05f);
|
|
for (int i = 0; i < mech.Struts.Count; i++)
|
|
{
|
|
var sr = mech.Struts[i];
|
|
if (!rig.Struts.TryGetValue(sr.Name, out var ss))
|
|
{
|
|
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)
|
|
{
|
|
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));
|
|
}
|
|
}
|
|
}
|
|
|
|
// 2. Scissor 2-link torque knees (LegL/R.TorqueUp, LegL/R.TorqueLow)
|
|
for (int i = 0; i < rig.Scissors.Count; i++)
|
|
{
|
|
var sb = rig.Scissors[i];
|
|
Transform3D forkInShin = sb.OleoNode.Transform * sb.OleoForkNode.Transform;
|
|
Vector3 bInShin = forkInShin * sb.BInOleoFork;
|
|
Vector3 apexInShin = SolveScissorApex(sb.AInShin, bInShin, sb.OutInShin, sb.Arm);
|
|
|
|
Vector3 dirUp = (apexInShin - sb.AInShin);
|
|
if (dirUp.LengthSquared() > 1e-8f)
|
|
{
|
|
Quaternion qUp = ShortestArcQuat(sb.DirUp0InShin, dirUp.Normalized());
|
|
Basis rotUp = new Basis(qUp);
|
|
sb.UpperNode.Transform = new Transform3D(rotUp, sb.AInShin - rotUp * sb.AInShin) * sb.RestLocalUp;
|
|
}
|
|
|
|
Vector3 apexInFork = forkInShin.Inverse() * apexInShin;
|
|
Vector3 dirLow = (apexInFork - sb.BInOleoFork);
|
|
if (dirLow.LengthSquared() > 1e-8f)
|
|
{
|
|
Quaternion qLow = ShortestArcQuat(sb.DirLow0InOleoFork, dirLow.Normalized());
|
|
Basis rotLow = new Basis(qLow);
|
|
sb.LowerNode.Transform = new Transform3D(rotLow, sb.BInOleoFork - rotLow * sb.BInOleoFork) * sb.RestLocalLow;
|
|
}
|
|
}
|
|
|
|
// 3. Telescoping recess hydraulic rams (LegL/R.RecessRam0/1)
|
|
for (int i = 0; i < rig.Rams.Count; i++)
|
|
{
|
|
var rb = rig.Rams[i];
|
|
Transform3D forkInShin = rb.OleoNode.Transform * rb.OleoForkNode.Transform;
|
|
Transform3D footInLug = rb.LugNode.Transform.Inverse() * forkInShin * rb.FootNode.Transform;
|
|
Vector3 bInLug = footInLug * rb.BPosInFoot;
|
|
Vector3 span = bInLug - rb.APosInLug;
|
|
if (span.LengthSquared() > 1e-8f)
|
|
{
|
|
Quaternion qRam = ShortestArcQuat(rb.WantDir0InLug, span.Normalized());
|
|
rb.RamNode.Transform = new Transform3D(new Basis(qRam) * rb.RestLocalRam.Basis, rb.RestLocalRam.Origin);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static BreakerMechRig InitBreakerRig(VehicleVisualInstance visual, VehicleMechanismsRecord mech)
|
|
{
|
|
var rig = new BreakerMechRig();
|
|
|
|
foreach (var sr in mech.Struts)
|
|
{
|
|
float w = MathF.Tau * MathF.Max(0.1f, sr.Hertz);
|
|
float col = 1.15f * MathF.Max(0.02f, sr.Travel);
|
|
float xRest = col * (1f - MathF.Pow(1f + 1.3f * 9.81f * 1.0f / (w * w * col), -1f / 1.3f));
|
|
rig.Struts[sr.Name] = new StrutState
|
|
{
|
|
Compression = Math.Clamp(xRest, 0f, sr.Travel),
|
|
Velocity = 0f
|
|
};
|
|
|
|
if (visual.GetNamedNode(sr.Dof) is { } oNode && visual.TryGetRestLocal(sr.Dof, out var oRest))
|
|
{
|
|
rig.OleoNodes[sr.Dof] = (oNode, oRest);
|
|
}
|
|
}
|
|
|
|
foreach (var sc in mech.Scissors)
|
|
{
|
|
string side = sc.Name.StartsWith("LegR", StringComparison.Ordinal) ? "R" : "L";
|
|
string upName = $"Leg{side}.TorqueUp";
|
|
string lowName = $"Leg{side}.TorqueLow";
|
|
string shinName = $"Leg{side}.Shin";
|
|
string oleoName = $"Leg{side}.Oleo";
|
|
string forkName = $"Leg{side}.OleoFork";
|
|
|
|
if (visual.GetNamedNode(upName) is not { } upNode ||
|
|
visual.GetNamedNode(lowName) is not { } lowNode ||
|
|
visual.GetNamedNode(shinName) is not { } shinNode ||
|
|
visual.GetNamedNode(oleoName) is not { } oleoNode ||
|
|
visual.GetNamedNode(forkName) is not { } forkNode ||
|
|
!visual.TryGetRestLocal(upName, out var restUp) ||
|
|
!visual.TryGetRestLocal(lowName, out var restLow) ||
|
|
!visual.TryGetRestModel(shinName, out var shinRestModel) ||
|
|
!visual.TryGetRestModel(forkName, out var forkRestModel) ||
|
|
sc.APos is null || sc.BPos is null || sc.OutDir is null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Vector3 aModel = new(sc.APos[0], sc.APos[1], sc.APos[2]);
|
|
Vector3 bModel = new(sc.BPos[0], sc.BPos[1], sc.BPos[2]);
|
|
Vector3 outModel = new(sc.OutDir[0], sc.OutDir[1], sc.OutDir[2]);
|
|
|
|
Transform3D shinInv = shinRestModel.Inverse();
|
|
Transform3D forkInv = forkRestModel.Inverse();
|
|
|
|
Vector3 aInShin = shinInv * aModel;
|
|
Vector3 bRestInShin = shinInv * bModel;
|
|
Vector3 bInFork = forkInv * bModel;
|
|
Vector3 outInShin = (shinInv.Basis * outModel).Normalized();
|
|
|
|
Vector3 apex0InShin = SolveScissorApex(aInShin, bRestInShin, outInShin, sc.Arm);
|
|
Vector3 apex0InFork = (forkRestModel.Inverse() * shinRestModel) * apex0InShin;
|
|
|
|
Vector3 dirUp0 = (apex0InShin - aInShin).Normalized();
|
|
Vector3 dirLow0 = (apex0InFork - bInFork).Normalized();
|
|
|
|
rig.Scissors.Add(new ScissorBinding
|
|
{
|
|
UpperNode = upNode,
|
|
LowerNode = lowNode,
|
|
ShinNode = shinNode,
|
|
OleoNode = oleoNode,
|
|
OleoForkNode = forkNode,
|
|
RestLocalUp = restUp,
|
|
RestLocalLow = restLow,
|
|
AInShin = aInShin,
|
|
BInOleoFork = bInFork,
|
|
OutInShin = outInShin,
|
|
Arm = sc.Arm,
|
|
DirUp0InShin = dirUp0,
|
|
DirLow0InOleoFork = dirLow0
|
|
});
|
|
}
|
|
|
|
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}";
|
|
string oleoName = $"Leg{side}.Oleo";
|
|
string forkName = $"Leg{side}.OleoFork";
|
|
string footName = $"Leg{side}.Foot";
|
|
|
|
if (visual.GetNamedNode(r.Name) is not { } ramNode ||
|
|
visual.GetNamedNode(lugName) is not { } lugNode ||
|
|
visual.GetNamedNode(oleoName) is not { } oleoNode ||
|
|
visual.GetNamedNode(forkName) is not { } forkNode ||
|
|
visual.GetNamedNode(footName) is not { } footNode ||
|
|
!visual.TryGetRestLocal(r.Name, out var restRam) ||
|
|
!visual.TryGetRestModel(lugName, out var lugRestModel) ||
|
|
!visual.TryGetRestModel(footName, out var footRestModel) ||
|
|
r.BPos is null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
Vector3 bModel = new(r.BPos[0], r.BPos[1], r.BPos[2]);
|
|
Vector3 aInLug = restRam.Origin;
|
|
Vector3 bInLug0 = lugRestModel.Inverse() * bModel;
|
|
Vector3 bInFoot = footRestModel.Inverse() * bModel;
|
|
Vector3 want0 = (bInLug0 - aInLug).Normalized();
|
|
|
|
rig.Rams.Add(new RamBinding
|
|
{
|
|
RamNode = ramNode,
|
|
LugNode = lugNode,
|
|
OleoNode = oleoNode,
|
|
OleoForkNode = forkNode,
|
|
FootNode = footNode,
|
|
RestLocalRam = restRam,
|
|
APosInLug = aInLug,
|
|
BPosInFoot = bInFoot,
|
|
WantDir0InLug = want0
|
|
});
|
|
}
|
|
|
|
rig.Initialized = rig.Struts.Count > 0;
|
|
return rig;
|
|
}
|
|
|
|
private static Vector3 SolveScissorApex(Vector3 a, Vector3 b, Vector3 outDir, float arm)
|
|
{
|
|
Vector3 u = b - a;
|
|
float d = u.Length();
|
|
if (d < 1e-5f)
|
|
{
|
|
return (a + b) * 0.5f + outDir * arm;
|
|
}
|
|
u /= d;
|
|
Vector3 oFlat = outDir - u * outDir.Dot(u);
|
|
oFlat = oFlat.LengthSquared() > 1e-6f ? oFlat.Normalized() : Vector3.Back;
|
|
float h = MathF.Sqrt(MathF.Max(arm * arm - (d * 0.5f) * (d * 0.5f), 0f));
|
|
return (a + b) * 0.5f + oFlat * h;
|
|
}
|
|
|
|
private static void SetJointAngle(VehicleVisualInstance.BoundJoint j, float angleRad)
|
|
{
|
|
if (!GodotObject.IsInstanceValid(j.Node) || !float.IsFinite(angleRad))
|
|
{
|
|
return;
|
|
}
|
|
Basis delta = new Basis(j.AxisInParent, angleRad);
|
|
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>();
|
|
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)
|
|
{
|
|
float dot = Math.Clamp(from.Dot(to), -1f, 1f);
|
|
if (dot > 0.99999f)
|
|
{
|
|
return Quaternion.Identity;
|
|
}
|
|
if (dot < -0.99999f)
|
|
{
|
|
Vector3 ortho = MathF.Abs(from.X) < 0.8f ? Vector3.Right : Vector3.Up;
|
|
Vector3 axis = from.Cross(ortho).Normalized();
|
|
return new Quaternion(axis, MathF.PI);
|
|
}
|
|
Vector3 cross = from.Cross(to);
|
|
return new Quaternion(cross.X, cross.Y, cross.Z, 1f + dot).Normalized();
|
|
}
|
|
|
|
private static float Sanitize(float v, float fallback, float min, float max) =>
|
|
float.IsFinite(v) ? Math.Clamp(v, min, max) : fallback;
|
|
|
|
private static Vector3 SanitizeVec(Vector3 v) =>
|
|
new(
|
|
float.IsFinite(v.X) ? v.X : 0f,
|
|
float.IsFinite(v.Y) ? v.Y : 0f,
|
|
float.IsFinite(v.Z) ? v.Z : 0f);
|
|
}
|