godot: NavigationAgent gets bugged when using tilemaps, NavigationRegion and NavigationObstacles

Godot version

4.0.beta (935a6ef46b7c6af4239b6adce41b8198f051b57a)

System information

Ubuntu 20.04.5 LTS

Issue description

NavigationAgent in combination with NavigationRegion seems to be broken.

First of all, there is a big issue when drawing the Polygons. I constantly get this kind of shapes, where the Navigation mesh is not correctly being recomputed. ploygon

With the following errors in the output:

  scene/resources/navigation_polygon.cpp:296 - NavigationPolygon: Convex partition failed! Failed to convert outlines to a valid NavigationMesh.
  NavigationPolygon outlines can not overlap vertices or edges inside same outline or with other outlines or have any intersections.
  Add the outmost and largest outline first. To add holes inside this outline add the smaller outlines with same winding order.

And the following error in the debugger:

E 0:00:00:0560   sync: Attempted to merge a navigation mesh triangle edge with another already-merged edge. This happens when the current `cell_size` is different from the one used to generate the navigation mesh. This will cause navigation problems.
  <C++ Source>   modules/navigation/nav_map.cpp:691 @ sync()

So to get around this issue, I opted to do small navigationRegions and link them with a NavigationLink. And I managed to make NPCs be able to navigate from A to B and B to A. The problem is that they get stuck if they cross paths:

stuck

So trying to avoid that I add a NavigationObstacle2D to the NPC class (CharacterBody2D). And after running the project, the NPCs start running to the walls, Michael Jackson style:

mj

Was using Godot 4 Beta 16, but since I found this issues I updated to latest master yesterday night in the hopes of fixing them. But still same behavior.

I am not sure at what point it get broken, not sure if it’s the tilemap, or what, but the smallest minimal example works. As soon as you start adding stuff it breaks. One thing I thought could be, is that once you cross one vertex of the NavigationPolygon, everything in the project related to Navigation gets messed. Which is how I believe I broke the sample project.

Steps to reproduce

I am not sure at what point it gets broken, not sure if it’s the tilemap, or some other setting, but this small sample project reproduces some of these issues.

This project is trying to spawn some CharacterBody2D and move them from one side to the other. There is a tilemap (with no navigation configured) and a NavigationRegion to mark the navigation area.

This navigation region is already broken, meaning, I made some polygons inside following the winding order and holes were not made. Also, the mesh is not being reconfigured for the top vertex (if you see, the navigation area does not include the area of the top vertex).

Another thing is that the Character has a NavigationObstacle that seems to have no effect, since no NPCs are trying to avoid themselves? And in the original project makes them walk backwards, messing with the safe_velocity.

You can play with the sample project by moving the SpawnPoints and TargetPoints. For example, moving them to the right NavigationRegion depending on which area of it, makes the spawned nodes go to IDLE mode, since they don’t find the target point as reachable.

Minimal reproduction project

sample.zip

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 19 (19 by maintainers)

Most upvoted comments

Godot 4.0 is already a release candidate. There will be no new major reworks in it.

The fix needed is far from a “small” change as it requires e.g. an entire new library to be integrated and plenty of behind the scene changes to the navigaton polygon creation. E.g. you need to provide users with “draw rules” option to actually give them the option to solve complex polygons with holes in it in a way they intend it.

Also the current NavigationPolygon draw tool is not per se “broken”. It is the same as has been for many years and users learned somehow to life with its brittleness and annoyances. It is nonetheless an issue that needs a longterm fix.

For the current situation you need to be careful to avoid creating stuff that breaks it. E.g. overlapping or crossing outline edges. Sometimes it also breaks cause you looked at it funny but that can’t be helped for now and needs to wait for the rework.

The “Convex partition failed!” error is common error shared and tracked with older issues or very recent like e.g. https://github.com/godotengine/godot/issues/70823 and https://github.com/godotengine/godot/issues/72761.

The pr https://github.com/godotengine/godot/pull/70724 should include a proper fix for that, currently not visible in the pr but included in a development branch to update it.

We can keep this issue here open and wait until it is merged / confirmed that the pr fixes this issue here as well.

Well that the entire drawing with all NavigationRegion2D was bugged was a project issue, something corrupted and I was only able to fix it in your project by removing all navigation related nodes and starting over. I can confirm that after doing that I can recreate your navigation layout without any issues using multiple NavigationRegion2D and polygons.

If you did that and now with a fresh single NavigationRegion2D and fresh NavigationPolygon resource still have this error it might be caused by the mentioned overlap / intersection or that the Editor 2D polygon draw tool can not figure out the outlines. That a single polygon gets the Convex Partition error can happen in rare cases due to how the outline vertices are placed and how the draw tool tries to figure out what is outside and what is inside see here for related issue.

The Editor 2D polygon draw tool has some problems with detecting outlines when there are holes in the polygon and the vertices are placed in a way that if you draw a virtual straight line from an outside vertex to an inside vertex it crosses another vertex. You can fix this by moving the middle vertex a little so it does not stay directly on the virtual line between the other two vertices, … or instead of battling with holes have multiple NavigationRegion2D with different NavigationPolygon, if you align their edges they will be merged by the NavigationServer.

You do not attach NavigationObstacle nodes to NavigationAgents. You only use the NavigationObstacle on moving “obstacles” that are not agents with pathfinding, e.g. some environment hazard moving back and forth in your game.

In current Godot 4 NavigationAgent and NavigationObstacle both create a NavAgent used in avoidance on the NavigationServer. They are basically the same thing just with different properties so by attaching an obstacle on top of your agent you are basically making the agent avoid itself which is an impossible situation that can only result in a broken avoidance simulation. The current Godot 4 avoidance is also very unstable so I would not really recommend using it until the rework in Godot 4.1 as it can bug out by itself without you doing anything wrong.

Also in current Godot 4 beta the Editor Input has bugs, creating duplicated Inputs which causes duplicated polygon vertices. Try moving the vertices if you get the bug and see if a duplicate was hidden below another vertex.