Home Game Development Procedural NavigationPolygon for a platformer in Godot 4

Procedural NavigationPolygon for a platformer in Godot 4

0
Procedural NavigationPolygon for a platformer in Godot 4

[ad_1]

I am attempting to make navigation doable for npcs in a 2D platformer.

The sport generates chunks with random platforms because the participant ascends, so I additionally must implement the navigation procedurally.

Every chunk is a sq. of 192x192px, so first I create a sq. formed define (clockwise) that conforms all of the chunk:

    var new_2d_region_rid: RID = NavigationServer2D.region_create()
    var default_2d_map_rid: RID = get_world_2d().get_navigation_map()
    NavigationServer2D.region_set_map(new_2d_region_rid, default_2d_map_rid)

    var chunk_outline := PackedVector2Array([
        Vector2(0.0, current_chunk_row),
        Vector2(CHUNK_SIZE.x, current_chunk_row),
        Vector2(CHUNK_SIZE.x, current_chunk_row + CHUNK_SIZE.y),
        Vector2(0.0, current_chunk_row + CHUNK_SIZE.y),
    ])

Then, my intention is to clip or substract the platforms (obstacles) from that define. Platforms for the brand new chunk are saved in an Array[PackedVector2Array] clockwise:

    var ccw_outlines: Array[PackedVector2Array] = []
    for platform in platforms_of_new_chunk:
        var clipped_outlines := Geometry2D.clip_polygons(chunk_outline, platform)
        # save the holes for later
        for clipped_outline in clipped_outlines:
            # holes are CCW from Geometry2D.clip_polygons
            if not Geometry2D.is_polygon_clockwise(clipped_outline):
                ccw_outlines.push_back(clipped_outline)

Lastly I create the NavigationPolygon:

    var new_navigation_polygon := NavigationPolygon.new()
    new_navigation_polygon.add_outline(chunk_outline) # the massive one masking the entire chunk
    for define in ccw_outlines:
        define.reverse() # I reverse this to make them CW, b/c error msg says so
        new_navigation_polygon.add_outline(define)
    new_navigation_polygon.make_polygons_from_outlines()
    
    NavigationServer2D.region_set_navigation_polygon(new_2d_region_rid, new_navigation_polygon)

This provides me 2 error messages and clearly no platform avoiding navigation:

E 0:00:00:0962 sync: Navigation map synchronization error. Tried
to merge a navigation mesh polygon edge with one other already-merged
edge. That is normally brought on by crossing edges, overlapping polygons,
or a mismatch of the NavigationMesh / NavigationPolygon baked
‘cell_size’ and navigation map ‘cell_size’. <C++ Supply>
modules/navigation/nav_map.cpp:845 @ sync()

And (emphasis mine):

E 0:00:12:0220 degree.gd:102 @ generate_navigation_region():
NavigationPolygon: Convex partition failed! Didn’t convert outlines
to a sound NavigationMesh. NavigationPolygon outlines cannot overlap
vertices or edges inside similar define or with different outlines or have
any intersections. Add the outmost and largest define first. So as to add
holes inside this define add the smaller outlines with similar winding
order.
<C++ Supply> scene/sources/navigation_polygon.cpp:296 @
make_polygons_from_outlines() degree.gd:102 @
generate_navigation_region()
degree.gd:63 @ generate_chunks()
degree.gd:189 @ _on_area_2d_body_entered()

I feel I am doing it because the error suggests. What could possibly be fallacious? Or perhaps there’s one other option to obtain what I need.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here