diff --git a/client/ArenaRenderer.cs b/client/ArenaRenderer.cs
index 9c368d5..0c4045f 100644
--- a/client/ArenaRenderer.cs
+++ b/client/ArenaRenderer.cs
@@ -8,41 +8,32 @@ namespace RustToDust.Client;
///
/// Renders the 3D arena entities from :
-/// cover ruins, shrinking Safe Circle ring, 6 distinct chassis silhouettes,
-/// 3 alien spectator tiers + status halos/lock lasers, salvage drops, and weapon tracers.
+/// cover ruins, shrinking Safe Circle ring, real Brokkr vehicle/mech GLBs with hardpoint snap-on gear,
+/// live secondary mechanism animations, part-by-cell debris detachment, 3 alien spectator tiers + status halos/lock lasers,
+/// real gear/chassis salvage drops, and weapon tracers.
///
public partial class ArenaRenderer : Node3D
{
private static readonly Color OlivePaint = Color.FromHtml("#9bb06a");
- private static readonly Color SignalOrange = Color.FromHtml("#e08a4a");
- private static readonly Color RivalSteel = Color.FromHtml("#6a584b");
- private static readonly Color RivalAccent = Color.FromHtml("#b64a32");
private static readonly Color TouristGold = Color.FromHtml("#e6b838");
- private static readonly Color TouristIvory = Color.FromHtml("#f2ead8");
- private static readonly Color PatronTeal = Color.FromHtml("#2d7a78");
- private static readonly Color PatronBronze = Color.FromHtml("#a86c36");
- private static readonly Color ExemplarObsidian = Color.FromHtml("#18181d");
- private static readonly Color ExemplarCrimson = Color.FromHtml("#c92a2a");
private static readonly Color CyanSalvage = Color.FromHtml("#44d9e8");
private static readonly Color CoverRust = Color.FromHtml("#4d433b");
private static readonly Color BermDark = Color.FromHtml("#2c2723");
private sealed class ActorVisual
{
- public Node3D Root = null!;
- public Node3D? AimPivot;
- public StandardMaterial3D HeatMat = null!;
+ public required VehicleVisualInstance Visual;
public MeshInstance3D? HaloMesh;
public StandardMaterial3D? HaloMat;
public MeshInstance3D? LaserMesh;
- public StandardMaterial3D? LaserMat;
- public string ChassisId = "";
}
+ private VehicleAssetCache _assetCache = null!;
private Node3D _staticGroup = null!;
private Node3D _actorsGroup = null!;
private Node3D _dropsGroup = null!;
private Node3D _tracersGroup = null!;
+ private DebrisSystem _debris = null!;
private MeshInstance3D _safeRingMesh = null!;
private ImmediateMesh _safeRingImm = null!;
@@ -54,43 +45,65 @@ public partial class ArenaRenderer : Node3D
private StandardMaterial3D _safeRingMat = null!;
private StandardMaterial3D _coverMat = null!;
private StandardMaterial3D _bermMat = null!;
- private StandardMaterial3D _humanCrateMat = null!;
- private StandardMaterial3D _imperialCrateMat = null!;
+ private StandardMaterial3D _humanBeaconMat = null!;
+ private StandardMaterial3D _imperialBeaconMat = null!;
private StandardMaterial3D _cyanBeaconMat = null!;
private MatchState? _builtForState;
+ public VehicleAssetCache AssetCache => _assetCache;
+ public DebrisSystem Debris => _debris;
+
+ public VehicleVisualInstance? GetContenderVisual(int contenderId) =>
+ _contenderVisuals.TryGetValue(contenderId, out var vis) ? vis.Visual : null;
+
+ public VehicleVisualInstance? GetSpectatorVisual(int spectatorId) =>
+ _spectatorVisuals.TryGetValue(spectatorId, out var vis) ? vis.Visual : null;
+
public override void _Ready()
{
+ _assetCache = new VehicleAssetCache();
+
_staticGroup = new Node3D { Name = "StaticArena" };
_actorsGroup = new Node3D { Name = "Actors" };
_dropsGroup = new Node3D { Name = "GroundDrops" };
_tracersGroup = new Node3D { Name = "Tracers" };
+ _debris = new DebrisSystem { Name = "Debris" };
AddChild(_staticGroup);
AddChild(_actorsGroup);
AddChild(_dropsGroup);
AddChild(_tracersGroup);
+ AddChild(_debris);
_coverMat = new StandardMaterial3D { AlbedoColor = CoverRust, Roughness = 0.88f, Metallic = 0.25f };
_bermMat = new StandardMaterial3D { AlbedoColor = BermDark, Roughness = 0.94f, Metallic = 0.1f };
- _humanCrateMat = new StandardMaterial3D { AlbedoColor = OlivePaint, Roughness = 0.65f, Metallic = 0.3f };
- _imperialCrateMat = new StandardMaterial3D
+ _humanBeaconMat = new StandardMaterial3D
{
- AlbedoColor = TouristGold,
+ AlbedoColor = new Color(OlivePaint.R, OlivePaint.G, OlivePaint.B, 0.72f),
+ Roughness = 0.55f,
+ Metallic = 0.35f,
+ EmissionEnabled = true,
+ Emission = OlivePaint,
+ EmissionEnergyMultiplier = 0.6f,
+ Transparency = BaseMaterial3D.TransparencyEnum.Alpha
+ };
+ _imperialBeaconMat = new StandardMaterial3D
+ {
+ AlbedoColor = new Color(TouristGold.R, TouristGold.G, TouristGold.B, 0.82f),
Roughness = 0.35f,
Metallic = 0.8f,
EmissionEnabled = true,
Emission = TouristGold,
- EmissionEnergyMultiplier = 1.4f
+ EmissionEnergyMultiplier = 1.4f,
+ Transparency = BaseMaterial3D.TransparencyEnum.Alpha
};
_cyanBeaconMat = new StandardMaterial3D
{
- AlbedoColor = CyanSalvage,
+ AlbedoColor = new Color(CyanSalvage.R, CyanSalvage.G, CyanSalvage.B, 0.72f),
EmissionEnabled = true,
Emission = CyanSalvage,
EmissionEnergyMultiplier = 1.5f,
Transparency = BaseMaterial3D.TransparencyEnum.Alpha
};
- _cyanBeaconMat.AlbedoColor = new Color(CyanSalvage.R, CyanSalvage.G, CyanSalvage.B, 0.72f);
_safeRingImm = new ImmediateMesh();
_safeRingMat = new StandardMaterial3D
@@ -113,7 +126,12 @@ public partial class ArenaRenderer : Node3D
AddChild(_safeRingMesh);
}
- public void Sync(ArenaSession session, float elapsedSeconds)
+ public override void _ExitTree()
+ {
+ _assetCache?.Dispose();
+ }
+
+ public void Sync(ArenaSession session, float elapsedSeconds, float dt = 1f / 60f)
{
var state = session.State;
if (!ReferenceEquals(_builtForState, state))
@@ -124,10 +142,11 @@ public partial class ArenaRenderer : Node3D
}
UpdateSafeCircleRing(state.Terrain, state.Spectators.SafeRadius);
- SyncContenders(session);
- SyncSpectators(session, elapsedSeconds);
+ SyncContenders(session, dt);
+ SyncSpectators(session, elapsedSeconds, dt);
SyncGroundDrops(state, elapsedSeconds);
SyncTracers(state.Tracers);
+ _debris.Step(dt, state.Terrain);
}
private void RebuildStaticArena(MatchState state)
@@ -163,6 +182,7 @@ public partial class ArenaRenderer : Node3D
{
ClearChildren(_actorsGroup);
ClearChildren(_dropsGroup);
+ _debris.ClearAll();
_contenderVisuals.Clear();
_spectatorVisuals.Clear();
_dropVisuals.Clear();
@@ -210,7 +230,7 @@ public partial class ArenaRenderer : Node3D
_safeRingImm.SurfaceEnd();
}
- private void SyncContenders(ArenaSession session)
+ private void SyncContenders(ArenaSession session, float dt)
{
var view = session.World.View;
var contenders = session.State.Contenders;
@@ -222,54 +242,53 @@ public partial class ArenaRenderer : Node3D
{
if (_contenderVisuals.TryGetValue(c.Id, out var deadVis))
{
- deadVis.Root.Visible = false;
+ // Flush any final stripped plates/gear into debris before hiding wreck
+ deadVis.Visual.SyncDamageAndHeat(c.Grid, _debris, Vector3.Zero);
+ deadVis.Visual.Root.Visible = false;
}
continue;
}
- if (!_contenderVisuals.TryGetValue(c.Id, out var vis) || vis.ChassisId != c.Grid.Spec.Id)
+ if (!_contenderVisuals.TryGetValue(c.Id, out var vis) || vis.Visual.ChassisId != c.Grid.Spec.Id)
{
if (vis is not null)
{
- vis.Root.QueueFree();
+ vis.Visual.Root.QueueFree();
}
- vis = BuildChassisVisual(
- c.Grid.Spec.Id,
- primary: c.IsPlayer ? OlivePaint : RivalSteel,
- accent: c.IsPlayer ? SignalOrange : RivalAccent,
- isSpectator: false,
- tier: SpectatorTier.Patron);
- _actorsGroup.AddChild(vis.Root);
+
+ var role = c.IsPlayer ? VehicleVisualRole.Player : VehicleVisualRole.Rival;
+ var instance = VehicleVisualInstance.Create(_assetCache, c.Grid.Spec.Id, role);
+ _actorsGroup.AddChild(instance.Root);
+ vis = new ActorVisual { Visual = instance };
_contenderVisuals[c.Id] = vis;
}
- vis.Root.Visible = true;
+ vis.Visual.Root.Visible = true;
+
+ Vector3 vel;
var body = session.GetContenderBody(c.Id);
if (body is { } h)
{
int lane = h.Lane;
var pos = new Vector3(view.PositionX[lane], view.PositionY[lane], view.PositionZ[lane]);
var q = new Quaternion(view.RotationX[lane], view.RotationY[lane], view.RotationZ[lane], view.RotationW[lane]);
- vis.Root.Transform = new Transform3D(new Basis(q), pos);
+ vel = new Vector3(view.VelocityX[lane], view.VelocityY[lane], view.VelocityZ[lane]);
+ vis.Visual.Root.Transform = new Transform3D(new Basis(q), pos);
}
else
{
- vis.Root.Position = new Vector3(c.X, c.Y, c.Z);
- vis.Root.Rotation = new Vector3(0f, c.Yaw, 0f);
+ vel = Vector3.Zero;
+ vis.Visual.Root.Position = new Vector3(c.X, c.Y, c.Z);
+ vis.Visual.Root.Rotation = new Vector3(0f, c.Yaw, 0f);
}
- if (vis.AimPivot is not null)
- {
- float aimYawWorld = MathF.Atan2(c.AimX - c.X, c.AimZ - c.Z);
- float relYaw = MatchRules.NormalizeAngle(aimYawWorld - c.Yaw);
- vis.AimPivot.Rotation = new Vector3(0f, relYaw, 0f);
- }
-
- UpdateHeatGlow(vis.HeatMat, c.Grid);
+ vis.Visual.SyncMountedGear(c.Grid);
+ VehicleRigAnimator.Update(vis.Visual, c, vel, vis.Visual.Root.Basis, dt);
+ vis.Visual.SyncDamageAndHeat(c.Grid, _debris, vel);
}
}
- private void SyncSpectators(ArenaSession session, float elapsedSeconds)
+ private void SyncSpectators(ArenaSession session, float elapsedSeconds, float dt)
{
var view = session.World.View;
var specs = session.State.Spectators.Spectators;
@@ -281,7 +300,8 @@ public partial class ArenaRenderer : Node3D
{
if (_spectatorVisuals.TryGetValue(s.Id, out var deadVis))
{
- deadVis.Root.Visible = false;
+ deadVis.Visual.SyncDamageAndHeat(s.Grid, _debris, Vector3.Zero);
+ deadVis.Visual.Root.Visible = false;
if (deadVis.LaserMesh is not null)
{
deadVis.LaserMesh.Visible = false;
@@ -292,34 +312,81 @@ public partial class ArenaRenderer : Node3D
if (!_spectatorVisuals.TryGetValue(s.Id, out var vis))
{
- var (primary, accent) = s.Tier switch
+ var role = s.Tier switch
{
- SpectatorTier.Tourist => (TouristGold, TouristIvory),
- SpectatorTier.Patron => (PatronTeal, PatronBronze),
- _ => (ExemplarObsidian, ExemplarCrimson)
+ SpectatorTier.Tourist => VehicleVisualRole.SpectatorTourist,
+ SpectatorTier.Patron => VehicleVisualRole.SpectatorPatron,
+ _ => VehicleVisualRole.SpectatorExemplar
};
- vis = BuildChassisVisual(s.Grid.Spec.Id, primary, accent, isSpectator: true, tier: s.Tier);
- _actorsGroup.AddChild(vis.Root);
+ var instance = VehicleVisualInstance.Create(
+ _assetCache,
+ s.Grid.Spec.Id,
+ role,
+ visualScale: 1f,
+ targetHalfHeight: s.Grid.Spec.HalfHeight);
+ _actorsGroup.AddChild(instance.Root);
+ vis = CreateSpectatorVisualWrapper(instance, s.Grid.Spec);
_spectatorVisuals[s.Id] = vis;
}
- vis.Root.Visible = true;
+ vis.Visual.Root.Visible = true;
var body = session.GetSpectatorBody(s.Id);
Vector3 specPos;
+ Vector3 vel;
if (body is { } h)
{
int lane = h.Lane;
specPos = new Vector3(view.PositionX[lane], view.PositionY[lane], view.PositionZ[lane]);
var q = new Quaternion(view.RotationX[lane], view.RotationY[lane], view.RotationZ[lane], view.RotationW[lane]);
- vis.Root.Transform = new Transform3D(new Basis(q), specPos);
+ vel = new Vector3(view.VelocityX[lane], view.VelocityY[lane], view.VelocityZ[lane]);
+ vis.Visual.Root.Transform = new Transform3D(new Basis(q), specPos);
}
else
{
specPos = new Vector3(s.X, s.Y, s.Z);
- vis.Root.Position = specPos;
- vis.Root.Rotation = new Vector3(0f, s.Yaw, 0f);
+ vel = Vector3.Zero;
+ vis.Visual.Root.Position = specPos;
+ vis.Visual.Root.Rotation = new Vector3(0f, s.Yaw, 0f);
}
+ ContenderState? target = s.TargetContenderId.HasValue &&
+ s.TargetContenderId.Value >= 0 &&
+ s.TargetContenderId.Value < session.State.Contenders.Count
+ ? session.State.Contenders[s.TargetContenderId.Value]
+ : null;
+
+ float aimYawWorld = s.Yaw;
+ float aimPitchDeg = 0f;
+ if (s.Stance != SpectatorStance.Neutral && target is { Alive: true })
+ {
+ float tdx = target.X - s.X;
+ float tdz = target.Z - s.Z;
+ if (tdx * tdx + tdz * tdz > 0.01f)
+ {
+ aimYawWorld = MathF.Atan2(tdx, tdz);
+ float horiz = MathF.Sqrt(tdx * tdx + tdz * tdz);
+ float eyeY = MatchRules.GetObserverEyeY(s.Y, s.Grid.Spec.HalfHeight);
+ float targetY = MatchRules.GetTargetAimY(target.Y, target.Grid.Spec.HalfHeight);
+ aimPitchDeg = Mathf.RadToDeg(MathF.Atan2(targetY - eyeY, horiz));
+ }
+ }
+
+ var rigInput = new VehicleRigDriveInput(
+ Velocity: vel,
+ Yaw: s.Yaw,
+ AimYawWorld: aimYawWorld,
+ AimPitchDeg: aimPitchDeg,
+ SteerCmd: 0f,
+ ThrottleCmd: vel.Length() > 0.5f ? 0.6f : 0f,
+ VerticalThrottleCmd: 0f,
+ EngineThermalPerf: s.Grid.ComputeEngineThermalPerformance(),
+ DeltaTime: dt,
+ IsGrounded: !s.Grid.Spec.IsVtol);
+
+ vis.Visual.SyncMountedGear(s.Grid);
+ VehicleRigAnimator.Update(vis.Visual, in rigInput, vis.Visual.Root.Basis);
+ vis.Visual.SyncDamageAndHeat(s.Grid, _debris, vel);
+
// Status halo
if (vis.HaloMat is not null && vis.HaloMesh is not null)
{
@@ -347,12 +414,6 @@ public partial class ArenaRenderer : Node3D
// Lock-on laser beam when Cleared or Provoked
if (vis.LaserMesh is not null)
{
- ContenderState? target = s.TargetContenderId.HasValue &&
- s.TargetContenderId.Value >= 0 &&
- s.TargetContenderId.Value < session.State.Contenders.Count
- ? session.State.Contenders[s.TargetContenderId.Value]
- : null;
-
if (s.Stance != SpectatorStance.Neutral && s.HasTargetLos && target is { Alive: true })
{
Vector3 start = specPos + new Vector3(0f, s.Grid.Spec.HalfHeight * 0.7f, 0f);
@@ -377,11 +438,57 @@ public partial class ArenaRenderer : Node3D
vis.LaserMesh.Visible = false;
}
}
-
- UpdateHeatGlow(vis.HeatMat, s.Grid);
}
}
+ private ActorVisual CreateSpectatorVisualWrapper(VehicleVisualInstance instance, ChassisSpec spec)
+ {
+ float modelScale = instance.Template.Package.HalfHeight > 1e-4f
+ ? spec.HalfHeight / instance.Template.Package.HalfHeight
+ : 1f;
+ float haloY = MathF.Max(2.2f, instance.Template.MaxY * modelScale - spec.HalfHeight + 0.75f);
+ var haloMat = new StandardMaterial3D
+ {
+ ShadingMode = BaseMaterial3D.ShadingModeEnum.Unshaded,
+ EmissionEnabled = true,
+ Transparency = BaseMaterial3D.TransparencyEnum.Alpha
+ };
+ var halo = new MeshInstance3D
+ {
+ Mesh = new CylinderMesh { TopRadius = 2.4f, BottomRadius = 2.4f, Height = 0.1f, RadialSegments = 16 },
+ MaterialOverride = haloMat,
+ Position = new Vector3(0f, haloY, 0f),
+ CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
+ };
+ instance.Root.AddChild(halo);
+
+ var laserMat = new StandardMaterial3D
+ {
+ AlbedoColor = new Color(1f, 0.15f, 0.15f, 0.85f),
+ EmissionEnabled = true,
+ Emission = new Color(1f, 0.12f, 0.1f),
+ EmissionEnergyMultiplier = 2.2f,
+ ShadingMode = BaseMaterial3D.ShadingModeEnum.Unshaded,
+ Transparency = BaseMaterial3D.TransparencyEnum.Alpha
+ };
+ var laser = new MeshInstance3D
+ {
+ Mesh = new BoxMesh { Size = new Vector3(0.08f, 0.08f, 1.0f) },
+ MaterialOverride = laserMat,
+ Visible = false,
+ CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
+ };
+ _actorsGroup.AddChild(laser);
+
+ return new ActorVisual
+ {
+ Visual = instance,
+ HaloMesh = halo,
+ HaloMat = haloMat,
+ LaserMesh = laser
+ };
+ }
+
private void SyncGroundDrops(MatchState state, float elapsedSeconds)
{
for (int i = 0; i < state.GroundDrops.Count; i++)
@@ -398,7 +505,7 @@ public partial class ArenaRenderer : Node3D
if (!_dropVisuals.TryGetValue(d.Id, out var node))
{
- node = BuildGroundDropVisual(d, state.Terrain);
+ node = BuildGroundDropVisual(d);
_dropsGroup.AddChild(node);
_dropVisuals[d.Id] = node;
}
@@ -473,319 +580,54 @@ public partial class ArenaRenderer : Node3D
}
}
- private Node3D BuildGroundDropVisual(GroundDrop drop, TerrainField terrain)
+ private Node3D BuildGroundDropVisual(GroundDrop drop)
{
var root = new Node3D { Name = $"Drop_{drop.Id}" };
if (drop.Module is { } mod)
{
- var mat = mod.IsImperial ? _imperialCrateMat : _humanCrateMat;
+ var beaconMat = mod.IsImperial ? _imperialBeaconMat : _humanBeaconMat;
root.AddChild(new MeshInstance3D
{
- Mesh = new BoxMesh { Size = new Vector3(1.1f, 0.8f, 1.1f) },
- MaterialOverride = mat,
- Position = new Vector3(0f, 0.5f, 0f)
+ Mesh = new CylinderMesh { TopRadius = 0.72f, BottomRadius = 0.85f, Height = 0.14f, RadialSegments = 14 },
+ MaterialOverride = beaconMat,
+ Position = new Vector3(0f, 0.08f, 0f)
});
+
+ // Real Brokkr gear GLB hovering cleanly above the salvage beacon disc
+ Node3D gear = VehicleVisualInstance.CreateStandaloneGearVisual(_assetCache, mod);
+ gear.Scale *= 1.45f;
+ gear.Position += new Vector3(0f, 0.35f, 0f);
+ root.AddChild(gear);
+
if (mod.IsImperial)
{
root.AddChild(new MeshInstance3D
{
- Mesh = new CylinderMesh { TopRadius = 0.08f, BottomRadius = 0.25f, Height = 5.5f, RadialSegments = 8 },
- MaterialOverride = _imperialCrateMat,
- Position = new Vector3(0f, 3.0f, 0f)
+ Mesh = new CylinderMesh { TopRadius = 0.06f, BottomRadius = 0.22f, Height = 5.5f, RadialSegments = 8 },
+ MaterialOverride = _imperialBeaconMat,
+ Position = new Vector3(0f, 2.9f, 0f)
});
}
}
else if (drop.Chassis is { } ch)
{
- var hull = BuildChassisVisual(ch.Id, new Color(0.32f, 0.35f, 0.36f), CyanSalvage, isSpectator: false, tier: SpectatorTier.Patron);
- hull.Root.Position = new Vector3(0f, ch.HalfHeight * 0.85f, 0f);
- hull.Root.Scale = new Vector3(0.85f, 0.85f, 0.85f);
+ const float dropScale = 0.85f;
+ var hull = VehicleVisualInstance.Create(_assetCache, ch.Id, VehicleVisualRole.SalvageDrop, dropScale);
+ // ModelRoot sits at -HalfHeight*dropScale inside hull.Root, so lifting hull.Root by +HalfHeight*dropScale grounds minY=0 on terrain
+ hull.Root.Position = new Vector3(0f, ch.HalfHeight * dropScale, 0f);
root.AddChild(hull.Root);
var ring = new MeshInstance3D
{
Mesh = new CylinderMesh { TopRadius = 2.6f, BottomRadius = 2.8f, Height = 0.14f, RadialSegments = 18 },
MaterialOverride = _cyanBeaconMat,
- Position = new Vector3(0f, 0.15f, 0f)
+ Position = new Vector3(0f, 0.12f, 0f)
};
root.AddChild(ring);
}
return root;
}
- private ActorVisual BuildChassisVisual(
- string chassisId,
- Color primary,
- Color accent,
- bool isSpectator,
- SpectatorTier tier)
- {
- var root = new Node3D { Name = $"Chassis_{chassisId}" };
- var bodyMat = new StandardMaterial3D { AlbedoColor = primary, Roughness = 0.55f, Metallic = 0.45f };
- var accentMat = new StandardMaterial3D { AlbedoColor = accent, Roughness = 0.45f, Metallic = 0.55f };
- var darkMat = new StandardMaterial3D { AlbedoColor = new Color(0.14f, 0.15f, 0.14f), Roughness = 0.8f, Metallic = 0.35f };
- var heatMat = new StandardMaterial3D
- {
- AlbedoColor = accent,
- Roughness = 0.4f,
- Metallic = 0.6f,
- EmissionEnabled = true,
- Emission = new Color(1.0f, 0.35f, 0.1f),
- EmissionEnergyMultiplier = 0.0f
- };
-
- var aimPivot = new Node3D { Name = "AimPivot" };
- root.AddChild(aimPivot);
-
- switch (chassisId)
- {
- case "buggy":
- case "jackal":
- BuildBuggyParts(root, aimPivot, bodyMat, accentMat, darkMat, heatMat);
- break;
- case "technical":
- case "mule":
- BuildTechnicalParts(root, aimPivot, bodyMat, accentMat, darkMat, heatMat);
- break;
- case "tank":
- case "anvil":
- case "ironclad":
- case "ferret":
- case "mammoth":
- BuildTankParts(root, aimPivot, bodyMat, accentMat, darkMat, heatMat);
- break;
- case "hornet":
- case "condor":
- BuildVtolParts(root, aimPivot, bodyMat, accentMat, darkMat, heatMat, isHeavy: chassisId == "condor");
- break;
- case "manticore":
- case "bastion":
- BuildManticoreParts(root, aimPivot, bodyMat, accentMat, darkMat, heatMat);
- break;
- case "raptor":
- case "spectre":
- case "kestrel":
- case "exemplar_frame":
- BuildRaptorParts(root, aimPivot, bodyMat, accentMat, darkMat, heatMat, isExemplar: chassisId == "exemplar_frame");
- break;
- case "paladin":
- case "breaker":
- BuildPaladinParts(root, aimPivot, bodyMat, accentMat, darkMat, heatMat, isTourist: isSpectator && tier == SpectatorTier.Tourist);
- break;
- default:
- BuildBuggyParts(root, aimPivot, bodyMat, accentMat, darkMat, heatMat);
- break;
- }
-
- var vis = new ActorVisual
- {
- Root = root,
- AimPivot = aimPivot,
- HeatMat = heatMat,
- ChassisId = chassisId
- };
-
- if (isSpectator)
- {
- var haloMat = new StandardMaterial3D
- {
- ShadingMode = BaseMaterial3D.ShadingModeEnum.Unshaded,
- EmissionEnabled = true,
- Transparency = BaseMaterial3D.TransparencyEnum.Alpha
- };
- var halo = new MeshInstance3D
- {
- Mesh = new CylinderMesh { TopRadius = 2.4f, BottomRadius = 2.4f, Height = 0.1f, RadialSegments = 16 },
- MaterialOverride = haloMat,
- Position = new Vector3(0f, 3.3f, 0f),
- CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
- };
- root.AddChild(halo);
- vis.HaloMesh = halo;
- vis.HaloMat = haloMat;
-
- var laserMat = new StandardMaterial3D
- {
- AlbedoColor = new Color(1f, 0.15f, 0.15f, 0.85f),
- EmissionEnabled = true,
- Emission = new Color(1f, 0.12f, 0.1f),
- EmissionEnergyMultiplier = 2.2f,
- ShadingMode = BaseMaterial3D.ShadingModeEnum.Unshaded,
- Transparency = BaseMaterial3D.TransparencyEnum.Alpha
- };
- var laser = new MeshInstance3D
- {
- Mesh = new BoxMesh { Size = new Vector3(0.08f, 0.08f, 1.0f) },
- MaterialOverride = laserMat,
- Visible = false,
- CastShadow = GeometryInstance3D.ShadowCastingSetting.Off
- };
- _actorsGroup.AddChild(laser);
- vis.LaserMesh = laser;
- vis.LaserMat = laserMat;
- }
-
- return vis;
- }
-
- private static void BuildBuggyParts(Node3D root, Node3D aimPivot, Material bodyMat, Material accentMat, Material darkMat, Material heatMat)
- {
- AddBox(root, new Vector3(1.5f, 0.45f, 2.9f), new Vector3(0f, -0.25f, 0f), bodyMat);
- AddBox(root, new Vector3(1.1f, 0.45f, 1.3f), new Vector3(0f, 0.25f, -0.2f), accentMat); // rollcage canopy
- AddBox(root, new Vector3(0.8f, 0.32f, 0.5f), new Vector3(0f, 0.15f, -1.35f), heatMat); // rear radiator
- // 4 wheels
- AddWheel(root, new Vector3(-0.98f, -0.45f, 1.05f), 0.36f, 0.32f, darkMat);
- AddWheel(root, new Vector3( 0.98f, -0.45f, 1.05f), 0.36f, 0.32f, darkMat);
- AddWheel(root, new Vector3(-0.98f, -0.45f, -1.05f), 0.36f, 0.32f, darkMat);
- AddWheel(root, new Vector3( 0.98f, -0.45f, -1.05f), 0.36f, 0.32f, darkMat);
- // Roof MG turret
- aimPivot.Position = new Vector3(0f, 0.6f, 0f);
- AddBox(aimPivot, new Vector3(0.45f, 0.26f, 0.55f), Vector3.Zero, darkMat);
- AddBox(aimPivot, new Vector3(0.12f, 0.12f, 1.25f), new Vector3(0f, 0.04f, 0.65f), accentMat);
- }
-
- private static void BuildTechnicalParts(Node3D root, Node3D aimPivot, Material bodyMat, Material accentMat, Material darkMat, Material heatMat)
- {
- AddBox(root, new Vector3(2.0f, 0.55f, 4.2f), new Vector3(0f, -0.25f, 0f), bodyMat);
- AddBox(root, new Vector3(1.85f, 0.72f, 1.5f), new Vector3(0f, 0.35f, 0.85f), accentMat); // armored cab
- AddBox(root, new Vector3(1.9f, 0.38f, 2.0f), new Vector3(0f, 0.20f, -0.95f), bodyMat); // bed walls
- AddBox(root, new Vector3(1.2f, 0.30f, 0.4f), new Vector3(0f, 0.20f, -2.0f), heatMat); // tail radiator
- AddWheel(root, new Vector3(-1.12f, -0.55f, 1.45f), 0.44f, 0.36f, darkMat);
- AddWheel(root, new Vector3( 1.12f, -0.55f, 1.45f), 0.44f, 0.36f, darkMat);
- AddWheel(root, new Vector3(-1.12f, -0.55f, -1.45f), 0.44f, 0.36f, darkMat);
- AddWheel(root, new Vector3( 1.12f, -0.55f, -1.45f), 0.44f, 0.36f, darkMat);
- aimPivot.Position = new Vector3(0f, 0.72f, -0.8f);
- AddBox(aimPivot, new Vector3(0.65f, 0.4f, 0.7f), Vector3.Zero, darkMat);
- AddBox(aimPivot, new Vector3(0.14f, 0.14f, 1.7f), new Vector3(-0.18f, 0.08f, 0.8f), accentMat);
- AddBox(aimPivot, new Vector3(0.14f, 0.14f, 1.7f), new Vector3( 0.18f, 0.08f, 0.8f), accentMat);
- }
-
- private static void BuildTankParts(Node3D root, Node3D aimPivot, Material bodyMat, Material accentMat, Material darkMat, Material heatMat)
- {
- AddBox(root, new Vector3(2.4f, 0.85f, 4.6f), new Vector3(0f, -0.2f, 0f), bodyMat);
- AddBox(root, new Vector3(0.55f, 0.72f, 4.9f), new Vector3(-1.32f, -0.45f, 0f), darkMat); // left track pod
- AddBox(root, new Vector3(0.55f, 0.72f, 4.9f), new Vector3( 1.32f, -0.45f, 0f), darkMat); // right track pod
- AddBox(root, new Vector3(1.6f, 0.35f, 0.7f), new Vector3(0f, 0.25f, -1.7f), heatMat); // rear engine deck
- aimPivot.Position = new Vector3(0f, 0.65f, 0.2f);
- AddBox(aimPivot, new Vector3(1.75f, 0.68f, 2.2f), Vector3.Zero, accentMat);
- AddBox(aimPivot, new Vector3(0.24f, 0.24f, 3.0f), new Vector3(0f, 0.05f, 2.0f), darkMat); // main cannon
- }
-
- private static void BuildManticoreParts(Node3D root, Node3D aimPivot, Material bodyMat, Material accentMat, Material darkMat, Material heatMat)
- {
- // Wide dorsal carapace + 4 splayed articulated legs + radiator flanks
- AddBox(root, new Vector3(2.2f, 0.75f, 2.8f), new Vector3(0f, 0.25f, 0f), bodyMat);
- AddBox(root, new Vector3(1.3f, 0.55f, 1.2f), new Vector3(0f, 0.45f, 1.2f), accentMat); // chin armor
- AddBox(root, new Vector3(0.45f, 0.45f, 1.4f), new Vector3(-1.25f, 0.45f, -0.4f), heatMat);
- AddBox(root, new Vector3(0.45f, 0.45f, 1.4f), new Vector3( 1.25f, 0.45f, -0.4f), heatMat);
-
- Vector3[] legAnchors =
- {
- new(-1.35f, -0.35f, 1.25f),
- new( 1.35f, -0.35f, 1.25f),
- new(-1.35f, -0.35f, -1.25f),
- new( 1.35f, -0.35f, -1.25f)
- };
- for (int i = 0; i < legAnchors.Length; i++)
- {
- AddBox(root, new Vector3(0.42f, 1.35f, 0.42f), legAnchors[i], darkMat);
- AddBox(root, new Vector3(0.68f, 0.26f, 0.68f), legAnchors[i] + new Vector3(0f, -0.7f, 0f), accentMat);
- }
-
- aimPivot.Position = new Vector3(0f, 0.8f, 0.2f);
- AddBox(aimPivot, new Vector3(0.9f, 0.38f, 1.0f), Vector3.Zero, darkMat);
- AddBox(aimPivot, new Vector3(0.18f, 0.18f, 1.7f), new Vector3(-0.6f, 0f, 0.75f), accentMat);
- AddBox(aimPivot, new Vector3(0.18f, 0.18f, 1.7f), new Vector3( 0.6f, 0f, 0.75f), accentMat);
- }
-
- private static void BuildRaptorParts(Node3D root, Node3D aimPivot, Material bodyMat, Material accentMat, Material darkMat, Material heatMat, bool isExemplar)
- {
- float scale = isExemplar ? 0.78f : 1.0f;
- // Tall bipedal legs
- AddBox(root, new Vector3(0.42f * scale, 1.85f * scale, 0.52f * scale), new Vector3(-0.62f * scale, -0.8f * scale, 0f), darkMat);
- AddBox(root, new Vector3(0.42f * scale, 1.85f * scale, 0.52f * scale), new Vector3( 0.62f * scale, -0.8f * scale, 0f), darkMat);
- // Angled torso + head sensor
- AddBox(root, new Vector3(1.55f * scale, 1.15f * scale, 1.25f * scale), new Vector3(0f, 0.45f * scale, 0f), bodyMat);
- AddBox(root, new Vector3(0.65f * scale, 0.45f * scale, 0.85f * scale), new Vector3(0f, 1.25f * scale, 0.25f * scale), accentMat);
- AddBox(root, new Vector3(0.95f * scale, 0.55f * scale, 0.35f * scale), new Vector3(0f, 0.6f * scale, -0.75f * scale), heatMat);
-
- aimPivot.Position = new Vector3(0f, 0.5f * scale, 0f);
- AddBox(aimPivot, new Vector3(0.32f * scale, 0.35f * scale, 1.55f * scale), new Vector3(-1.05f * scale, 0f, 0.55f * scale), accentMat);
- AddBox(aimPivot, new Vector3(0.32f * scale, 0.35f * scale, 1.55f * scale), new Vector3( 1.05f * scale, 0f, 0.55f * scale), accentMat);
- }
-
- private static void BuildPaladinParts(Node3D root, Node3D aimPivot, Material bodyMat, Material accentMat, Material darkMat, Material heatMat, bool isTourist)
- {
- // Broad heavy bipedal silhouette + shoulder pods + chest reactor
- AddBox(root, new Vector3(0.68f, 2.0f, 0.82f), new Vector3(-0.85f, -1.1f, 0f), darkMat);
- AddBox(root, new Vector3(0.68f, 2.0f, 0.82f), new Vector3( 0.85f, -1.1f, 0f), darkMat);
- AddBox(root, new Vector3(2.25f, 1.65f, 1.65f), new Vector3(0f, 0.45f, 0f), bodyMat);
- AddBox(root, new Vector3(0.85f, 0.75f, 0.35f), new Vector3(0f, 0.55f, 0.85f), heatMat); // chest reactor
- // Twin shoulder pods
- AddBox(root, new Vector3(0.75f, 0.72f, 1.15f), new Vector3(-1.35f, 1.45f, 0f), accentMat);
- AddBox(root, new Vector3(0.75f, 0.72f, 1.15f), new Vector3( 1.35f, 1.45f, 0f), accentMat);
-
- if (isTourist)
- {
- // Gaudy gilded luxury dome on top so Tourists are unmistakable loot pinatas
- var dome = new MeshInstance3D
- {
- Mesh = new CylinderMesh { TopRadius = 0.5f, BottomRadius = 0.95f, Height = 0.75f, RadialSegments = 12 },
- MaterialOverride = bodyMat,
- Position = new Vector3(0f, 1.65f, 0f)
- };
- root.AddChild(dome);
- }
-
- aimPivot.Position = new Vector3(0f, 0.45f, 0f);
- AddBox(aimPivot, new Vector3(0.45f, 0.48f, 2.0f), new Vector3(-1.45f, 0f, 0.65f), accentMat);
- AddBox(aimPivot, new Vector3(0.45f, 0.48f, 2.0f), new Vector3( 1.45f, 0f, 0.65f), accentMat);
- }
-
- private static void BuildVtolParts(Node3D root, Node3D aimPivot, Material bodyMat, Material accentMat, Material darkMat, Material heatMat, bool isHeavy)
- {
- float s = isHeavy ? 1.3f : 1.0f;
- AddBox(root, new Vector3(1.1f * s, 0.7f * s, 3.4f * s), Vector3.Zero, bodyMat);
- AddBox(root, new Vector3(3.1f * s, 0.16f * s, 0.9f * s), new Vector3(0f, 0.15f * s, -0.2f * s), accentMat);
- AddBox(root, new Vector3(0.65f * s, 0.35f * s, 0.9f * s), new Vector3(-1.35f * s, 0.25f * s, -0.15f * s), darkMat);
- AddBox(root, new Vector3(0.65f * s, 0.35f * s, 0.9f * s), new Vector3( 1.35f * s, 0.25f * s, -0.15f * s), darkMat);
- AddBox(root, new Vector3(0.6f * s, 0.32f * s, 0.6f * s), new Vector3(0f, 0.25f * s, -1.4f * s), heatMat);
- aimPivot.Position = new Vector3(0f, -0.25f * s, 1.3f * s);
- AddBox(aimPivot, new Vector3(0.32f * s, 0.24f * s, 0.9f * s), new Vector3(0f, 0f, 0.35f * s), accentMat);
- }
-
- private static void AddBox(Node3D parent, Vector3 size, Vector3 pos, Material mat)
- {
- parent.AddChild(new MeshInstance3D
- {
- Mesh = new BoxMesh { Size = size },
- MaterialOverride = mat,
- Position = pos
- });
- }
-
- private static void AddWheel(Node3D parent, Vector3 pos, float radius, float width, Material mat)
- {
- parent.AddChild(new MeshInstance3D
- {
- Mesh = new CylinderMesh { TopRadius = radius, BottomRadius = radius, Height = width, RadialSegments = 12 },
- MaterialOverride = mat,
- Position = pos,
- Rotation = new Vector3(0f, 0f, MathF.PI * 0.5f)
- });
- }
-
- private static void UpdateHeatGlow(StandardMaterial3D heatMat, ChassisGrid grid)
- {
- float maxTemp = ChassisGrid.AmbientTemp;
- for (int i = 0; i < grid.Mounted.Count; i++)
- {
- maxTemp = MathF.Max(maxTemp, grid.Mounted[i].CurrentTemp);
- }
- float glow = Math.Clamp((maxTemp - 65f) / 110f, 0f, 1f);
- heatMat.EmissionEnergyMultiplier = glow * 2.6f;
- }
-
private static void ClearChildren(Node parent)
{
for (int i = parent.GetChildCount() - 1; i >= 0; i--)
diff --git a/client/Main.cs b/client/Main.cs
index 0fd9f6d..33cd669 100644
--- a/client/Main.cs
+++ b/client/Main.cs
@@ -83,7 +83,7 @@ public partial class Main : Node3D
_terrain.InvalidateAll();
_terrain.Sync(_session.State.Terrain);
- _arena.Sync(_session, _elapsedSeconds);
+ _arena.Sync(_session, _elapsedSeconds, 0f);
UpdateCamera(1f);
_hud.Sync(_session);
@@ -187,7 +187,7 @@ public partial class Main : Node3D
_session.Step(input);
_terrain.Sync(_session.State.Terrain);
- _arena.Sync(_session, _elapsedSeconds);
+ _arena.Sync(_session, _elapsedSeconds, (float)delta);
}
public override void _Process(double delta)
diff --git a/client/VehicleRigAnimator.cs b/client/VehicleRigAnimator.cs
new file mode 100644
index 0000000..b1b500d
--- /dev/null
+++ b/client/VehicleRigAnimator.cs
@@ -0,0 +1,830 @@
+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);
+
+///
+/// Drives real-time secondary mechanism animations on
+/// from live physics state with strict NaN/Inf guards:
+/// - Wheeled: wheel spin (v / r), front-axle steering, subtle chassis suspension roll/pitch.
+/// - Turrets/Torsos/Arms: yaw tracking (joint.turret_yaw, joint.torso_yaw) & arm pitch.
+/// - VTOL: rotor spin scaled by EngineThermalPerf + 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.
+///
+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 Struts = new(StringComparer.Ordinal);
+ public readonly Dictionary OleoNodes = new(StringComparer.Ordinal);
+ public readonly List Scissors = new();
+ public readonly List Rams = new();
+ }
+
+ 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 static void Update(
+ VehicleVisualInstance visual,
+ ContenderState c,
+ Vector3 velocity,
+ Basis worldBasis,
+ float dt)
+ {
+ 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);
+ }
+
+ public static void Update(
+ VehicleVisualInstance visual,
+ in VehicleRigDriveInput input,
+ Basis worldBasis)
+ {
+ float dt = Sanitize(input.DeltaTime, 0f, 0f, 0.1f);
+ 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)
+ {
+ 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.09f);
+ StepBreakerMechanisms(visual, st, phiL, phiR, velY, dt);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ 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);
+ break;
+ }
+ }
+
+ 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 AnimateLocalPitchLeg(
+ VehicleVisualInstance visual,
+ string thighName,
+ string shinName,
+ string footName,
+ float phase,
+ float amp,
+ float hipSign,
+ float kneeSign)
+ {
+ 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;
+
+ RotateLocalX(visual, thighName, hipAng);
+ RotateLocalX(visual, shinName, kneeAng);
+ RotateLocalX(visual, footName, footAng);
+ }
+
+ private static void ApplyPelvisBob(
+ VehicleVisualInstance visual,
+ string rootNodeName,
+ float phase,
+ float amp,
+ float bobScale)
+ {
+ if (visual.GetNamedNode(rootNodeName) is not { } node ||
+ !visual.TryGetRestLocal(rootNodeName, out var rest))
+ {
+ return;
+ }
+
+ float bobY = (MathF.Abs(MathF.Sin(phase)) - 0.5f) * bobScale * amp;
+ float swayRoll = MathF.Cos(phase) * 0.032f * 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)
+ 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 RotateLocalX(VehicleVisualInstance visual, string nodeName, float angleRad)
+ {
+ if (visual.GetNamedNode(nodeName) is not { } node ||
+ !visual.TryGetRestLocal(nodeName, out var rest))
+ {
+ return;
+ }
+ Basis delta = new Basis(Vector3.Right, angleRad);
+ node.Transform = new Transform3D(rest.Basis * delta, rest.Origin);
+ }
+
+ 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);
+}