Walker locomotion: bold bent-knee strides, slope climb, crisp yaw stop

This commit is contained in:
Meta Vens 2026-12-05 14:18:08 +01:00
commit c789fe8af8
4 changed files with 493 additions and 61 deletions

View file

@ -5,6 +5,7 @@ using RustToDust.Core;
using RustToDust.Ymir;
using Xunit;
using Xunit.Abstractions;
using Ymir.Box3D;
namespace RustToDust.Tests
{
@ -1051,5 +1052,386 @@ namespace RustToDust.Tests
Assert.True(maxRelCom <= modelRide + 0.25f, $"Bastion idle COM floated too high: {maxRelCom:F3}m > {modelRide + 0.25f:F3}m");
Assert.True(maxReachOverflow <= 0.05f, $"Bastion idle leg hyper-extended past MaxLegReach by {maxReachOverflow:F4}m > 0.05m");
}
private static (TerrainField Terrain, float[] Heights, int GridRes, float CellSize, float MinY, float MaxY) MakeSyntheticSlope(float slopeDeg)
{
const float sizeMeters = 400f;
const int gridRes = 201;
const float cellSize = sizeMeters / (gridRes - 1);
float half = sizeMeters * 0.5f;
float tan = MathF.Tan(slopeDeg * MathF.PI / 180f);
var heights = new float[gridRes * gridRes];
for (int iz = 0; iz < gridRes; iz++)
{
float z = -half + iz * cellSize;
float h = (z + half) * tan;
int row = iz * gridRes;
for (int ix = 0; ix < gridRes; ix++)
{
heights[row + ix] = h;
}
}
float minY = -10f;
float maxY = sizeMeters * tan + 10f;
var terrain = TerrainField.LoadHeightmap(sizeMeters, gridRes, heights, minY, maxY, seed: 1UL);
return (terrain, heights, gridRes, cellSize, minY, maxY);
}
private static (Box3DSoaWorld World, Box3DSoaBodyHandle Body, WalkerState Walker, ChassisGrid Grid) SpawnWalkerOnSyntheticTerrain(
ChassisSpec spec,
in (TerrainField Terrain, float[] Heights, int GridRes, float CellSize, float MinY, float MaxY) syn,
float startX,
float startZ)
{
var world = new Box3DSoaWorld(capacity: 8, gravityX: 0f, gravityY: -20f, gravityZ: 0f);
float half = syn.Terrain.MapHalfExtent;
world.AddStaticHeightField(
1UL,
(-half, 0f, -half),
syn.Heights,
syn.GridRes,
syn.GridRes,
(syn.CellSize, 1.0f, syn.CellSize),
syn.MinY,
syn.MaxY,
friction: 0.15f,
restitution: 0.0f);
float physHalfH = ArenaSession.GetPhysicsHalfHeight(spec);
float volume = 8f * spec.HalfWidth * physHalfH * spec.HalfLength;
float density = spec.MassKg / MathF.Max(0.1f, volume);
Quaternion q0 = Quaternion.Identity;
if (spec.Plane == GridPlane.Dorsal)
{
var (nx, ny, nz) = syn.Terrain.SampleNormal(startX, startZ);
Vector3 desUp = Vector3.Normalize(Vector3.Lerp(Vector3.UnitY, new Vector3(nx, ny, nz), 0.90f));
Vector3 axis = Vector3.Cross(Vector3.UnitY, desUp);
if (axis.LengthSquared() > 1e-8f)
{
float ang = MathF.Min(0.295f, MathF.Acos(Math.Clamp(desUp.Y, -1f, 1f)));
q0 = Quaternion.Normalize(Quaternion.CreateFromAxisAngle(Vector3.Normalize(axis), ang));
}
}
float spawnY = syn.Terrain.SampleHeight(startX, startZ) + WalkerLocomotion.GetRideHeight(spec);
var body = world.AddBox(
1000UL,
(startX, spawnY, startZ),
(spec.HalfWidth, physHalfH, spec.HalfLength),
density,
(q0.X, q0.Y, q0.Z, q0.W),
friction: 0.2f,
restitution: 0.1f);
var grid = new ChassisGrid(spec);
var ws = new WalkerState(spec);
ws.InitializePose(new Vector3(startX, spawnY, startZ), q0, world.View.Mass[body.Lane], syn.Terrain);
return (world, body, ws, grid);
}
[Fact]
public void HybridB_All7Mechs_BoldWeightedStrides_NoTinyBabySteps()
{
var specs = new[]
{
Catalog.Breaker,
Catalog.Paladin,
Catalog.Raptor,
Catalog.Spectre,
Catalog.Kestrel,
Catalog.Manticore,
Catalog.Bastion
};
var flat = MakeSyntheticSlope(0f);
foreach (var spec in specs)
{
var (world, body, ws, grid) = SpawnWalkerOnSyntheticTerrain(spec, flat, 0f, -140f);
using (world)
{
int lane = body.Lane;
var view = world.View;
int nLegs = ws.Geom.LegCount;
var prevPlanted = new bool[nLegs];
var lastPlantTime = new float[nLegs];
for (int i = 0; i < nLegs; i++)
{
prevPlanted[i] = ws.Planted[i];
lastPlantTime[i] = -1f;
}
var minHipPitch = new float[nLegs];
var maxHipPitch = new float[nLegs];
Array.Fill(minHipPitch, float.PositiveInfinity);
Array.Fill(maxHipPitch, float.NegativeInfinity);
double stepDurSum = 0.0;
int stepDurCount = 0;
double footSepSum = 0.0;
double swingTravelSum = 0.0;
int strideCount = 0;
for (int t = 0; t < 180; t++)
{
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]);
var vel = new Vector3(view.VelocityX[lane], view.VelocityY[lane], view.VelocityZ[lane]);
var angVel = new Vector3(view.AngularVelocityX[lane], view.AngularVelocityY[lane], view.AngularVelocityZ[lane]);
WalkerLocomotion.StepWalker(
ws, grid, flat.Terrain, view, lane, view.Mass[lane],
pos, q, vel, angVel,
new Vector3(0f, 0f, grid.Spec.MaxSpeed), 0f, false, ArenaSession.FixedDt);
world.Step(ArenaSession.FixedDt, ArenaSession.SubStepCount);
if (t >= 60)
{
for (int i = 0; i < nLegs; i++)
{
float hp = ws.Actuators[i].Theta.HipPitch;
if (hp < minHipPitch[i]) minHipPitch[i] = hp;
if (hp > maxHipPitch[i]) maxHipPitch[i] = hp;
bool curPlanted = ws.Planted[i];
if (!prevPlanted[i] && curPlanted)
{
if (lastPlantTime[i] >= 0f)
{
float cycleSec = ws.SimTime - lastPlantTime[i];
stepDurSum += cycleSec * 0.5f;
stepDurCount++;
}
lastPlantTime[i] = ws.SimTime;
int partner = i ^ 1;
float foreAftSep = MathF.Abs(ws.FootSole[i].Z - ws.FootSole[partner].Z);
float swingTravel = Vector2.Distance(
new Vector2(ws.SwingTargetSole[i].X, ws.SwingTargetSole[i].Z),
new Vector2(ws.SwingStartSole[i].X, ws.SwingStartSole[i].Z));
footSepSum += foreAftSep;
swingTravelSum += swingTravel;
strideCount++;
}
prevPlanted[i] = curPlanted;
}
}
else
{
for (int i = 0; i < nLegs; i++)
{
prevPlanted[i] = ws.Planted[i];
}
}
}
Assert.True(stepDurCount >= 2, $"{spec.Id}: expected >= 2 measured step intervals, got {stepDurCount}");
Assert.True(strideCount >= 2, $"{spec.Id}: expected >= 2 measured strides, got {strideCount}");
float avgStepDur = (float)(stepDurSum / stepDurCount);
float avgFootSep = (float)(footSepSum / strideCount);
float avgSwingTravel = (float)(swingTravelSum / strideCount);
float maxHipSwingRad = 0f;
for (int i = 0; i < nLegs; i++)
{
maxHipSwingRad = MathF.Max(maxHipSwingRad, maxHipPitch[i] - minHipPitch[i]);
}
bool isCompact = spec.Id == "breaker" || spec.Id == "manticore";
float minFootSep = isCompact ? 1.25f : 1.80f;
_output.WriteLine(
$"{spec.Id,-10} stepDur={avgStepDur:F3}s ({1f / avgStepDur:F2} Hz) footToFoot={avgFootSep:F2}m " +
$"swingTravel={avgSwingTravel:F2}m hipPitchP2P={maxHipSwingRad:F3}rad ({maxHipSwingRad * 180f / MathF.PI:F1}deg)");
Assert.True(avgStepDur >= 0.21f,
$"{spec.Id}: tiny baby-step cadence {avgStepDur:F3}s < 0.21s");
Assert.True(avgFootSep >= minFootSep,
$"{spec.Id}: foot-to-foot stride {avgFootSep:F2}m < {minFootSep:F2}m");
Assert.True(maxHipSwingRad >= 0.45f,
$"{spec.Id}: insufficient hip-pitch actuator swing {maxHipSwingRad:F3} rad < 0.45 rad");
}
}
}
[Fact]
public void HybridB_All7Mechs_ClimbSmallAndMediumSlopes_6To22Deg_NeverDefeated()
{
var specs = new[]
{
Catalog.Breaker,
Catalog.Paladin,
Catalog.Raptor,
Catalog.Spectre,
Catalog.Kestrel,
Catalog.Manticore,
Catalog.Bastion
};
float[] slopesDeg = { 6f, 12f, 18f, 22f };
var flat = MakeSyntheticSlope(0f);
var slopeTerrains = new (float Deg, (TerrainField Terrain, float[] Heights, int GridRes, float CellSize, float MinY, float MaxY) Syn)[slopesDeg.Length];
for (int s = 0; s < slopesDeg.Length; s++)
{
slopeTerrains[s] = (slopesDeg[s], MakeSyntheticSlope(slopesDeg[s]));
}
foreach (var spec in specs)
{
float flatSpeed;
{
var (world, body, ws, grid) = SpawnWalkerOnSyntheticTerrain(spec, flat, 0f, -140f);
using (world)
{
int lane = body.Lane;
var view = world.View;
for (int t = 0; t < 150; t++)
{
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]);
var vel = new Vector3(view.VelocityX[lane], view.VelocityY[lane], view.VelocityZ[lane]);
var angVel = new Vector3(view.AngularVelocityX[lane], view.AngularVelocityY[lane], view.AngularVelocityZ[lane]);
WalkerLocomotion.StepWalker(
ws, grid, flat.Terrain, view, lane, view.Mass[lane],
pos, q, vel, angVel,
new Vector3(0f, 0f, grid.Spec.MaxSpeed), 0f, false, ArenaSession.FixedDt);
world.Step(ArenaSession.FixedDt, ArenaSession.SubStepCount);
}
flatSpeed = MathF.Sqrt(view.VelocityX[lane] * view.VelocityX[lane] + view.VelocityZ[lane] * view.VelocityZ[lane]);
}
}
var summary = $"{spec.Id,-10} flat={flatSpeed:F2}m/s";
foreach (var (deg, syn) in slopeTerrains)
{
var (world, body, ws, grid) = SpawnWalkerOnSyntheticTerrain(spec, syn, 0f, -140f);
using (world)
{
int lane = body.Lane;
var view = world.View;
float gStart = syn.Terrain.SampleHeight(0f, -140f);
float minSpeedAfterAccel = float.PositiveInfinity;
for (int t = 0; t < 150; t++)
{
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]);
var vel = new Vector3(view.VelocityX[lane], view.VelocityY[lane], view.VelocityZ[lane]);
var angVel = new Vector3(view.AngularVelocityX[lane], view.AngularVelocityY[lane], view.AngularVelocityZ[lane]);
WalkerLocomotion.StepWalker(
ws, grid, syn.Terrain, view, lane, view.Mass[lane],
pos, q, vel, angVel,
new Vector3(0f, 0f, grid.Spec.MaxSpeed), 0f, false, ArenaSession.FixedDt);
world.Step(ArenaSession.FixedDt, ArenaSession.SubStepCount);
if (t >= 30)
{
float fwdSpd = view.VelocityZ[lane];
if (fwdSpd < minSpeedAfterAccel) minSpeedAfterAccel = fwdSpd;
}
}
float endZ = view.PositionZ[lane];
float endHorizSpeed = MathF.Sqrt(view.VelocityX[lane] * view.VelocityX[lane] + view.VelocityZ[lane] * view.VelocityZ[lane]);
float gEnd = syn.Terrain.SampleHeight(view.PositionX[lane], endZ);
float climbM = gEnd - gStart;
float dispM = endZ - (-140f);
float ratio = endHorizSpeed / MathF.Max(1f, flatSpeed);
float reqRatio = deg <= 12f ? 0.60f : 0.45f;
summary += $" | {deg:F0}deg:{endHorizSpeed:F1}m/s({ratio * 100f:F0}%,+{climbM:F1}m,min={minSpeedAfterAccel:F1})";
Assert.True(dispM > 12f && climbM > 1.2f,
$"{spec.Id} @ {deg}deg: failed climb (disp={dispM:F2}m, climb={climbM:F2}m)");
Assert.True(minSpeedAfterAccel > 4.0f,
$"{spec.Id} @ {deg}deg: stalled or kicked back (minFwdSpd={minSpeedAfterAccel:F2}m/s)");
Assert.True(ratio >= reqRatio,
$"{spec.Id} @ {deg}deg: speed {endHorizSpeed:F2}m/s ({ratio * 100f:F1}% of flat {flatSpeed:F2}) < {reqRatio * 100f:F0}%");
}
}
_output.WriteLine(summary);
}
}
[Fact]
public void HybridB_All7Mechs_YawRelease_StopsCrisplyWithMinimalCoast()
{
var specs = new[]
{
Catalog.Breaker,
Catalog.Paladin,
Catalog.Raptor,
Catalog.Spectre,
Catalog.Kestrel,
Catalog.Manticore,
Catalog.Bastion
};
foreach (var spec in specs)
{
using var session = new ArenaSession(seed: 42, mapKind: ArenaMapKind.Desert, slot0Human: true);
for (int i = 1; i < session.State.Contenders.Count; i++)
{
session.SetContenderPose(i, 500f + i * 16f, 500f);
}
session.SetContenderPose(0, 0f, 0f, 0f);
session.State.SpawnChassisDrop(0f, 0f, spec);
Assert.True(session.TrySwapNearbyChassis(0), $"Failed swap to {spec.Id}");
session.SetContenderPose(0, 0f, 0f, 0f);
var player = session.State.Contenders[0];
int lane = session.GetContenderBody(player.Id)!.Value.Lane;
// Spin 1.0s at full steer
for (int t = 0; t < 60; t++)
{
session.SetHumanInput(0, new PlayerInput(
Throttle: 0f,
Steer: 1.0f,
AimWorldX: 0f,
AimWorldZ: 50f,
Fire: false,
InteractPickup: false,
Brake: false));
session.Step();
}
float yawAtRelease = player.Yaw;
float spinAngVelY = session.World.View.AngularVelocityY[lane];
float maxOvershootRad = 0f;
float angVelAt020s = 0f;
// Release for 0.5s (30 ticks)
for (int t = 0; t < 30; t++)
{
session.SetHumanInput(0, new PlayerInput(
Throttle: 0f,
Steer: 0f,
AimWorldX: 0f,
AimWorldZ: 50f,
Fire: false,
InteractPickup: false,
Brake: false));
session.Step();
float overshoot = MathF.Abs(MatchRules.NormalizeAngle(player.Yaw - yawAtRelease));
if (overshoot > maxOvershootRad)
{
maxOvershootRad = overshoot;
}
if (t == 11)
{
angVelAt020s = session.World.View.AngularVelocityY[lane];
}
}
_output.WriteLine(
$"{spec.Id,-10} spinOmegaY={spinAngVelY:F2}rad/s overshoot={maxOvershootRad:F4}rad ({maxOvershootRad * 180f / MathF.PI:F2}deg) " +
$"omegaY@0.20s={angVelAt020s:F4}rad/s");
Assert.True(MathF.Abs(spinAngVelY) > 1.0f,
$"{spec.Id}: failed to spin up before release (omegaY={spinAngVelY:F2})");
Assert.True(maxOvershootRad <= 0.08f,
$"{spec.Id}: yaw coast overshoot {maxOvershootRad:F4} rad ({maxOvershootRad * 180f / MathF.PI:F2} deg) > 0.08 rad");
Assert.True(MathF.Abs(angVelAt020s) <= 0.05f,
$"{spec.Id}: residual yaw angVel@0.20s={angVelAt020s:F4} rad/s > 0.05 rad/s");
}
}
}
}

View file

@ -373,11 +373,15 @@ namespace RustToDust.Tests
Assert.False(float.IsNaN(up.Y), $"NaN orientation at tick {tick}");
float gh = session.State.Terrain.SampleHeight(player.X, player.Z);
float bottomRel = (player.Y - player.Grid.Spec.HalfHeight) - gh;
float hullHalfH = player.Grid.Spec.IsWalker
? ArenaSession.GetPhysicsHalfHeight(player.Grid.Spec)
: player.Grid.Spec.HalfHeight;
float bottomRel = (player.Y - hullHalfH) - gh;
float airGapRel = (player.Y - player.Grid.Spec.HalfHeight) - gh;
maxGroundY = MathF.Max(maxGroundY, gh);
minBottomRel = MathF.Min(minBottomRel, bottomRel);
maxAirGap = MathF.Max(maxAirGap, bottomRel);
maxAirGap = MathF.Max(maxAirGap, airGapRel);
minUpDot = MathF.Min(minUpDot, up.Y);
}