Fix closest possible navigation path position #79004
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes closest possible navigation path position.
Fixes #78943.
This fixes two edge cases in the navigation mesh pathfinding related to unreachable target positions. It wouldn't affect pathfinding when the target position is reachable.
In the situation of an unreachable target position the users expect a path returned that goes from the closest point to the start position to the closest possible point reachable to the target position.
Without the fixes in this pr the path would in certain cases be either empty or end at the wrong position on an unexpected polygon.
The first edge case was that an empty path is returned as the fallback when no route is found at all. This is the case when the starting polygon has not a single connected edge, e.g. is a single, isolated triangle or other convex polygon. Since the pathfinding loop goes through connections it does not run to the code point where it would add path segments for a basic route, returning the empty path as the defined fallback. Now in this case an additional search of the start polygon faces is added.
The second edge case was if the target position is on an unreachable polygon and the start polygon happens to be the one with the closest possible position to the target position, it would still pick positions on other polygons. The reason for this is that the first polygon is never fully added to the path corridor, only the segment from the start position to the first connected edge is added. This means the start polygons entire faces are not considered in the search for the closest, possible position to the target. Only position along the line segment from start position to edge are. If another polygon would have a point closer than this segment the path would use the other polygon instead even if visually the user would see the start polygon being closer. Now in this case a full search of the start polygon faces is added.