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(), PortalPolygons = new Dictionary { [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(), }; 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(); 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(); 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(); CellTransit.FindTransitCellsSphere( cache, exitCell, currentCellId: 0xA9B40100u, worldSphereCenter, sphereRadius: 0.5f, candidates, out bool exitOutside); Assert.True(exitOutside); } }