We’ve been encountering an out of range exception while using the RecastGraph’s line casting methods (which indirects into the NavMeshGraph’s linecasting methods; sorry if I categorized this thread incorrectly!). I’ve tried to find a reproducible test case, but it’s been very elusive and only crops up once every couple of weeks.
It appears the ‘left’ array ends up with 0 elements in it despite the fact that the Linecast method depends on it being at least of length 1? I’m not sure if this entails a small logic error somewhere in GetPortal, or if we’re doing something silly I haven’t been able to detect, or what.
We are indeed doing a bunch of graph slicing (often immediately before attempting to do all these linecasts!). HOWEVER, all the math I’m doing takes place within the callback of an AstarPath.active.AddWorkItem() invocation, which I had assumed would wait for the completion of any recast recalculations?
So the linecasts are also done inside a work item? If so, nothing else should be interfering.
Also, do you think you could post a small snippet of code from where the exception was thrown? The line numbers differ between different versions, so it’s hard to pinpoint the exact location by just looking at the backtrace.
The particular line at which the exception occurs is:
“Vector3 a = left[0];”
And here is some surrounding context:
left.Clear();
right.Clear();
if (!node.GetPortal (node.connections[i],left,right,false)) continue;
Vector3 a = left[0];
Vector3 b = right[0];
//i.e Right or colinear
if (!Polygon.LeftNotColinear (a,b,hit.origin)) {
if (Polygon.LeftNotColinear (a, b, tmp_end)) {
//Since polygons are laid out in clockwise order, the ray would intersect (if intersecting) this edge going in to the node, not going out from it
continue;
}
}
if (!node.GetPortal(node.connections[i].node, left, right, false)) continue;
Vector3 a = left[0];
Vector3 b = right[0];
//i.e Left or colinear
if (!VectorMath.RightXZ(a, b, hit.origin)) {
if (VectorMath.RightXZ(a, b, tmp_end)) {
//Since polygons are laid out in clockwise order, the ray would intersect (if intersecting) this edge going in to the node, not going out from it
continue;
}
}
Ah. Yes that indeed looks like a bug. I couldn’t replicate it when it was originally posted, but your hint about using links made it easier to debug.
I think the issue stems from an incorrect return value in the GetPortal method. It would only happen if the two nodes were in different tiles, but were still connected while not sharing an edge.
Here is a diff of the required fix (I think).
If you find the TriangleMeshNode.GetPortal method, go to the bottom and add that ‘return true’ line and change the ‘return true’ line at the bottom of the method to ‘return false’. Let me know if this fixes the bug.