fix(core): AC2D render split formula + triangle-aware Z sampling

Two fundamental terrain fixes based on the AC2D + holtburger deep dive:

1. Terrain split formula: replaced WorldBuilder's physics-path formula
   (214614067/1813693831) with AC2D's render-path formula (0x0CCAC033,
   0x421BE3BD, 0x6C1AC587, 0x519B8F25). The two produce different splits
   for some cells. Since the render mesh uses this formula, the physics
   Z sampler must match it to avoid misalignment on slopes.

2. Triangle-aware Z: replaced bilinear interpolation in TerrainSurface
   with per-triangle barycentric interpolation. Each cell is split into
   two triangles (using the same AC2D formula). SampleZ determines which
   triangle the query point falls in, then interpolates within that
   triangle. This produces Z values that exactly match the visual terrain
   mesh — no more slope clipping.

Removes the multi-point Z sampling hack from PlayerMovementController
(no longer needed with exact triangle Z).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-04-12 22:08:42 +02:00
parent 5cd776914a
commit c5de445e5c
4 changed files with 116 additions and 72 deletions

View file

@ -94,7 +94,6 @@ public sealed class PlayerMovementController
// Heartbeat timer.
private float _heartbeatAccum;
private float _prevGroundZ;
public const float HeartbeatInterval = 0.2f; // 200ms
public bool HeartbeatDue { get; private set; }
@ -107,7 +106,6 @@ public sealed class PlayerMovementController
{
Position = pos;
CellId = cellId;
_prevGroundZ = pos.Z;
}
public MovementResult Update(float dt, MovementInput input)
@ -206,31 +204,10 @@ public sealed class PlayerMovementController
}
}
// Multi-point Z sampling: on slopes the visual terrain mesh can be
// higher or lower than the center-point physics sample. Sample 4
// points around the character (forward, back, left, right at ~0.7
// units — roughly foot distance) and use the MAX Z. This prevents
// feet clipping on both uphill and downhill slopes.
if (!IsAirborne)
{
const float sampleDist = 0.7f;
ReadOnlySpan<(float ox, float oy)> offsets = stackalloc (float, float)[]
{
( forwardX * sampleDist, forwardY * sampleDist), // forward
(-forwardX * sampleDist, -forwardY * sampleDist), // back
( forwardY * sampleDist, -forwardX * sampleDist), // right
(-forwardY * sampleDist, forwardX * sampleDist), // left
};
foreach (var (ox, oy) in offsets)
{
var sampleResult = _physics.Resolve(
new Vector3(result.Position.X + ox, result.Position.Y + oy, newZ),
CellId, Vector3.Zero, StepUpHeight);
if (sampleResult.IsOnGround && sampleResult.Position.Z > newZ)
newZ = sampleResult.Position.Z;
}
}
_prevGroundZ = newZ;
// Triangle-aware Z sampling in TerrainSurface now matches the visual
// terrain mesh exactly (using AC2D's FSplitNESW render formula +
// barycentric interpolation within each triangle). No multi-point
// sampling hacks needed.
Position = new Vector3(result.Position.X, result.Position.Y, newZ);
CellId = result.CellId;