Skip to content

Commit b92ac1f

Browse files
committed
fixed broken path finding with new version
1 parent 37e084e commit b92ac1f

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

sources/engine/Stride.Navigation/InternalNavigationMesh.cs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Linq;
67
using System.Runtime.InteropServices;
78
using DotRecast.Core.Numerics;
89
using DotRecast.Detour;
@@ -79,14 +80,32 @@ public void DoPathFindQuery(PathFindQuery query, ref PathFindResult result)
7980
return;
8081

8182
long[] polys = new long[query.MaxPathPoints];
82-
status = navQuery.FindPath(startPoly, endPoly, startPoint, endPoint, filter, polys, out var pathCount, query.MaxPathPoints);
83-
if (status.Failed() || status.IsPartial())
84-
return;
83+
navQuery.FindPath(startPoly, endPoly, startPoint, endPoint, filter, polys, out var pathCount, polys.Length);
8584

86-
var pathPointsSpan = CollectionsMarshal.AsSpan(result.PathPoints);
87-
status = navQuery.FindStraightPath(startPoint, endPoint, polys, polys.Length, pathPointsSpan, out var straightPathCount, query.MaxPathPoints, 0);
88-
if (status.Failed())
85+
if (0 >= pathCount)
86+
{
8987
return;
88+
}
89+
90+
// In case of partial path, make sure the end point is clamped to the last polygon.
91+
var endPosition = new RcVec3f(endPoint.X, endPoint.Y, endPoint.Z);
92+
if (polys[pathCount - 1] != endPoly)
93+
{
94+
status = navQuery.ClosestPointOnPoly(polys[pathCount - 1], endPoint, out var closest, out var _);
95+
if (status.Succeeded())
96+
{
97+
endPosition = closest;
98+
}
99+
}
100+
101+
// Due to Dotrecast using Spans, we need to allocate the array with the max size possible then resize it later to remove empty entries.
102+
// TODO: By default we allocate 1024 points which is way more than enough for most cases and should maybe be defaulted to a smaller value in the future.
103+
result.PathPoints = new DtStraightPath[query.MaxPathPoints];
104+
navQuery.FindStraightPath(startPoint, endPosition, polys, pathCount, result.PathPoints, out var straightPathCount, query.MaxPathPoints, 0);
105+
106+
// cut out the empty entries
107+
Array.Resize(ref result.PathPoints, straightPathCount);
108+
90109
result.PathFound = true;
91110
}
92111

sources/engine/Stride.Navigation/Navigation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ internal struct PathFindQuery
2727
internal struct PathFindResult
2828
{
2929
public bool PathFound;
30-
public List<DtStraightPath> PathPoints;
30+
public DtStraightPath[] PathPoints;
3131
}
3232

3333
internal struct BuildSettings

sources/engine/Stride.Navigation/Processors/RecastNavigationMesh.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ public bool TryFindPath(Vector3 start, Vector3 end, ICollection<Vector3> path, N
8686
query.FindNearestPolyExtent = querySettings.FindNearestPolyExtent;
8787
PathFindResult queryResult = default;
8888

89-
queryResult.PathPoints = new List<DtStraightPath>(querySettings.MaxPathPoints);
89+
queryResult.PathPoints = new DtStraightPath[querySettings.MaxPathPoints];
9090
navmesh.DoPathFindQuery(query, ref queryResult);
9191
if (!queryResult.PathFound)
9292
return false;
9393

94-
for (int i = 0; i < queryResult.PathPoints.Count; i++)
94+
for (int i = 0; i < queryResult.PathPoints.Length; i++)
9595
{
9696
path.Add(queryResult.PathPoints[i].pos.ToStrideVector());
9797
}

0 commit comments

Comments
 (0)