godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
89.14k stars 20.2k forks source link

`NavigationServer2D.bake_from_source_geometry_data` handles obstructions improperly #85550

Closed spikeyarmaku closed 9 months ago

spikeyarmaku commented 9 months ago

Godot version

v4.2.stable.official [46dc27791]

System information

Godot v4.2.stable - Windows 10.0.19045 - Vulkan (Mobile) - dedicated Radeon RX Vega (Advanced Micro Devices, Inc.; 31.0.21905.1001) - AMD Ryzen 5 3600 6-Core Processor (12 Threads)

Issue description

I want to create a navmesh from code. The setup is this:

Both rectangles have the same winding.

Expected result: an L-shaped traversable area Actual result: an empty navmesh

This is the code that is responsible for creating the navmesh:

func _ready():
    var rect1 = [Vector2(0, 0), Vector2(100, 0), Vector2(100, 100), Vector2(0, 100)]
    var rect2 = [Vector2(-20, -20), Vector2(80, -20), Vector2(80, 80), Vector2(-20, 80)]
    var src = NavigationMeshSourceGeometryData2D.new()
    var navpoly = NavigationPolygon.new()

    src.add_traversable_outline(rect1)
    src.add_obstruction_outline(rect2)
    NavigationServer2D.bake_from_source_geometry_data(navpoly, src)

    $NavigationRegion2D.navigation_polygon = navpoly

Steps to reproduce

There is a Line2D surrounding the area where the navmesh should appear.

Minimal reproduction project

polygon2d_bug.zip

smix8 commented 9 months ago

The NavigationPolygon has by default an agent_radius of 10 pixel.

In the test project the available surface is too small so there is no navigation mesh surface left after the offset is done.

So either increase the available, traversable surface or reduce the agent radius on the NavigationPolygon.

spikeyarmaku commented 9 months ago

I rewrote the code by multiplying every coordinate by 10, but the result is the same. For convenience's sake, here is the code I tried:

func _ready():
    var rect1 = [Vector2(0, 0), Vector2(1000, 0), Vector2(1000, 1000), Vector2(0, 1000)]
    var rect2 = [Vector2(-200, -200), Vector2(800, -200), Vector2(800, 800), Vector2(-200, 800)]
    var src = NavigationMeshSourceGeometryData2D.new()
    var navpoly = NavigationPolygon.new()

    src.add_traversable_outline(rect1)
    src.add_obstruction_outline(rect2)
    NavigationServer2D.bake_from_source_geometry_data(navpoly, src)

    $NavigationRegion2D.navigation_polygon = navpoly

I also tried setting the agent radius to 1, but that also didn't change anything.

smix8 commented 9 months ago

Bakes just fine but with that size you really need to zoom out to see the navigation mesh in the bottom right corner.

navmesh

spikeyarmaku commented 9 months ago

You're right. Turns out I forgot to switch "Visible navigations" on when I restarted Godot.

smix8 commented 9 months ago

Turns out I forgot to switch "Visible navigations" on when I restarted Godot.

Happens to the best of us.

smix8 commented 9 months ago

Closing as issue seems to be solved, feel free to reply if it isn't.