Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NavigationAgent2D has repath loop issue #95628

Open
rob1gat opened this issue Aug 16, 2024 · 2 comments
Open

NavigationAgent2D has repath loop issue #95628

rob1gat opened this issue Aug 16, 2024 · 2 comments

Comments

@rob1gat
Copy link

rob1gat commented Aug 16, 2024

Tested versions

  • Reproducible in 4.3.stable, 4.2.1.stable and 4.1.2.stable

System information

Godot v4.3.stable - Linux Mint 21.3 (Virginia) - X11 - Vulkan (Forward+) - dedicated AMD Radeon RX 6700 XT (RADV NAVI22) - 12th Gen Intel(R) Core(TM) i5-12400 (12 Threads)

Issue description

In this scenario, Blue Godot wants to go to Green Godot :
nav_issue_1

But Blue Godot is stuck in place as it constantly hesitates between 2 paths :
nav_issue_2
nav_issue_3

Blue Godot path_desired_distance is equal to 10. If I set the path_desired_distance to 20, Blue Godot manages to choose the lower path, but crosses the red obstacle.

This scenario is a simplification of a more complex scenario that suddenly happened in a complex project, with multiple NPCs that constantly use navigation in various scenarios. I tried to adjust path_desired_distance in my project but when I set it higher it breaks on other scenarios (basically, when I set higher values, NPCs paths sometime cross obstacles but NPCs cannot cross these obstacles due to physical collision).

About this simple scenario, I don't understand how I can end in a repath loop since Blue Godot does not seem to overshoot distance to the next point. Is it kind of a path finding heuristic edge case ?

Steps to reproduce

Just run the MRP

Minimal reproduction project (MRP)

mrp_nav_repath_loop.zip

@smix8
Copy link
Contributor

smix8 commented Aug 16, 2024

Thanks for reporting. This not an engine bug but a responsibility of the script coding to fix.

The reason why it is even happening is because the script sets a new target position every single frame.

This queries a new path everytime and resets the entire path every single frame. In turn this breaks every consistency in the movement. Querying a new path is not something that you find in the official documentation script example for this very reason. It is also mentioned in the documentation under the common agent issues with more info why it is happening. https://docs.godotengine.org/en/latest/tutorials/navigation/navigation_using_navigationagents.html#pathfollowing-common-problems

So what should be done instead is to only update the path when necessary , e.g. by a distance check when the target has moved to far away from the current target position, or with a timer that does not fire every single frame. This will also improve performance so for the hypothetical complex project this will be a necessary step anyway.

@rob1gat
Copy link
Author

rob1gat commented Aug 16, 2024

Thanks for your fast and clear answer.

I updated my code by adding a distance check and it works now.

For some reason, I was thinking that the entire path was reset each frame by get_next_path_position() method (instead of set_target_position()), so I have assumed that path resetting was inevitable.

Maybe the documentation you linked could be updated to add some hints on what could trigger a path reset ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants