feat(core): Phase B.3 — CellPortal-based indoor/outdoor transitions in PhysicsEngine

Replace the disabled if(false) outdoor→indoor branch with real portal-plane
crossing logic. LandblockPhysics now carries IReadOnlyList<PortalPlane> Portals
(populated at load time; GameWindow passes Array.Empty for now until Task 3).

Resolve logic:
- Outdoor player: tests all portals where TargetCellId==0xFFFF (outside-facing);
  crossing enters the portal's OwnerCellId.
- Indoor player: tests portals where OwnerCellId==currentCell; crossing to
  TargetCellId==0xFFFF exits to terrain, otherwise transitions room-to-room.
- Landblock boundary crossing: unchanged — candidatePos landblock lookup already
  picks the adjacent block's terrain naturally.

Tests: renamed disabled test → Resolve_OutdoorThroughPortal_TransitionsToIndoor;
added Resolve_IndoorThroughExitPortal_TransitionsToOutdoor and
Resolve_LandblockBoundary_PicksAdjacentTerrain. 274 tests green.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Erik 2026-04-12 18:22:55 +02:00
parent cb46d892d5
commit 8252523b8b
5 changed files with 318 additions and 62 deletions

View file

@ -26,7 +26,7 @@ public class PhysicsEngineTests
{
var engine = new PhysicsEngine();
var terrain = new TerrainSurface(FlatHeightmap((byte)terrainZ), LinearHeightTable());
engine.AddLandblock(0xA9B4FFFFu, terrain, Array.Empty<CellSurface>(),
engine.AddLandblock(0xA9B4FFFFu, terrain, Array.Empty<CellSurface>(), Array.Empty<PortalPlane>(),
worldOffsetX: 0f, worldOffsetY: 0f);
return engine;
}
@ -55,7 +55,7 @@ public class PhysicsEngineTests
var engine = new PhysicsEngine();
var terrain = new TerrainSurface(heights, LinearHeightTable());
engine.AddLandblock(0xA9B4FFFFu, terrain, Array.Empty<CellSurface>(),
engine.AddLandblock(0xA9B4FFFFu, terrain, Array.Empty<CellSurface>(), Array.Empty<PortalPlane>(),
worldOffsetX: 0f, worldOffsetY: 0f);
var result = engine.Resolve(
@ -77,7 +77,7 @@ public class PhysicsEngineTests
var engine = new PhysicsEngine();
var terrain = new TerrainSurface(heights, LinearHeightTable());
engine.AddLandblock(0xA9B4FFFFu, terrain, Array.Empty<CellSurface>(),
engine.AddLandblock(0xA9B4FFFFu, terrain, Array.Empty<CellSurface>(), Array.Empty<PortalPlane>(),
worldOffsetX: 0f, worldOffsetY: 0f);
// Try to walk from the low side to the high side.
@ -91,40 +91,105 @@ public class PhysicsEngineTests
}
[Fact]
public void Resolve_EnterIndoorCell_StaysOutdoor_BecauseTransitionDisabled()
public void Resolve_OutdoorThroughPortal_TransitionsToIndoor()
{
var engine = new PhysicsEngine();
var terrain = new TerrainSurface(FlatHeightmap(50), LinearHeightTable());
// A CellSurface for the indoor cell with floor at Z=50.
var cellVerts = new Dictionary<ushort, Vector3>
{
[0] = new(40f, 40f, 50f),
[1] = new(60f, 40f, 50f),
[2] = new(60f, 60f, 50f),
[3] = new(40f, 60f, 50f),
};
var cellPolys = new List<List<short>> { new() { 0, 1, 2, 3 } };
var cell = new CellSurface(0x0100, cellVerts, cellPolys);
// A portal plane at X=45 (vertical plane facing +X).
// OwnerCellId = 0x0100 (the indoor cell), TargetCellId = 0xFFFF (faces outdoor).
// From outside, walking through this portal enters OwnerCellId.
var portal = PortalPlane.FromVertices(
new Vector3(45f, 40f, 45f),
new Vector3(45f, 60f, 45f),
new Vector3(45f, 60f, 55f),
targetCellId: 0xFFFF, ownerCellId: 0x0100, flags: 0);
engine.AddLandblock(0xA9B4FFFFu, terrain, new[] { cell }, new[] { portal },
worldOffsetX: 0f, worldOffsetY: 0f);
// Walk from X=40 (outdoor) through X=45 (portal) to X=50 (indoor).
var result = engine.Resolve(
new Vector3(40f, 50f, 50f), cellId: 0x0001, delta: new Vector3(10f, 0f, 0f),
stepUpHeight: 5f);
// Should have transitioned to indoor cell 0x0100.
Assert.Equal(0x0100u, result.CellId & 0xFFFFu);
Assert.True(result.IsOnGround);
}
[Fact]
public void Resolve_IndoorThroughExitPortal_TransitionsToOutdoor()
{
// Phase B.2 MVP: outdoor→indoor transitions are disabled because
// CellSurface floor polygons are too aggressive (building
// footprints/roofs capture the player). Walking over a cell's XY
// area stays on the outdoor terrain Z. Indoor transitions will be
// re-enabled when portal-based detection lands in Phase E.
var engine = new PhysicsEngine();
var terrain = new TerrainSurface(FlatHeightmap(50), LinearHeightTable());
var cellVerts = new Dictionary<ushort, Vector3>
{
[0] = new(40f, 40f, 55f),
[1] = new(60f, 40f, 55f),
[2] = new(60f, 60f, 55f),
[3] = new(40f, 60f, 55f),
[0] = new(40f, 40f, 50f),
[1] = new(60f, 40f, 50f),
[2] = new(60f, 60f, 50f),
[3] = new(40f, 60f, 50f),
};
var cellPolys = new List<List<short>> { new() { 0, 1, 2, 3 } };
var cell = new CellSurface(0x0100, cellVerts, cellPolys);
engine.AddLandblock(0xA9B4FFFFu, terrain, new[] { cell },
// Same portal geometry — OwnerCellId = 0x0100, TargetCellId = 0xFFFF (outdoor exit).
var portal = PortalPlane.FromVertices(
new Vector3(45f, 40f, 45f),
new Vector3(45f, 60f, 45f),
new Vector3(45f, 60f, 55f),
targetCellId: 0xFFFF, ownerCellId: 0x0100, flags: 0);
engine.AddLandblock(0xA9B4FFFFu, terrain, new[] { cell }, new[] { portal },
worldOffsetX: 0f, worldOffsetY: 0f);
// Walk from outdoor (30, 50) into the cell's floor area (50, 50).
// Walk from X=50 (indoor) through X=45 (portal) to X=40 (outdoor).
var result = engine.Resolve(
new Vector3(30f, 50f, 50f), cellId: 0x0001, delta: new Vector3(20f, 0f, 0f),
stepUpHeight: 10f);
new Vector3(50f, 50f, 50f), cellId: 0x0100, delta: new Vector3(-10f, 0f, 0f),
stepUpHeight: 5f);
// Should stay outdoor (transition disabled) at terrain Z = 50.
Assert.True(result.CellId < 0x0100u);
Assert.Equal(50f, result.Position.Z, precision: 1);
// Should have transitioned to outdoor.
Assert.True((result.CellId & 0xFFFFu) < 0x0100u);
Assert.True(result.IsOnGround);
}
[Fact]
public void Resolve_LandblockBoundary_PicksAdjacentTerrain()
{
var engine = new PhysicsEngine();
// Landblock A: flat at Z=50, offset at X=0.
var terrainA = new TerrainSurface(FlatHeightmap(50), LinearHeightTable());
engine.AddLandblock(0xA9B4FFFFu, terrainA, Array.Empty<CellSurface>(),
Array.Empty<PortalPlane>(), worldOffsetX: 0f, worldOffsetY: 0f);
// Landblock B: flat at Z=60, offset at X=192 (adjacent east).
var terrainB = new TerrainSurface(FlatHeightmap(60), LinearHeightTable());
engine.AddLandblock(0xAAB4FFFFu, terrainB, Array.Empty<CellSurface>(),
Array.Empty<PortalPlane>(), worldOffsetX: 192f, worldOffsetY: 0f);
// Walk from X=190 (landblock A) across to X=194 (landblock B).
var result = engine.Resolve(
new Vector3(190f, 96f, 50f), cellId: 0x0001, delta: new Vector3(4f, 0f, 0f),
stepUpHeight: 15f);
// Should be at Z=60 (landblock B's terrain) and position X≈194.
Assert.Equal(60f, result.Position.Z, precision: 1);
Assert.True(result.Position.X > 192f);
}
[Fact]
public void Resolve_LeaveIndoorCell_TransitionsToOutdoor()
{
@ -141,7 +206,7 @@ public class PhysicsEngineTests
var cellPolys = new List<List<short>> { new() { 0, 1, 2, 3 } };
var cell = new CellSurface(0x0100, cellVerts, cellPolys);
engine.AddLandblock(0xA9B4FFFFu, terrain, new[] { cell },
engine.AddLandblock(0xA9B4FFFFu, terrain, new[] { cell }, Array.Empty<PortalPlane>(),
worldOffsetX: 0f, worldOffsetY: 0f);
// Start inside the cell, walk out.