diff --git a/src/RustToDust.Ymir/WalkerLocomotion.cs b/src/RustToDust.Ymir/WalkerLocomotion.cs index 45eb57b..7ae4a89 100644 --- a/src/RustToDust.Ymir/WalkerLocomotion.cs +++ b/src/RustToDust.Ymir/WalkerLocomotion.cs @@ -187,10 +187,16 @@ namespace RustToDust.Ymir if (Grounded && centerHit.Normal.Y < 0.96f) { + float minSoleY = float.PositiveInfinity; float meanSoleY = 0f; - for (int i = 0; i < n; i++) meanSoleY += FootSole[i].Y; + for (int i = 0; i < n; i++) + { + meanSoleY += FootSole[i].Y; + if (FootSole[i].Y < minSoleY) minSoleY = FootSole[i].Y; + } meanSoleY /= n; - PelvisOffset = new Vector3(0f, Math.Clamp(meanSoleY - centerHit.SurfaceY, -0.40f * GeomScale, 0.15f * GeomScale), 0f); + float slopeTarget = n == 2 ? (minSoleY - centerHit.SurfaceY) * 0.65f : (meanSoleY - centerHit.SurfaceY); + PelvisOffset = new Vector3(0f, Math.Clamp(slopeTarget, -0.45f * GeomScale, 0.15f * GeomScale), 0f); } if (n == 4 && Grounded) { @@ -1712,9 +1718,9 @@ namespace RustToDust.Ymir float limpShiftX = (legIntegrities[1] - legIntegrities[0]) * 0.14f * scale; float targetPelvisX = plantedSignX * lateralAmp * swingSin * speedWeight + limpShiftX; - float meanSoleY = 0.5f * (ws.FootSole[0].Y + ws.FootSole[1].Y); + float minSoleY = MathF.Min(ws.FootSole[0].Y, ws.FootSole[1].Y); var centerG = SampleGroundAt(terrain, world, comPos.X, comPos.Z, comPos.Y + 0.6f); - float slopeAdjY = Math.Clamp(meanSoleY - centerG.SurfaceY, -0.35f * scale, 0.08f * scale); + float slopeAdjY = Math.Clamp((minSoleY - centerG.SurfaceY) * 0.65f, -0.45f * scale, 0.05f * scale); float sprintCrouchFrac = Math.Clamp(horizSpeed / MathF.Max(5f, grid.Spec.MaxSpeed * 0.35f), 0f, 1f); float sprintCrouchCoeff = 0.055f + 0.072f * straightness; @@ -1881,10 +1887,6 @@ namespace RustToDust.Ymir // Keep all soles locked inside 0.995 * MaxLegReach of visual hip (post-pelvis & post-lean) for (int i = 0; i < n; i++) { - if (ws.Grounded && !ws.Planted[i]) - { - continue; - } Vector3 hipTotalW = comPos + pelvisWorldShift + Vector3.Transform(geom.Legs[i].HipInCom * scale, qTotal); float maxReach = geom.Legs[i].MaxLegReach * scale * 0.995f; Vector3 oldSole = ws.FootSole[i]; @@ -2054,47 +2056,55 @@ namespace RustToDust.Ymir } } - // If ground under hipWorld is reachable, binary-search along (soleXZ -> hipWorldXZ) so foot stays grounded on slopes - var gHip = SampleGroundAt(terrain, world, hipWorld.X, hipWorld.Z, referenceMaxY); - Vector3 hipGroundAnkle = new Vector3(hipWorld.X, gHip.SurfaceY, hipWorld.Z) + gHip.Normal * ankleH; - if ((hipGroundAnkle - hipWorld).Length() <= maxReach) + // Search from (fx, fz) toward hipWorld first to stay on ground with minimum displacement + float prevT = 0f; + for (int s = 1; s <= 6; s++) { - float lo = 0f; // at hipWorld (reachable) - float hi = 1f; // at (fx, fz) (unreachable) - Vector3 bestSole = new(hipWorld.X, gHip.SurfaceY, hipWorld.Z); - Vector3 bestNorm = gHip.Normal; - for (int b = 0; b < 4; b++) + float t = s / 6f; + float sx = fx + (hipWorld.X - fx) * t; + float sz = fz + (hipWorld.Z - fz) * t; + var sh = SampleGroundAt(terrain, world, sx, sz, referenceMaxY); + Vector3 sAnkle = new Vector3(sx, sh.SurfaceY, sz) + sh.Normal * ankleH; + if ((sAnkle - hipWorld).Length() <= maxReach) { - float mid = 0.5f * (lo + hi); - float mx = hipWorld.X + (fx - hipWorld.X) * mid; - float mz = hipWorld.Z + (fz - hipWorld.Z) * mid; - var mh = SampleGroundAt(terrain, world, mx, mz, referenceMaxY); - Vector3 mAnkle = new Vector3(mx, mh.SurfaceY, mz) + mh.Normal * ankleH; - if ((mAnkle - hipWorld).Length() <= maxReach) + float lo = prevT; + float hi = t; + Vector3 bestSole = new(sx, sh.SurfaceY, sz); + Vector3 bestNorm = sh.Normal; + for (int b = 0; b < 3; b++) { - lo = mid; - bestSole = new Vector3(mx, mh.SurfaceY, mz); - bestNorm = mh.Normal; - } - else - { - hi = mid; + float mid = 0.5f * (lo + hi); + float mx = fx + (hipWorld.X - fx) * mid; + float mz = fz + (hipWorld.Z - fz) * mid; + var mh = SampleGroundAt(terrain, world, mx, mz, referenceMaxY); + Vector3 mAnkle = new Vector3(mx, mh.SurfaceY, mz) + mh.Normal * ankleH; + if ((mAnkle - hipWorld).Length() <= maxReach) + { + hi = mid; + bestSole = new Vector3(mx, mh.SurfaceY, mz); + bestNorm = mh.Normal; + } + else + { + lo = mid; + } } + sole = bestSole; + normal = bestNorm; + return true; } - sole = bestSole; - normal = bestNorm; - return true; + prevT = t; } - // Ground under hipWorld is vertically out of reach (steep sidehill / cliff / airborne): - // pull progressively from (fx, fz) toward chassisCenterWorld where ground may be reachable. + // Ground along (fx, fz) -> hipWorld is vertically out of reach (steep sidehill / cliff / airborne): + // pull progressively from (fx, fz) toward/slightly past chassisCenterWorld where uphill ground may be reachable. Vector2 fromSoleToCenter = new(chassisCenterWorld.X - fx, chassisCenterWorld.Z - fz); float spanLen = fromSoleToCenter.Length(); if (spanLen > 1e-3f) { - for (int s = 1; s <= 4; s++) + for (int s = 1; s <= 5; s++) { - float t = s / 4f; + float t = s * 0.25f; float sx = fx + fromSoleToCenter.X * t; float sz = fz + fromSoleToCenter.Y * t; var sh = SampleGroundAt(terrain, world, sx, sz, referenceMaxY);