feat(physics): Phase 2 — port CellTransit + wire into ResolveCellId
New CellTransit static class ports retail's portal-graph cell traversal:
- FindTransitCellsSphere — indoor portal-neighbour walk
- AddAllOutsideCells — outdoor 24m grid expansion
- FindCellList — top-level driver (BFS through portals;
PointInsideCellBsp for final containment)
PhysicsEngine.ResolveOutdoorCellId renamed to ResolveCellId. Body
rewritten: indoor seeds delegate to CellTransit.FindCellList (portal-
graph BFS + BSP containment test); outdoor seeds keep the landblock
terrain grid lookup from the original implementation (preserving the
L.2e prefix-preservation fix). Signature extended with sphereRadius
parameter (needed by the sphere-vs-portal-plane test). Three call
sites updated (PhysicsEngine x2, TransitionTypes x1).
BSPQuery.PointInsideCellBsp retyped from PhysicsBSPNode? to CellBSPNode?
— the function operates on the cell-BSP tree (CellPhysics.CellBSP.Root
is a CellBSPNode). The previous PhysicsBSPNode typing was dead code, so
retype is safe.
Deletes the Phase D ResolveOutdoorCellIdTests.cs file. New ResolveCellIdTests
covers the equivalent contracts (fallback zero, outdoor seed with no
landblock).
Outdoor->indoor entry (check_building_transit) is stubbed pending the
BuildingPhysics infrastructure landing in the next commit.
Spec: docs/superpowers/specs/2026-05-19-indoor-portal-cell-tracking-design.md
Plan: docs/superpowers/plans/2026-05-19-indoor-portal-cell-tracking.md
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
1969c55823
commit
aad697602e
8 changed files with 472 additions and 182 deletions
|
|
@ -0,0 +1,47 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using AcDream.Core.Physics;
|
||||
using Xunit;
|
||||
|
||||
namespace AcDream.Core.Tests.Physics;
|
||||
|
||||
public class CellTransitAddAllOutsideCellsTests
|
||||
{
|
||||
[Fact]
|
||||
public void SphereWellInsideCell_AddsOneCell()
|
||||
{
|
||||
// Player at world (12, 12, 0) in landblock 0xA9B40000 → cell (0,0).
|
||||
// Landblock origin: 0xA9 = 169 → world X = 169*192 = 32448.
|
||||
// 0xB4 = 180 → world Y = 180*192 = 34560.
|
||||
// Player needs to be in cell (0,0) RELATIVE to landblock origin:
|
||||
// world X = 32448 + 12 = 32460
|
||||
// world Y = 34560 + 12 = 34572
|
||||
var candidates = new HashSet<uint>();
|
||||
CellTransit.AddAllOutsideCells(
|
||||
worldSphereCenter: new Vector3(32460f, 34572f, 0f),
|
||||
sphereRadius: 0.5f,
|
||||
currentCellId: 0xA9B40001u,
|
||||
candidates);
|
||||
|
||||
Assert.Single(candidates);
|
||||
Assert.Contains(0xA9B40001u, candidates);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SphereAtCellEastBoundary_AddsTwoCells()
|
||||
{
|
||||
// Player at world (32448 + 23.6, 34560 + 12, 0) — near +X edge of cell (0,0).
|
||||
// Sphere reach to localX = 23.6 + 0.5 = 24.1 → cell (1,0) added.
|
||||
var candidates = new HashSet<uint>();
|
||||
CellTransit.AddAllOutsideCells(
|
||||
worldSphereCenter: new Vector3(32448f + 23.6f, 34560f + 12f, 0f),
|
||||
sphereRadius: 0.5f,
|
||||
currentCellId: 0xA9B40001u,
|
||||
candidates);
|
||||
|
||||
Assert.Equal(2, candidates.Count);
|
||||
Assert.Contains(0xA9B40001u, candidates);
|
||||
// Cell (1,0): low-16 id = 1 * 8 + 0 + 1 = 9 → 0x0009.
|
||||
Assert.Contains(0xA9B40009u, candidates);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,108 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Numerics;
|
||||
using AcDream.Core.Physics;
|
||||
using Xunit;
|
||||
|
||||
namespace AcDream.Core.Tests.Physics;
|
||||
|
||||
public class CellTransitFindTransitCellsSphereTests
|
||||
{
|
||||
private static CellPhysics MakeCellWithPortalAtRightWall(
|
||||
Matrix4x4 worldTransform, uint otherCellId, ushort flags)
|
||||
{
|
||||
// Portal poly at local x=2.5 (right wall), normal +X.
|
||||
var portalPolyA = new ResolvedPolygon
|
||||
{
|
||||
Vertices = new[]
|
||||
{
|
||||
new Vector3(2.5f, -2.5f, 0f),
|
||||
new Vector3(2.5f, 2.5f, 0f),
|
||||
new Vector3(2.5f, 2.5f, 5f),
|
||||
new Vector3(2.5f, -2.5f, 5f),
|
||||
},
|
||||
Plane = new Plane(new Vector3(1, 0, 0), -2.5f), // x = 2.5
|
||||
NumPoints = 4,
|
||||
SidesType = DatReaderWriter.Enums.CullMode.None,
|
||||
};
|
||||
|
||||
Matrix4x4.Invert(worldTransform, out var inv);
|
||||
return new CellPhysics
|
||||
{
|
||||
WorldTransform = worldTransform,
|
||||
InverseWorldTransform = inv,
|
||||
Resolved = new Dictionary<ushort, ResolvedPolygon>(),
|
||||
PortalPolygons = new Dictionary<ushort, ResolvedPolygon> { [10] = portalPolyA },
|
||||
Portals = new[]
|
||||
{
|
||||
new PortalInfo(otherCellId: (ushort)otherCellId, polygonId: 10, flags: flags),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SphereInsideCellA_NearPortal_AddsCellB()
|
||||
{
|
||||
var cellA = MakeCellWithPortalAtRightWall(Matrix4x4.Identity, otherCellId: 0x0101, flags: 0);
|
||||
|
||||
var cellBT = Matrix4x4.CreateTranslation(new Vector3(5f, 0f, 0f));
|
||||
Matrix4x4.Invert(cellBT, out var cellBInv);
|
||||
var cellB = new CellPhysics
|
||||
{
|
||||
WorldTransform = cellBT,
|
||||
InverseWorldTransform = cellBInv,
|
||||
Resolved = new Dictionary<ushort, ResolvedPolygon>(),
|
||||
};
|
||||
|
||||
var cache = new PhysicsDataCache();
|
||||
cache.RegisterCellStructForTest(0xA9B40100u, cellA);
|
||||
cache.RegisterCellStructForTest(0xA9B40101u, cellB);
|
||||
|
||||
// Sphere center near portal (local x=2.0, radius=0.5 → reaches x=2.5 = portal plane).
|
||||
var worldSphereCenter = new Vector3(2.0f, 0f, 2.5f);
|
||||
|
||||
var candidates = new HashSet<uint>();
|
||||
CellTransit.FindTransitCellsSphere(
|
||||
cache, cellA, currentCellId: 0xA9B40100u,
|
||||
worldSphereCenter, sphereRadius: 0.5f, candidates, out bool exitOutside);
|
||||
|
||||
Assert.Contains(0xA9B40101u, candidates);
|
||||
Assert.False(exitOutside);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SphereInsideCellA_FarFromPortal_DoesNotAddCellB()
|
||||
{
|
||||
var cellA = MakeCellWithPortalAtRightWall(Matrix4x4.Identity, otherCellId: 0x0101, flags: 0);
|
||||
|
||||
var cache = new PhysicsDataCache();
|
||||
cache.RegisterCellStructForTest(0xA9B40100u, cellA);
|
||||
|
||||
// Sphere far from portal (local x=-1.0, reach to x=-0.5 — nowhere near portal at x=2.5).
|
||||
var worldSphereCenter = new Vector3(-1.0f, 0f, 2.5f);
|
||||
|
||||
var candidates = new HashSet<uint>();
|
||||
CellTransit.FindTransitCellsSphere(
|
||||
cache, cellA, currentCellId: 0xA9B40100u,
|
||||
worldSphereCenter, sphereRadius: 0.5f, candidates, out bool exitOutside);
|
||||
|
||||
Assert.DoesNotContain(0xA9B40101u, candidates);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExitPortal_SphereStraddlesPortalPlane_FlagsCheckOutside()
|
||||
{
|
||||
var exitCell = MakeCellWithPortalAtRightWall(Matrix4x4.Identity, otherCellId: 0xFFFF, flags: 0);
|
||||
|
||||
var cache = new PhysicsDataCache();
|
||||
cache.RegisterCellStructForTest(0xA9B40100u, exitCell);
|
||||
|
||||
var worldSphereCenter = new Vector3(2.0f, 0f, 2.5f);
|
||||
var candidates = new HashSet<uint>();
|
||||
|
||||
CellTransit.FindTransitCellsSphere(
|
||||
cache, exitCell, currentCellId: 0xA9B40100u,
|
||||
worldSphereCenter, sphereRadius: 0.5f, candidates, out bool exitOutside);
|
||||
|
||||
Assert.True(exitOutside);
|
||||
}
|
||||
}
|
||||
31
tests/AcDream.Core.Tests/Physics/ResolveCellIdTests.cs
Normal file
31
tests/AcDream.Core.Tests/Physics/ResolveCellIdTests.cs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
using System.Numerics;
|
||||
using AcDream.Core.Physics;
|
||||
using Xunit;
|
||||
|
||||
namespace AcDream.Core.Tests.Physics;
|
||||
|
||||
public class ResolveCellIdTests
|
||||
{
|
||||
[Fact]
|
||||
public void ResolveCellId_FallbackZero_ReturnsZero()
|
||||
{
|
||||
var engine = new PhysicsEngine();
|
||||
uint result = engine.ResolveCellId(Vector3.Zero, sphereRadius: 0.5f, fallbackCellId: 0u);
|
||||
Assert.Equal(0u, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResolveCellId_NoLandblock_OutdoorSeed_ReturnsFallback()
|
||||
{
|
||||
var engine = new PhysicsEngine();
|
||||
engine.DataCache = new PhysicsDataCache();
|
||||
// Outdoor seed with no landblock added → AddAllOutsideCells produces
|
||||
// candidates but none have a CellBSP → falls back to input.
|
||||
uint result = engine.ResolveCellId(
|
||||
new Vector3(100, 100, 0),
|
||||
sphereRadius: 0.5f,
|
||||
fallbackCellId: 0xA9B40001u);
|
||||
|
||||
Assert.Equal(0xA9B40001u, result);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,150 +0,0 @@
|
|||
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,
|
||||
};
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// 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,
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue