Godot Version
v4.3.stable.arch_linux
Question
I’m trying to use the 2D Navigation nodes to enable character pathfinding in my project, which features a procedurally-generated 2D world sorted into 256x256 pixel chunks, represented in the engine as custom Node2Ds.
I generate a NavigationRegion2D for each chunk upon chunk generation like so:
chunk.navigation_region = NavigationRegion2D.new()
var navigation_mesh = NavigationPolygon.new()
var navigation_mesh_vertices = PackedVector2Array([
Vector2(chunk_size * x, chunk_size * y),
Vector2(chunk_size * x, chunk_size * (y+1)),
Vector2(chunk_size * (x+1), chunk_size * (y+1)),
Vector2(chunk_size * (x+1), chunk_size * y)
])
navigation_mesh.vertices = navigation_mesh_vertices
var polygon_indices = PackedInt32Array([0, 1, 2, 3])
navigation_mesh.add_polygon(polygon_indices)
chunk.navigation_region.navigation_polygon = navigation_mesh
chunk.add_child(chunk.navigation_region)
My characters each have a NavigationAgent2D, and up to this point in the setup they have no problem setting their target_position and walking to it after querying get_next_path_position().
My problem occurs when I try to add a NavigationObstacle2D to the chunk, as a child of the chunk’s NavigationRegion2D (“Palm” in the variable names because this obstacle represents the trunk of a palm tree):
var palm_navigation_obstacle = NavigationObstacle2D.new()
var palm_navigation_obstacle_vertices = PackedVector2Array([
Vector2(8, 8),
Vector2(-8, 8),
Vector2(-8, 0),
Vector2(8, 0)
])
palm_navigation_obstacle.vertices = palm_navigation_obstacle_vertices
palm_navigation_obstacle.position = palm.position
palm_navigation_obstacle.affect_navigation_mesh = true
palm_navigation_obstacle.avoidance_enabled = true
palm_navigation_obstacle.carve_navigation_mesh = true
palm_navigation_obstacle.radius = 8
chunk.navigation_region.add_child(palm_navigation_obstacle)
The agents completely ignore these obstacles, they walk right through them. Assuming perhaps this is because carve_navigation_mesh
doesn’t work until baked, I try re-baking the NavigationRegion after the chunk is added to the world…
chunk.navigation_region.bake_navigation_polygon()
…Which breaks agent navigation completely, they just stand in place. I assume because this call also breaks the NavigationRegion2D.
Please aid me, Godot geniuses! Is there a viable way here to generate NavigationObstacle2Ds / carve up existing NavigationRegion2Ds at run time?
[edit: I’ve since confirmed using the debug visuals, that calling bake_navigation_polygon() on the NavigationRegion2Ds at run time deletes their polygon geometry. I’ve also tested with Polygon2D child objects in place of NavigationObstacle2D child objects, same result! Instead of the child scene geometry punching holes in the mesh, the mesh just disappears.]