fix(physics): Cluster A #84 + #85 — indoor cell tracking

ResolveOutdoorCellId only resolved outdoor terrain landcells. A player
geometrically inside an EnvCell stayed in outdoor-landcell range, so
FindEnvCollisions' indoor cell-BSP branch (gated on cellLow >= 0x0100)
never fired. Both #84 (blocked by air indoors) and #85 (pass through
walls outside→in) are downstream of this — without indoor cell-BSP
collision the player gets stuck against outdoor-stab back-faces of the
building shell, and walls only block from one side.

Adds an indoor-cell-containment check via PhysicsDataCache: at
CacheCellStruct time, compute each cell's local AABB from its resolved
polygon vertices; at ResolveOutdoorCellId time, transform the world
position into each cached cell's local space and return the matched
cell's full id when contained. Falls through to the existing outdoor
terrain logic when no EnvCell contains the position.

Also fixes a pre-existing prefix-preservation bug in the outdoor branch:
the function now always applies the matched landblock's high-16 prefix
even when the input fallbackCellId arrived bare-low-byte (the L.2e
finding from CLAUDE.md). Updated two existing PhysicsEngineTests that
encoded the old bare-low-byte output.

Evidence: launch-cluster-a-capture.log @ 2026-05-19 — player at
worldPos (155.376, 14.010, 94.000) geometrically inside cottage cell
0xA9B40172, but sp.CheckCellId stuck at 0x00000031 (outdoor landcell)
across 454 [resolve] lines; zero [indoor-bsp] lines because the gate
never opened.

Closes #84.
Closes #85.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Erik 2026-05-19 15:20:36 +02:00
parent 4e308d567a
commit c19d6fb321
4 changed files with 292 additions and 10 deletions

View file

@ -136,6 +136,27 @@ public sealed class PhysicsDataCache
Matrix4x4.Invert(worldTransform, out var inverseTransform); Matrix4x4.Invert(worldTransform, out var inverseTransform);
var resolved = ResolvePolygons(cellStruct.PhysicsPolygons, cellStruct.VertexArray);
// Indoor walking Phase D (2026-05-19): compute a tight local AABB from
// the resolved polygon vertices. Computed once at cache time so the
// per-frame TryFindContainingCell check only does AABB point tests.
var aabbMin = new Vector3(float.MaxValue);
var aabbMax = new Vector3(float.MinValue);
foreach (var (_, poly) in resolved)
{
if (poly.Vertices is null) continue;
foreach (var v in poly.Vertices)
{
if (v.X < aabbMin.X) aabbMin.X = v.X;
if (v.Y < aabbMin.Y) aabbMin.Y = v.Y;
if (v.Z < aabbMin.Z) aabbMin.Z = v.Z;
if (v.X > aabbMax.X) aabbMax.X = v.X;
if (v.Y > aabbMax.Y) aabbMax.Y = v.Y;
if (v.Z > aabbMax.Z) aabbMax.Z = v.Z;
}
}
_cellStruct[envCellId] = new CellPhysics _cellStruct[envCellId] = new CellPhysics
{ {
BSP = cellStruct.PhysicsBSP, BSP = cellStruct.PhysicsBSP,
@ -143,7 +164,9 @@ public sealed class PhysicsDataCache
Vertices = cellStruct.VertexArray, Vertices = cellStruct.VertexArray,
WorldTransform = worldTransform, WorldTransform = worldTransform,
InverseWorldTransform = inverseTransform, InverseWorldTransform = inverseTransform,
Resolved = ResolvePolygons(cellStruct.PhysicsPolygons, cellStruct.VertexArray), Resolved = resolved,
LocalAabbMin = aabbMin,
LocalAabbMax = aabbMax,
}; };
} }
@ -219,6 +242,53 @@ public sealed class PhysicsDataCache
/// </summary> /// </summary>
public IReadOnlyCollection<uint> CellStructIds => (IReadOnlyCollection<uint>)_cellStruct.Keys; public IReadOnlyCollection<uint> CellStructIds => (IReadOnlyCollection<uint>)_cellStruct.Keys;
/// <summary>
/// Indoor walking Phase D (2026-05-19). Returns the full id of the first
/// cached EnvCell whose local AABB contains <paramref name="worldPos"/>,
/// or false if no cached EnvCell contains it. Used by
/// <see cref="PhysicsEngine.ResolveOutdoorCellId"/> to promote the player's
/// CellId to an indoor EnvCell when the player is geometrically inside one.
///
/// <para>
/// AABBs are pre-computed in <see cref="CacheCellStruct"/> from each
/// cell's resolved polygon vertices, transformed into local space via
/// <see cref="CellPhysics.InverseWorldTransform"/>. Iteration is O(N) over
/// cached cells; N is bounded by the streaming radius (~80 cells at
/// radius 4).
/// </para>
///
/// <para>
/// Local AABB is a tight bound around the cell's geometry. EnvCells in
/// Holtburg are roughly room-sized cuboids; the local AABB is therefore
/// a reasonable proxy for "is the player in this cell." For cells with
/// concave shapes or non-room geometry, the AABB will over-approximate;
/// this only matters if two cells' AABBs overlap and the player is in
/// the overlap region (rare in practice; if it becomes an issue, switch
/// to a BSP point-in-cell test).
/// </para>
/// </summary>
public bool TryFindContainingCell(Vector3 worldPos, out uint envCellId)
{
foreach (var (id, cp) in _cellStruct)
{
// Guard: if the AABB was never populated (no vertices in the cell),
// LocalAabbMin stays at float.MaxValue — the containment test will
// always fail, so we skip the cell silently.
if (cp.LocalAabbMin.X == float.MaxValue) continue;
var local = Vector3.Transform(worldPos, cp.InverseWorldTransform);
if (local.X >= cp.LocalAabbMin.X && local.X <= cp.LocalAabbMax.X &&
local.Y >= cp.LocalAabbMin.Y && local.Y <= cp.LocalAabbMax.Y &&
local.Z >= cp.LocalAabbMin.Z && local.Z <= cp.LocalAabbMax.Z)
{
envCellId = id;
return true;
}
}
envCellId = 0;
return false;
}
/// <summary> /// <summary>
/// Register a pre-built <see cref="GfxObjPhysics"/> directly. /// Register a pre-built <see cref="GfxObjPhysics"/> directly.
/// Intended for unit-test fixtures that construct synthetic BSP trees /// Intended for unit-test fixtures that construct synthetic BSP trees
@ -226,6 +296,14 @@ public sealed class PhysicsDataCache
/// </summary> /// </summary>
public void RegisterGfxObjForTest(uint gfxObjId, GfxObjPhysics physics) public void RegisterGfxObjForTest(uint gfxObjId, GfxObjPhysics physics)
=> _gfxObj[gfxObjId] = physics; => _gfxObj[gfxObjId] = physics;
/// <summary>
/// Register a pre-built <see cref="CellPhysics"/> directly. Intended for
/// unit-test fixtures that construct synthetic cells without going through
/// dat-driven <see cref="CacheCellStruct"/>.
/// </summary>
public void RegisterCellStructForTest(uint envCellId, CellPhysics physics)
=> _cellStruct[envCellId] = physics;
} }
/// <summary> /// <summary>
@ -310,4 +388,22 @@ public sealed class CellPhysics
/// Pre-resolved polygon data with vertex positions and computed planes. /// Pre-resolved polygon data with vertex positions and computed planes.
/// </summary> /// </summary>
public required Dictionary<ushort, ResolvedPolygon> Resolved { get; init; } public required Dictionary<ushort, ResolvedPolygon> Resolved { get; init; }
/// <summary>
/// Indoor walking Phase D (2026-05-19). Local-space AABB minimum corner,
/// computed from the resolved polygon vertices at <see cref="PhysicsDataCache.CacheCellStruct"/>
/// time. Initialized to <c>float.MaxValue</c> so that
/// <see cref="PhysicsDataCache.TryFindContainingCell"/> silently skips
/// cells with no vertex data.
/// </summary>
public Vector3 LocalAabbMin { get; init; } = new Vector3(float.MaxValue);
/// <summary>
/// Indoor walking Phase D (2026-05-19). Local-space AABB maximum corner,
/// computed from the resolved polygon vertices at <see cref="PhysicsDataCache.CacheCellStruct"/>
/// time. Initialized to <c>float.MinValue</c> so that
/// <see cref="PhysicsDataCache.TryFindContainingCell"/> silently skips
/// cells with no vertex data.
/// </summary>
public Vector3 LocalAabbMax { get; init; } = new Vector3(float.MinValue);
} }

View file

@ -230,20 +230,48 @@ public sealed class PhysicsEngine
} }
/// <summary> /// <summary>
/// Resolve the outdoor cell id that owns a world-space position. /// Resolve a position's CellId. Tries indoor EnvCell containment first
/// Indoor ids are preserved because EnvCell ownership still comes from /// (via <see cref="PhysicsDataCache.TryFindContainingCell"/>); falls back
/// portal/cell BSP state; outdoor ids are derived from the registered /// to outdoor terrain landcell resolution.
/// landblock that currently contains the point. ///
/// <para>
/// Indoor walking Phase D (2026-05-19) extended this to fix #84 + #85:
/// previously the function only resolved outdoor cells, so a player
/// geometrically inside an EnvCell stayed in outdoor-landcell range and
/// the indoor cell-BSP collision branch never fired. The indoor
/// containment check promotes the player's CellId to the matched
/// EnvCell, which lets <see cref="Transition.FindEnvCollisions"/>'s
/// indoor branch (gated on cellLow &gt;= 0x0100) take effect.
/// </para>
///
/// <para>
/// Also fixes a pre-existing prefix-preservation bug: the outdoor branch
/// now always applies the matched landblock's high-16 prefix even when
/// the input <paramref name="fallbackCellId"/> arrived bare-low-byte
/// (the L.2e finding from CLAUDE.md).
/// </para>
/// </summary> /// </summary>
internal uint ResolveOutdoorCellId(Vector3 worldPos, uint fallbackCellId) internal uint ResolveOutdoorCellId(Vector3 worldPos, uint fallbackCellId)
{ {
if (fallbackCellId == 0) if (fallbackCellId == 0)
return 0; return 0;
// Phase D: indoor-cell-containment check. If the player's worldPos
// is geometrically inside a cached EnvCell, return that cell's full
// id — overrides any prior outdoor CellId the caller passed in.
if (DataCache is not null && DataCache.TryFindContainingCell(worldPos, out var indoorId))
return indoorId;
// Pre-existing: if the caller already passes an indoor CellId AND
// the player isn't in any cached EnvCell, trust the caller. This
// preserves behaviour for indoor cells whose physics hasn't been
// cached yet (rare; should be impossible in steady state).
uint fallbackLow = fallbackCellId & 0xFFFFu; uint fallbackLow = fallbackCellId & 0xFFFFu;
if (fallbackLow >= 0x0100u) if (fallbackLow >= 0x0100u)
return fallbackCellId; return fallbackCellId;
// Outdoor terrain resolution. Always applies the matched landblock's
// prefix — fixes the bare-low-byte preservation bug (L.2e).
foreach (var kvp in _landblocks) foreach (var kvp in _landblocks)
{ {
var lb = kvp.Value; var lb = kvp.Value;
@ -252,9 +280,7 @@ public sealed class PhysicsEngine
if (localX >= 0f && localX < 192f && localY >= 0f && localY < 192f) if (localX >= 0f && localX < 192f && localY >= 0f && localY < 192f)
{ {
uint lowCellId = lb.Terrain.ComputeOutdoorCellId(localX, localY); uint lowCellId = lb.Terrain.ComputeOutdoorCellId(localX, localY);
return (fallbackCellId & 0xFFFF0000u) == 0 return (kvp.Key & 0xFFFF0000u) | lowCellId;
? lowCellId
: (kvp.Key & 0xFFFF0000u) | lowCellId;
} }
} }

View file

@ -207,7 +207,10 @@ public class PhysicsEngineTests
Assert.True(result.IsOnGround); Assert.True(result.IsOnGround);
Assert.InRange(result.Position.X, 24.9f, 25.1f); Assert.InRange(result.Position.X, 24.9f, 25.1f);
Assert.Equal(0x0009u, result.CellId); // Phase D fix: ResolveOutdoorCellId now always applies the matched
// landblock's high-16 prefix — 0xA9B4 prefix from the registered
// landblock (0xA9B4FFFF) is now included in the returned CellId.
Assert.Equal(0xA9B40009u, result.CellId);
} }
[Fact] [Fact]
@ -228,7 +231,10 @@ public class PhysicsEngineTests
Assert.True(result.IsOnGround); Assert.True(result.IsOnGround);
Assert.InRange(result.Position.X, 97.9f, 98.1f); Assert.InRange(result.Position.X, 97.9f, 98.1f);
Assert.Equal(0x0025u, result.CellId); // Phase D fix: ResolveOutdoorCellId now always applies the matched
// landblock's high-16 prefix — 0xA9B4 prefix from the registered
// landblock (0xA9B4FFFF) is now included in the returned CellId.
Assert.Equal(0xA9B40025u, result.CellId);
} }
[Fact] [Fact]

View file

@ -0,0 +1,154 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using AcDream.Core.Physics;
using Xunit;
namespace AcDream.Core.Tests.Physics;
/// <summary>
/// Indoor walking Phase D (2026-05-19): tests for the indoor-cell-containment
/// check added to <see cref="PhysicsEngine.ResolveOutdoorCellId"/>.
/// Covers the four scenarios described in the Phase D implementation plan.
/// </summary>
public class ResolveOutdoorCellIdIndoorContainmentTests
{
/// <summary>
/// Build a <see cref="CellPhysics"/> whose local AABB spans ±<paramref name="halfExtent"/>
/// around the origin, placed at <paramref name="worldOrigin"/> via the
/// WorldTransform / InverseWorldTransform pair.
/// </summary>
private static CellPhysics MakeIndoorCellAt(Vector3 worldOrigin, Vector3 halfExtent)
{
// Four vertices defining a floor quad — enough for AABB computation at
// cache time (in production this is done by CacheCellStruct, in tests
// we pre-supply LocalAabbMin / LocalAabbMax directly).
var min = -halfExtent;
var max = halfExtent;
var verts = new[]
{
new Vector3(min.X, min.Y, min.Z),
new Vector3(max.X, min.Y, min.Z),
new Vector3(max.X, max.Y, max.Z),
new Vector3(min.X, max.Y, max.Z),
};
var poly = new ResolvedPolygon
{
Vertices = verts,
Plane = new Plane(Vector3.UnitZ, 0f),
NumPoints = 4,
SidesType = DatReaderWriter.Enums.CullMode.None,
};
var world = Matrix4x4.CreateTranslation(worldOrigin);
Matrix4x4.Invert(world, out var inv);
return new CellPhysics
{
Resolved = new Dictionary<ushort, ResolvedPolygon> { [0] = poly },
WorldTransform = world,
InverseWorldTransform = inv,
LocalAabbMin = min,
LocalAabbMax = max,
};
}
// -----------------------------------------------------------------------
// Test 1: player inside a cached EnvCell → returns that cell's full id.
// -----------------------------------------------------------------------
[Fact]
public void ResolveOutdoorCellId_PlayerInsideCachedEnvCell_ReturnsEnvCellId()
{
var engine = new PhysicsEngine();
engine.DataCache = new PhysicsDataCache();
// Cache an EnvCell at world origin spanning ±5 m on each axis.
var cell = MakeIndoorCellAt(Vector3.Zero, new Vector3(5f, 5f, 5f));
engine.DataCache.RegisterCellStructForTest(0xA9B40172u, cell);
// Player at world origin → inside the EnvCell's AABB.
uint result = engine.ResolveOutdoorCellId(Vector3.Zero, fallbackCellId: 0x00000031u);
Assert.Equal(0xA9B40172u, result);
}
// -----------------------------------------------------------------------
// Test 2: player outside all cached EnvCells → falls through to outdoor
// (and since no landblocks are registered, returns the fallback unchanged).
// -----------------------------------------------------------------------
[Fact]
public void ResolveOutdoorCellId_PlayerOutsideAllCachedEnvCells_FallsThroughToOutdoor()
{
var engine = new PhysicsEngine();
engine.DataCache = new PhysicsDataCache();
var cell = MakeIndoorCellAt(Vector3.Zero, new Vector3(5f, 5f, 5f));
engine.DataCache.RegisterCellStructForTest(0xA9B40172u, cell);
// Player at (100, 100, 0) — far outside the cached EnvCell.
// No landblocks registered → outdoor branch can't match either.
uint result = engine.ResolveOutdoorCellId(new Vector3(100f, 100f, 0f), fallbackCellId: 0x00000031u);
Assert.Equal(0x00000031u, result);
}
// -----------------------------------------------------------------------
// Test 3: EnvCell with a non-identity WorldTransform (rotation around Z).
// Player at world (3, 0, 0) is still inside the rotated local AABB.
// -----------------------------------------------------------------------
[Fact]
public void ResolveOutdoorCellId_PlayerInsideEnvCellWithRotatedTransform_StillDetectsContainment()
{
var halfExtent = new Vector3(5f, 5f, 5f);
var verts = new[]
{
new Vector3(-5f, -5f, -5f),
new Vector3( 5f, -5f, -5f),
new Vector3( 5f, 5f, 5f),
new Vector3(-5f, 5f, 5f),
};
var poly = new ResolvedPolygon
{
Vertices = verts,
Plane = new Plane(Vector3.UnitZ, 0f),
NumPoints = 4,
SidesType = DatReaderWriter.Enums.CullMode.None,
};
// 90° rotation around Z. A point at world (3, 0, 0) transforms to
// local (0, -3, 0) — still within ±5 on every axis.
var rotation = Matrix4x4.CreateRotationZ(MathF.PI / 2f);
Matrix4x4.Invert(rotation, out var inv);
var cell = new CellPhysics
{
Resolved = new Dictionary<ushort, ResolvedPolygon> { [0] = poly },
WorldTransform = rotation,
InverseWorldTransform = inv,
LocalAabbMin = -halfExtent,
LocalAabbMax = halfExtent,
};
var engine = new PhysicsEngine();
engine.DataCache = new PhysicsDataCache();
engine.DataCache.RegisterCellStructForTest(0xA9B40172u, cell);
uint result = engine.ResolveOutdoorCellId(new Vector3(3f, 0f, 0f), fallbackCellId: 0x00000031u);
Assert.Equal(0xA9B40172u, result);
}
// -----------------------------------------------------------------------
// Test 4: fallbackCellId == 0 → always returns 0 (existing early-return).
// -----------------------------------------------------------------------
[Fact]
public void ResolveOutdoorCellId_FallbackZero_ReturnsZero()
{
var engine = new PhysicsEngine();
engine.DataCache = new PhysicsDataCache();
// Even if the player is inside a cell, fallback=0 should still return 0.
var cell = MakeIndoorCellAt(Vector3.Zero, new Vector3(5f, 5f, 5f));
engine.DataCache.RegisterCellStructForTest(0xA9B40172u, cell);
uint result = engine.ResolveOutdoorCellId(Vector3.Zero, fallbackCellId: 0u);
Assert.Equal(0u, result);
}
}