Revert "fix(physics): remove per-frame indoor walkable-plane synthesis"

This reverts commit 9f874f4650.
This commit is contained in:
Erik 2026-05-20 09:17:24 +02:00
parent 9f874f4650
commit 0a7ce8fd58
4 changed files with 573 additions and 23 deletions

View file

@ -1152,14 +1152,11 @@ public static class BSPQuery
/// </para>
///
/// <para>
/// Out-of-band "find a walkable plane indoors" entry point for callers
/// that genuinely need to query a cell's walkable floor (spawn-placement
/// validation, teleport-target verification, future debug overlays).
/// NOT called from the per-frame physics resolver — the original
/// per-frame caller (TryFindIndoorWalkablePlane) was deleted 2026-05-20
/// because retail's BSPTREE::find_collisions does NOT re-synthesize the
/// ContactPlane on the OK path. The wrapper is kept here as the
/// underlying retail-faithful walkable-finder API.
/// Intended call site: indoor walkable-plane synthesis in
/// <c>Transition.TryFindIndoorWalkablePlane</c> when the indoor cell-BSP
/// collision returns OK (no wall hit) and the resolver still needs a
/// ContactPlane to feed ValidateWalkable. Outdoor terrain has its own path
/// (<see cref="PhysicsEngine.SampleTerrainWalkable"/>) and does not use this.
/// </para>
///
/// <para>

View file

@ -1266,6 +1266,120 @@ public sealed class Transition
// Environment collision — outdoor terrain
// -----------------------------------------------------------------------
/// <summary>
/// Synthesize the indoor walkable contact plane for the player's current
/// position when the cell BSP returns OK (no wall collision).
///
/// <para>
/// Routes through the retail-faithful BSP walkable-finder
/// (<see cref="BSPQuery.FindWalkableSphere"/>) — which traverses the cell
/// PhysicsBSP and picks the polygon closest to the foot along the up vector.
/// Phase 2 commit eb0f772 introduced a linear first-match XY scan as a
/// stop-gap; that scan picked the wrong floor whenever two polygons
/// overlapped in XY at different Z (cellars, 2nd floors, balconies).
/// </para>
///
/// <para>
/// Returns <c>false</c> if no walkable floor poly is found under the
/// player. The caller falls through to outdoor terrain in that case
/// (defensive backstop — should not normally happen inside a sealed cell).
/// </para>
///
/// <para>
/// Retail oracle: BSPLEAF::find_walkable (acclient_2013_pseudo_c.txt:326793),
/// BSPNODE::find_walkable (:326211), CPolygon::walkable_hits_sphere (:323006),
/// CPolygon::adjust_sphere_to_plane (:322032).
/// </para>
/// </summary>
internal bool TryFindIndoorWalkablePlane(
CellPhysics cellPhysics,
Vector3 localFootCenter,
float sphereRadius,
out System.Numerics.Plane worldPlane,
out Vector3[] worldVertices,
out uint hitPolyId)
{
worldPlane = default;
worldVertices = System.Array.Empty<Vector3>();
hitPolyId = 0;
if (cellPhysics.BSP?.Root is null) return false;
// Build foot sphere in cell-local space. Caller passes localFootCenter
// already transformed into cell-local space and the resolver's
// foot-sphere radius.
var localSphere = new DatReaderWriter.Types.Sphere
{
Origin = localFootCenter,
Radius = sphereRadius,
};
// Save/restore WalkableAllowance: CPolygon::walkable_hits_sphere reads
// path.WalkableAllowance (acclient_2013_pseudo_c.txt:323010). For
// "standing here, find my floor" we want the walkability slope
// threshold FloorZ. The outer resolver may have set it to LandingZ
// (airborne→ground transition) or another value; we must not leak our
// change back to the resolver. try/finally so an exception inside
// FindWalkableSphere doesn't leak the modified state.
float savedWalkableAllowance = this.SpherePath.WalkableAllowance;
this.SpherePath.WalkableAllowance = PhysicsGlobals.FloorZ;
ResolvedPolygon? hitPoly = null;
ushort hitId = 0;
Vector3 adjustedCenter;
bool found;
try
{
found = BSPQuery.FindWalkableSphere(
cellPhysics.BSP.Root,
cellPhysics.Resolved,
this,
localSphere,
INDOOR_WALKABLE_PROBE_DISTANCE,
Vector3.UnitZ, // local Z is up for indoor cells (identity transform)
out hitPoly,
out hitId,
out adjustedCenter);
}
finally
{
this.SpherePath.WalkableAllowance = savedWalkableAllowance;
}
// adjustedCenter (sphere slid onto polygon plane) is intentionally
// discarded — ValidateWalkable recomputes contact geometry from the
// world-space plane + foot position, consistent with the outdoor terrain
// path (SampleTerrainWalkable returns only plane + vertices, no adjusted
// sphere). The local is held only to satisfy the out param.
if (!found || hitPoly is null) return false;
// Transform hit polygon's plane + vertices to world space. Math is
// unchanged from the previous TryFindIndoorWalkablePlane implementation.
var worldNormal = Vector3.TransformNormal(hitPoly.Plane.Normal, cellPhysics.WorldTransform);
worldNormal = Vector3.Normalize(worldNormal);
var worldV0 = Vector3.Transform(hitPoly.Vertices[0], cellPhysics.WorldTransform);
float worldD = -Vector3.Dot(worldNormal, worldV0);
worldPlane = new System.Numerics.Plane(worldNormal, worldD);
worldVertices = new Vector3[hitPoly.Vertices.Length];
for (int i = 0; i < hitPoly.Vertices.Length; i++)
worldVertices[i] = Vector3.Transform(hitPoly.Vertices[i], cellPhysics.WorldTransform);
hitPolyId = hitId;
return true;
}
/// <summary>
/// Downward probe distance used by <see cref="TryFindIndoorWalkablePlane"/>
/// when scanning for the indoor walkable contact plane. 50 cm.
/// Larger than the +0.02f cell-origin Z-bump and larger than any realistic
/// step riser; smaller than a full cell height so we don't reach through
/// a thin floor into the cell above/below.
/// </summary>
private const float INDOOR_WALKABLE_PROBE_DISTANCE = 0.5f;
/// <summary>
/// Query the outdoor terrain at CheckPos and apply ValidateWalkable logic.
/// Indoor BSP collision is deferred to Task 6c.
@ -1389,22 +1503,59 @@ public sealed class Transition
return cellState;
}
// Indoor BSP returned OK — no wall collision. ContactPlane
// is RETAINED from the prior tick's seed
// (PhysicsEngine.ResolveWithTransition:583, the
// init_contact_plane equivalent) OR refreshed by Path 3
// step-down / Path 4 land if those fired this tick. Either
// way, no synthesis is needed here — matches retail's
// BSPTREE::find_collisions OK path
// (acclient_2013_pseudo_c.txt:323938).
// ── Synthesize indoor walkable contact plane ──────────────
// Indoor walking Phase 2 follow-up (2026-05-19). When the BSP
// returns OK (no wall collision), the player is standing on a
// floor poly inside the cell. We must NOT fall through to
// outdoor terrain (SampleTerrainWalkable) — the outdoor terrain
// Z is below the indoor floor due to the +0.02f Z-bump applied
// for render z-fight prevention. ValidateWalkable would then see
// the player 0.5m above the outdoor plane → marks them as
// airborne → walkable=False → falling animation, never recovers.
//
// Do NOT fall through to outdoor terrain backstop: the
// player is in an indoor cell, and the outdoor terrain
// Z is below the indoor floor by ~0.02m (the render Z-bump),
// which would mark the player as airborne and trigger the
// falling-animation stuck symptom (the original Bug A).
// 2026-05-20 slice 2 of indoor ContactPlane retention.
return TransitionState.OK;
// Retail: CEnvCell::find_env_collisions returns from the cell
// branch with the cell's walkable plane set — no fall-through
// to terrain.
bool walkableHit = TryFindIndoorWalkablePlane(
cellPhysics, localCenter, sphereRadius,
out var indoorPlane,
out var indoorVertices,
out uint hitPolyId);
if (PhysicsDiagnostics.ProbeIndoorBspEnabled)
{
if (walkableHit)
{
// dz = signed gap between foot and synthesized plane.
// Plane: N·p + D = 0 ⇒ pZ_on_plane = -D/N.z (for upward-facing planes)
// gap = foot.Z - pZ_on_plane = foot.Z - (-D/N.z) = foot.Z + D/N.z
float dz = footCenter.Z + indoorPlane.D / indoorPlane.Normal.Z;
Console.WriteLine(System.FormattableString.Invariant(
$"[indoor-walkable] cell=0x{sp.CheckCellId:X8} wpos=({footCenter.X:F3},{footCenter.Y:F3},{footCenter.Z:F3}) probe={INDOOR_WALKABLE_PROBE_DISTANCE:F2} result=HIT poly=0x{hitPolyId:X4} wn=({indoorPlane.Normal.X:F3},{indoorPlane.Normal.Y:F3},{indoorPlane.Normal.Z:F3}) wD={indoorPlane.D:F3} dz={dz:+0.00;-0.00;+0.00}"));
}
else
{
Console.WriteLine(System.FormattableString.Invariant(
$"[indoor-walkable] cell=0x{sp.CheckCellId:X8} wpos=({footCenter.X:F3},{footCenter.Y:F3},{footCenter.Z:F3}) probe={INDOOR_WALKABLE_PROBE_DISTANCE:F2} result=MISS"));
}
}
if (walkableHit)
{
return ValidateWalkable(
footCenter,
sphereRadius,
indoorPlane,
isWater: false,
waterDepth: 0f,
cellId: sp.CheckCellId,
walkableVertices: indoorVertices);
}
// If no walkable floor was found under the player indoors
// (rare — cell with only walls/ceiling), fall through to
// outdoor terrain as a defensive backstop. Indoor walking
// will report walkable=False until the player moves over a
// cell with a proper floor poly.
}
}