#Implementing dynamic obstacles for NavigationAgent

1 messages · Page 1 of 1 (latest)

drowsy aurora
#

I made simple character movement using NavigationAgent2D and it moves on NavRegion perfectly. But now I need to add walls that may be spawned on any place on the level and Nav should build path taking that wall into account. Walls may be destroyed by damage or timer, may be built new walls on the level and so on but NavRegion is unchangable.
So I don't understand how to make NavAgent to make path around those walls. As far as I know using NavigationObstacle is not a good idea since that walls are static. So how to implement wall avoidance?

drowsy aurora
#

I used navigation_agent_2d.set_velocity(new_velocity) and got signal from NavAgent

func _on_navigation_agent_2d_velocity_computed(safe_velocity):
    velocity = safe_velocity
    move_and_slide()

and I don't understand why character still runs into walls

drowsy aurora
#

Hint for NavObstacle says that NavPolygon should be simply refreshed , as far as I understand it can be refreshed ingame when something happened like wall spawned, but how can I do it?

rose crag
#

You get the NavigationPolygon resource of your NavigationRegion2D, duplicate it, keep the original version as a backup, now change the duplicate in script for your dynamic stuff e.g. like a placed wall by removing that section of the polygon, than you replace the current NavigationPolygon on the NavigationRegion2D with the new one.

#

The NavigationObstacles are only for avoidance and the avoidance velocities, they wouldn't change anything about your pathfinding. If you do not cut "holes" in your polygons for you obstructions like walls the paths will lead agents still through the sections that you do not want them to use. You see that in your video, the path points still go through that "obstacle" because the navigation polygon is still below.

#

you can use the helper functions on the Geometry2D class to cut holes in polygons.

drowsy aurora
#

The problem is I cannot just make different NavPolygons cause walls may be spawned anywhere (a spell). I found somewhere it's possible to dynamically refresh NavPolygon but it's resource costly so not a good way to use while it game since it may cause freezes.
Is there really no ways to add obstacles that NavigationAgent should avoid building pathway?

rose crag
#

pathfinding uses navigation meshes, you need to change the navigation mesh if you want pathfinding to change. No way around that.

#

pr https://github.com/godotengine/godot/pull/70724 will add a 2D navigation mesh bake functionality at some point but until then you need to work around that with scripts. The easist way is usually to have a NavigationRegion2D with a large navigation polygon that covers your gameplay area (or at least a large section of it in case your game is huuuge). You keep this navigation polygon with a copy as backup and when you place dynamic gameplay objects that should change it you use Geometry2D functions to cut holes in it.

GitHub

Supersedes #64682 and #70174
For context, the NavigationMeshGenerator singleton is responsible for navigation mesh baking and source geometry parsing from the SceneTree. SourceGeometryData is navig...

#

Complex games like e.g. StarCraft2 did their entire navigation system like that. They kept a copy of the levels entire navigation mesh that only included the static objects. Everytime they added a dynamic object like e.g. a building they loaded the original navigation mesh, cut the holes for all dynamic objects on the map, and used that.

drowsy aurora