Godot Version
4.3
Question
Hello,
I’m working on a top down view game where the player have to clear a room before entering the next one. When a room is leaved, it’s queue freed and the new one is instanciate.
Every room have a NavigationRegion2D. Until now I drew the polygon and baked from the editor manually for every room and it worked very well. But to gain time on room creation I wanted to do that via code, but I encounter a problem : on the first room the navigation area is fine, but from the second one it keeps the previously deleted areas from the shape
Room 1 :
Room 2 : (this is the exact shape of the previous room)
I tried to let the NavigationPolygon empty in the NavigationRegion and create a new one everytime, to use the same, get it and clear it and use NavigationServer2D.bake_from_source_geometry_data with a NavigationMeshSourceGeometryData2D where I add_traversable_outline = to a polygon who cover the entire room.
To bake the shape, I’m getting the coordinates of the corners of the room (obtained with wall’s TileMapLayer used cells) and then remove every collisionshape in a “Navigation” group
So everytime a room is instanciate, I call a method with this code:
var navigation_region = get_current_room().get_node("NavigationRegion2D")
var current_nav_polygon = navigation_region.get_navigation_polygon()
current_nav_polygon.clear()
var nav_polygon = NavigationPolygon.new()
nav_polygon.parsed_collision_mask = 9
nav_polygon.source_geometry_mode = 2
nav_polygon.source_geometry_group_name = "Navigation"
nav_polygon.agent_radius = 5
var top_left = Vector2(map_limits[0],map_limits[2])
var bottom_left = Vector2(map_limits[0],map_limits[3])
var bottom_right = Vector2(map_limits[1],map_limits[3])
var top_right = Vector2(map_limits[1],map_limits[2])
var poly_outlines:PackedVector2Array=[top_left,bottom_left,bottom_right,top_right]
nav_polygon.add_outline(poly_outlines)
navigation_region.navigation_polygon = nav_polygon
navigation_region.bake_navigation_polygon()
I wanted to use a NavigationMeshSourceGeometryData2D to add_obstruction_outline, but the collision polygons are only on a collision layer of the tile set and they don’t always fit the size of the tiles.
Do anyone know how to solve this ?
EDIT : I’m testing with 2 differents rooms, both are scenes and their path are in dictionary. I choose randomly one, then delete the entry from the dictionary, so the door lead to the other one and the dictionary is restored with both. Sometimes leaving a room leads to the same. In this case the navigation works in the second room
Room 1:
Room 2:
Room3: