From fa1e0eaeaaf71e2105b1dddd548e4efa0bf9be7c Mon Sep 17 00:00:00 2001 From: ikpil Date: Sat, 1 Jul 2023 00:18:25 +0900 Subject: [PATCH] Added DtConnectPoly --- src/DotRecast.Detour/DtConnectPoly.cs | 18 +++++++++++++++ src/DotRecast.Detour/DtNavMesh.cs | 32 +++++++++++++++------------ 2 files changed, 36 insertions(+), 14 deletions(-) create mode 100644 src/DotRecast.Detour/DtConnectPoly.cs diff --git a/src/DotRecast.Detour/DtConnectPoly.cs b/src/DotRecast.Detour/DtConnectPoly.cs new file mode 100644 index 0000000..c0b42d8 --- /dev/null +++ b/src/DotRecast.Detour/DtConnectPoly.cs @@ -0,0 +1,18 @@ +using System.Runtime.InteropServices; + +namespace DotRecast.Detour +{ + public readonly struct DtConnectPoly + { + public readonly long refs; + public readonly float tmin; + public readonly float tmax; + + public DtConnectPoly(long refs, float tmin, float tmax) + { + this.refs = refs; + this.tmin = tmin; + this.tmax = tmax; + } + } +} \ No newline at end of file diff --git a/src/DotRecast.Detour/DtNavMesh.cs b/src/DotRecast.Detour/DtNavMesh.cs index 56d47e1..e784ea7 100644 --- a/src/DotRecast.Detour/DtNavMesh.cs +++ b/src/DotRecast.Detour/DtNavMesh.cs @@ -724,6 +724,8 @@ namespace DotRecast.Detour return; } + var connectPolys = new List(); + // Connect border links. for (int i = 0; i < tile.data.header.polyCount; ++i) { @@ -750,12 +752,12 @@ namespace DotRecast.Detour // Create new links int va = poly.verts[j] * 3; int vb = poly.verts[(j + 1) % nv] * 3; - IList> connectedPolys = FindConnectingPolys(tile.data.verts, va, vb, target, DetourCommon.OppositeTile(dir)); - foreach (Tuple connectedPoly in connectedPolys) + int nnei = FindConnectingPolys(tile.data.verts, va, vb, target, DetourCommon.OppositeTile(dir), ref connectPolys); + foreach (var connectPoly in connectPolys) { int idx = AllocLink(tile); DtLink link = tile.links[idx]; - link.refs = connectedPoly.Item1; + link.refs = connectPoly.refs; link.edge = j; link.side = dir; @@ -765,9 +767,9 @@ namespace DotRecast.Detour // Compress portal limits to a byte value. if (dir == 0 || dir == 4) { - float tmin = (connectedPoly.Item2 - tile.data.verts[va + 2]) + float tmin = (connectPoly.tmin - tile.data.verts[va + 2]) / (tile.data.verts[vb + 2] - tile.data.verts[va + 2]); - float tmax = (connectedPoly.Item3 - tile.data.verts[va + 2]) + float tmax = (connectPoly.tmax - tile.data.verts[va + 2]) / (tile.data.verts[vb + 2] - tile.data.verts[va + 2]); if (tmin > tmax) { @@ -781,9 +783,9 @@ namespace DotRecast.Detour } else if (dir == 2 || dir == 6) { - float tmin = (connectedPoly.Item2 - tile.data.verts[va]) + float tmin = (connectPoly.tmin - tile.data.verts[va]) / (tile.data.verts[vb] - tile.data.verts[va]); - float tmax = (connectedPoly.Item3 - tile.data.verts[va]) + float tmax = (connectPoly.tmax - tile.data.verts[va]) / (tile.data.verts[vb] - tile.data.verts[va]); if (tmin > tmax) { @@ -887,14 +889,14 @@ namespace DotRecast.Detour } } - private IList> FindConnectingPolys(float[] verts, int va, int vb, DtMeshTile tile, int side) + private int FindConnectingPolys(float[] verts, int va, int vb, DtMeshTile tile, int side, + ref List cons) { if (tile == null) - { - return ImmutableArray>.Empty; - } + return 0; + + cons.Clear(); - List> result = new List>(); RcVec2f amin = RcVec2f.Zero; RcVec2f amax = RcVec2f.Zero; CalcSlabEndPoints(verts, va, vb, ref amin, ref amax, side); @@ -904,6 +906,7 @@ namespace DotRecast.Detour RcVec2f bmin = RcVec2f.Zero; RcVec2f bmax = RcVec2f.Zero; int m = DT_EXT_LINK | side; + int n = 0; long @base = GetPolyRefBase(tile); for (int i = 0; i < tile.data.header.polyCount; ++i) @@ -939,12 +942,13 @@ namespace DotRecast.Detour long refs = @base | (long)i; float tmin = Math.Max(amin.x, bmin.x); float tmax = Math.Min(amax.x, bmax.x); - result.Add(Tuple.Create(refs, tmin, tmax)); + cons.Add(new DtConnectPoly(refs, tmin, tmax)); + n++; break; } } - return result; + return n; } static float GetSlabCoord(float[] verts, int va, int side)