diff --git a/src/DotRecast.Core/CollectionExtensions.cs b/src/DotRecast.Core/CollectionExtensions.cs index b472f7e..1dd0173 100644 --- a/src/DotRecast.Core/CollectionExtensions.cs +++ b/src/DotRecast.Core/CollectionExtensions.cs @@ -21,9 +21,7 @@ namespace DotRecast.Core { n--; int k = random.Next(n + 1); - T value = list[k]; - list[k] = list[n]; - list[n] = value; + (list[k], list[n]) = (list[n], list[k]); } } } diff --git a/src/DotRecast.Core/IntersectResult.cs b/src/DotRecast.Core/IntersectResult.cs deleted file mode 100644 index 1bf8ba4..0000000 --- a/src/DotRecast.Core/IntersectResult.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace DotRecast.Core -{ - public class IntersectResult - { - public bool intersects; - public float tmin; - public float tmax = 1f; - public int segMin = -1; - public int segMax = -1; - } -} \ No newline at end of file diff --git a/src/DotRecast.Core/ConvexUtils.cs b/src/DotRecast.Core/RcConvexUtils.cs similarity index 98% rename from src/DotRecast.Core/ConvexUtils.cs rename to src/DotRecast.Core/RcConvexUtils.cs index 1e53d58..f17b654 100644 --- a/src/DotRecast.Core/ConvexUtils.cs +++ b/src/DotRecast.Core/RcConvexUtils.cs @@ -20,7 +20,7 @@ using System.Collections.Generic; namespace DotRecast.Core { - public static class ConvexUtils + public static class RcConvexUtils { // Calculates convex hull on xz-plane of points on 'pts', // stores the indices of the resulting hull in 'out' and diff --git a/src/DotRecast.Detour/DetourCommon.cs b/src/DotRecast.Detour/DetourCommon.cs index 81a7ff3..309e3da 100644 --- a/src/DotRecast.Detour/DetourCommon.cs +++ b/src/DotRecast.Detour/DetourCommon.cs @@ -339,10 +339,18 @@ namespace DotRecast.Detour return dx * dx + dz * dz; } - public static IntersectResult IntersectSegmentPoly2D(RcVec3f p0, RcVec3f p1, RcVec3f[] verts, int nverts) + public static bool IntersectSegmentPoly2D(RcVec3f p0, RcVec3f p1, + RcVec3f[] verts, int nverts, + out float tmin, out float tmax, + out int segMin, out int segMax) { - IntersectResult result = new IntersectResult(); const float EPS = 0.000001f; + + tmin = 0; + tmax = 1; + segMin = -1; + segMax = -1; + var dir = p1.Subtract(p0); var p0v = p0; @@ -359,7 +367,7 @@ namespace DotRecast.Detour // S is nearly parallel to this edge if (n < 0) { - return result; + return false; } else { @@ -371,35 +379,34 @@ namespace DotRecast.Detour if (d < 0) { // segment S is entering across this edge - if (t > result.tmin) + if (t > tmin) { - result.tmin = t; - result.segMin = j; + tmin = t; + segMin = j; // S enters after leaving polygon - if (result.tmin > result.tmax) + if (tmin > tmax) { - return result; + return false; } } } else { // segment S is leaving across this edge - if (t < result.tmax) + if (t < tmax) { - result.tmax = t; - result.segMax = j; + tmax = t; + segMax = j; // S leaves before entering polygon - if (result.tmax < result.tmin) + if (tmax < tmin) { - return result; + return false; } } } } - result.intersects = true; - return result; + return true; } public static int OppositeTile(int side) diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index f802ff0..02f8f78 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -2233,26 +2233,26 @@ namespace DotRecast.Detour nv++; } - IntersectResult iresult = DetourCommon.IntersectSegmentPoly2D(startPos, endPos, verts, nv); - if (!iresult.intersects) + bool intersects = DetourCommon.IntersectSegmentPoly2D(startPos, endPos, verts, nv, out var tmin, out var tmax, out var segMin, out var segMax); + if (!intersects) { // Could not hit the polygon, keep the old t and report hit. return DtStatus.DT_SUCCSESS; } - hit.hitEdgeIndex = iresult.segMax; + hit.hitEdgeIndex = segMax; // Keep track of furthest t so far. - if (iresult.tmax > hit.t) + if (tmax > hit.t) { - hit.t = iresult.tmax; + hit.t = tmax; } // Store visited polygons. hit.path.Add(curRef); // Ray end is completely inside the polygon. - if (iresult.segMax == -1) + if (segMax == -1) { hit.t = float.MaxValue; @@ -2274,7 +2274,7 @@ namespace DotRecast.Detour DtLink link = tile.links[i]; // Find link which contains this edge. - if (link.edge != iresult.segMax) + if (link.edge != segMax) { continue; } @@ -2331,7 +2331,7 @@ namespace DotRecast.Detour } // Find Z intersection. - float z = startPos.z + (endPos.z - startPos.z) * iresult.tmax; + float z = startPos.z + (endPos.z - startPos.z) * tmax; if (z >= lmin && z <= lmax) { nextRef = link.refs; @@ -2352,7 +2352,7 @@ namespace DotRecast.Detour } // Find X intersection. - float x = startPos.x + (endPos.x - startPos.x) * iresult.tmax; + float x = startPos.x + (endPos.x - startPos.x) * tmax; if (x >= lmin && x <= lmax) { nextRef = link.refs; @@ -2368,8 +2368,8 @@ namespace DotRecast.Detour // and correct the height (since the raycast moves in 2d) lastPos = curPos; curPos = RcVec3f.Mad(startPos, dir, hit.t); - var e1 = verts[iresult.segMax]; - var e2 = verts[(iresult.segMax + 1) % nv]; + var e1 = verts[segMax]; + var e2 = verts[(segMax + 1) % nv]; var eDir = e2.Subtract(e1); var diff = curPos.Subtract(e1); float s = Sqr(eDir.x) > Sqr(eDir.z) ? diff.x / eDir.x : diff.z / eDir.z; @@ -2384,8 +2384,8 @@ namespace DotRecast.Detour // No neighbour, we hit a wall. // Calculate hit normal. - int a = iresult.segMax; - int b = iresult.segMax + 1 < nv ? iresult.segMax + 1 : 0; + int a = segMax; + int b = segMax + 1 < nv ? segMax + 1 : 0; // int va = a * 3; // int vb = b * 3; float dx = verts[b].x - verts[a].x; @@ -2718,13 +2718,13 @@ namespace DotRecast.Detour } // If the poly is not touching the edge to the next polygon, skip the connection it. - IntersectResult ir = DetourCommon.IntersectSegmentPoly2D(va, vb, verts, nverts); - if (!ir.intersects) + bool intersects = DetourCommon.IntersectSegmentPoly2D(va, vb, verts, nverts, out var tmin, out var tmax, out var segMin, out var segMax); + if (!intersects) { continue; } - if (ir.tmin > 1.0f || ir.tmax < 0.0f) + if (tmin > 1.0f || tmax < 0.0f) { continue; } diff --git a/src/DotRecast.Recast.Demo/Tools/ConvexVolumeTool.cs b/src/DotRecast.Recast.Demo/Tools/ConvexVolumeTool.cs index 2fee53c..5b598ea 100644 --- a/src/DotRecast.Recast.Demo/Tools/ConvexVolumeTool.cs +++ b/src/DotRecast.Recast.Demo/Tools/ConvexVolumeTool.cs @@ -94,7 +94,7 @@ public class ConvexVolumeTool : IRcTool if (pts.Count > 3) { hull.Clear(); - hull.AddRange(ConvexUtils.Convexhull(pts)); + hull.AddRange(RcConvexUtils.Convexhull(pts)); } else {