From dbb03988f58f966ae8149d703c54c1538a780472 Mon Sep 17 00:00:00 2001 From: ikpil Date: Thu, 1 Feb 2024 02:21:07 +0900 Subject: [PATCH] fix: DtRaycastHit SOH issue reslove --- src/DotRecast.Detour.Crowd/DtPathCorridor.cs | 7 +++--- src/DotRecast.Detour/DtNavMeshQuery.cs | 23 +++++++++---------- src/DotRecast.Detour/DtRaycastHit.cs | 11 +-------- .../Tools/RcTestNavMeshTool.cs | 3 ++- 4 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/DotRecast.Detour.Crowd/DtPathCorridor.cs b/src/DotRecast.Detour.Crowd/DtPathCorridor.cs index 8ceaf5b..3dbdc3b 100644 --- a/src/DotRecast.Detour.Crowd/DtPathCorridor.cs +++ b/src/DotRecast.Detour.Crowd/DtPathCorridor.cs @@ -194,12 +194,13 @@ namespace DotRecast.Detour.Crowd var delta = RcVec3f.Subtract(next, m_pos); RcVec3f goal = RcVecUtils.Mad(m_pos, delta, pathOptimizationRange / dist); - var status = navquery.Raycast(m_path[0], m_pos, goal, filter, out var t, out var norm, out var path); + var res = new List(); + var status = navquery.Raycast(m_path[0], m_pos, goal, filter, out var t, out var norm, ref res); if (status.Succeeded()) { - if (path.Count > 1 && t > 0.99f) + if (res.Count > 1 && t > 0.99f) { - m_path = DtPathUtils.MergeCorridorStartShortcut(m_path, path); + m_path = DtPathUtils.MergeCorridorStartShortcut(m_path, res); } } } diff --git a/src/DotRecast.Detour/DtNavMeshQuery.cs b/src/DotRecast.Detour/DtNavMeshQuery.cs index d581a4b..940d754 100644 --- a/src/DotRecast.Detour/DtNavMeshQuery.cs +++ b/src/DotRecast.Detour/DtNavMeshQuery.cs @@ -793,7 +793,8 @@ namespace DotRecast.Detour DtNode lastBestNode = startNode; float lastBestNodeCost = startNode.total; - + DtRaycastHit rayHit = new DtRaycastHit(); + rayHit.path = new List(); while (!m_openList.IsEmpty()) { // Remove node from open list and put it in closed list. @@ -892,7 +893,6 @@ namespace DotRecast.Detour List shortcut = null; if (tryLOS) { - var rayHit = new DtRaycastHit(0); var rayStatus = Raycast(parentRef, parentNode.pos, neighbourPos, filter, DtRaycastOptions.DT_RAYCAST_USE_COSTS, ref rayHit, grandpaRef); if (rayStatus.Succeeded()) @@ -900,7 +900,7 @@ namespace DotRecast.Detour foundShortCut = rayHit.t >= 1.0f; if (foundShortCut) { - shortcut = rayHit.path; + shortcut = new List(rayHit.path); // shortcut found using raycast. Using shorter cost // instead cost = parentNode.cost + rayHit.pathCost; @@ -1090,6 +1090,9 @@ namespace DotRecast.Detour return DtStatus.DT_FAILURE; } + var rayHit = new DtRaycastHit(); + rayHit.path = new List(); + int iter = 0; while (iter < maxIter && !m_openList.IsEmpty()) { @@ -1214,7 +1217,6 @@ namespace DotRecast.Detour List shortcut = null; if (tryLOS) { - var rayHit = new DtRaycastHit(0); status = Raycast(parentRef, parentNode.pos, neighbourPos, m_query.filter, DtRaycastOptions.DT_RAYCAST_USE_COSTS, ref rayHit, grandpaRef); if (status.Succeeded()) @@ -1222,7 +1224,7 @@ namespace DotRecast.Detour foundShortCut = rayHit.t >= 1.0f; if (foundShortCut) { - shortcut = rayHit.path; + shortcut = new List(rayHit.path); // shortcut found using raycast. Using shorter cost // instead cost = parentNode.cost + rayHit.pathCost; @@ -2155,19 +2157,16 @@ namespace DotRecast.Detour /// public DtStatus Raycast(long startRef, RcVec3f startPos, RcVec3f endPos, IDtQueryFilter filter, - out float t, out RcVec3f hitNormal, out List path) + out float t, out RcVec3f hitNormal, ref List path) { - DtRaycastHit hit = new DtRaycastHit(0); - // hit.path = path; - // hit.maxPath = maxPath; + DtRaycastHit hit = new DtRaycastHit(); + hit.path = path; DtStatus status = Raycast(startRef, startPos, endPos, filter, 0, ref hit, 0); t = hit.t; hitNormal = hit.hitNormal; path = hit.path; - // if (pathCount) - // *pathCount = hit.pathCount; return status; } @@ -2224,7 +2223,7 @@ namespace DotRecast.Detour /// @param[out] pathCount The number of visited polygons. [opt] /// @param[in] maxPath The maximum number of polygons the @p path array can hold. /// @returns The status flags for the query. - public DtStatus Raycast(long startRef, RcVec3f startPos, RcVec3f endPos, + public DtStatus Raycast(long startRef, RcVec3f startPos, RcVec3f endPos, IDtQueryFilter filter, int options, ref DtRaycastHit hit, long prevRef) { diff --git a/src/DotRecast.Detour/DtRaycastHit.cs b/src/DotRecast.Detour/DtRaycastHit.cs index 3e68e3d..d087ed8 100644 --- a/src/DotRecast.Detour/DtRaycastHit.cs +++ b/src/DotRecast.Detour/DtRaycastHit.cs @@ -38,18 +38,9 @@ namespace DotRecast.Detour public int hitEdgeIndex; /// Pointer to an array of reference ids of the visited polygons. [opt] - public readonly List path; + public List path; /// The cost of the path until hit. public float pathCost; - - public DtRaycastHit(int s) - { - t = 0; - hitEdgeIndex = 0; - hitNormal = RcVec3f.Zero; - path = new List(); - pathCost = 0; - } } } \ No newline at end of file diff --git a/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs b/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs index 96d1dee..62d20c7 100644 --- a/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs +++ b/src/DotRecast.Recast.Toolset/Tools/RcTestNavMeshTool.cs @@ -257,7 +257,8 @@ namespace DotRecast.Recast.Toolset.Tools return DtStatus.DT_FAILURE; } - var status = navQuery.Raycast(startRef, startPos, endPos, filter, out var t, out var hitNormal2, out var path); + var path = new List(); + var status = navQuery.Raycast(startRef, startPos, endPos, filter, out var t, out var hitNormal2, ref path); if (!status.Succeeded()) { return status;