navigation2D get_simple_path返回poolvector2Array大小:0

发布于 2025-02-04 19:21:11 字数 2710 浏览 2 评论 0原文

这是原始代码:

var navigation = Navigation2D.new()
for door_loc in all_door_coords:
    var start_door = door_loc
    var end_door = all_door_coords[randi() % all_door_coords.size()]
    if start_door == end_door:
        continue
    var path = navigation.get_simple_path(start_door, end_door, false)
    for coord in path:
        tilemap.set_cellv(coord, -1)

它的作用:从所有可能的坐标中选择2并在它们之间绘制NAV路径(获取路径的所有坐标)。然后在每个路径上坐标删除所在 /删除它的图块。依此类推,每个可能的坐标。

我使用生成的TILEMAP,所以我认为问题在于坐标(本地/全局)。下面我实现了坐标转换地图 - > 全球 - > local ,另一种是环绕的。

var navigation = Navigation2D.new()
for door_loc in all_door_coords:
    var start_door = to_local(tilemap.map_to_world(door_loc))
    var end_door = to_local(tilemap.map_to_world(all_door_coords[randi() % all_door_coords.size()]))
    if start_door == end_door:
        continue
    var path = navigation.get_simple_path(start_door, end_door, false)
    for coord in path:
        coord = tilemap.world_to_map(to_global(coord))
        tilemap.set_cellv(coord, -1)

问题在于,无论如何,我都会得到0尺寸路径坐标数组。我还为要用于此路径的图块中的每个瓷砖绘制了NAV多边形。 +我尝试在get_simple_path中切换优化,但仍然没有。

为什么不起作用?

编辑1 将转换更改为地图 - > local (谢谢@theraot)

var navigation = Navigation2D.new()
for door_loc in all_door_coords:
    var start_door = tilemap.map_to_world(door_loc)
    var end_door = tilemap.map_to_world(all_door_coords[randi() % all_door_coords.size()])
    if start_door == end_door:
        continue
    var path = navigation.get_simple_path(start_door, end_door, true)
    for coord in path:
        coord = tilemap.world_to_map(coord)
        tilemap.set_cellv(coord, -1)

他们最好将名称更改为map_to_to_locallocal_to_map。但是问题仍然存在。

编辑2 为生成循环中每个瓷砖的NAV Polygon创建代码:

var polygon = NavigationPolygon.new()
var outline = PoolVector2Array([Vector2(x*TILE_SIZE, y*TILE_SIZE), 
                                        Vector2(x*TILE_SIZE, y*TILE_SIZE+TILE_SIZE), 
                                        Vector2(x*TILE_SIZE+TILE_SIZE, y*TILE_SIZE+TILE_SIZE), 
                                        Vector2(x*TILE_SIZE+TILE_SIZE, y*TILE_SIZE)])
polygon.add_outline(outline)
polygon.make_polygons_from_outlines()
var polygon_instance = NavigationPolygonInstance.new()   #If I create an instance for each tile and then use it for .navpoly - nav polygons won't be created
$NavigationPolygonInstance.navpoly = polygon    #So I can only use preassigned node for it, that is why only one tile shows to have this polygon I suppose

Here's the original code:

var navigation = Navigation2D.new()
for door_loc in all_door_coords:
    var start_door = door_loc
    var end_door = all_door_coords[randi() % all_door_coords.size()]
    if start_door == end_door:
        continue
    var path = navigation.get_simple_path(start_door, end_door, false)
    for coord in path:
        tilemap.set_cellv(coord, -1)

What it does: from all the possible coordinates pick 2 and draw a nav path between them (get all the coordinates for the path). Then on each path coordinate remove the tile that is there / delete it. And so forth for each of the possible coordinates.

I use a generated tilemap, so I thought the problem is in the coordinates (local/global). Below I implemented coordinates conversion map -> global -> local and one for the other way around.

var navigation = Navigation2D.new()
for door_loc in all_door_coords:
    var start_door = to_local(tilemap.map_to_world(door_loc))
    var end_door = to_local(tilemap.map_to_world(all_door_coords[randi() % all_door_coords.size()]))
    if start_door == end_door:
        continue
    var path = navigation.get_simple_path(start_door, end_door, false)
    for coord in path:
        coord = tilemap.world_to_map(to_global(coord))
        tilemap.set_cellv(coord, -1)

The problem is that no matter what, I get a 0 size path coordinates array. I have also drawn nav polygons for each tile in the tileset I want to be used for this path. + I tried switching the optimization in get_simple_path, still nothing.

Why does it not work?

EDIT 1
Changed the conversion to map -> local (Thank you @Theraot)

var navigation = Navigation2D.new()
for door_loc in all_door_coords:
    var start_door = tilemap.map_to_world(door_loc)
    var end_door = tilemap.map_to_world(all_door_coords[randi() % all_door_coords.size()])
    if start_door == end_door:
        continue
    var path = navigation.get_simple_path(start_door, end_door, true)
    for coord in path:
        coord = tilemap.world_to_map(coord)
        tilemap.set_cellv(coord, -1)

They better change the names to map_to_local and local_to_map. But the problem is still there.

EDIT 2
Code for nav polygon creation for each tile in the generation loop:

var polygon = NavigationPolygon.new()
var outline = PoolVector2Array([Vector2(x*TILE_SIZE, y*TILE_SIZE), 
                                        Vector2(x*TILE_SIZE, y*TILE_SIZE+TILE_SIZE), 
                                        Vector2(x*TILE_SIZE+TILE_SIZE, y*TILE_SIZE+TILE_SIZE), 
                                        Vector2(x*TILE_SIZE+TILE_SIZE, y*TILE_SIZE)])
polygon.add_outline(outline)
polygon.make_polygons_from_outlines()
var polygon_instance = NavigationPolygonInstance.new()   #If I create an instance for each tile and then use it for .navpoly - nav polygons won't be created
$NavigationPolygonInstance.navpoly = polygon    #So I can only use preassigned node for it, that is why only one tile shows to have this polygon I suppose

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清音悠歌 2025-02-11 19:21:11

如果有人有这个问题(当您使用脚本生成世界并在其中添加nav2d的东西时),然后用get_simple_path - 使用yarks(get_tree(),“ idle_frame”)< /code>并将您的get_simple_path放入代码的另一个函数或块中,只需以某种方式将其与主代码分开。我只是这样做了,它运行得很好。我从此线程。

If anyone has this problem (when you generate world with a script and add Nav2D stuff to it), before calculating the path with get_simple_path - use yield(get_tree(), "idle_frame") AND put your get_simple_path into another function or block of code, just separate it from the main code somehow. I did just that and it works perfectly. I got the solution from this thread.

天荒地未老 2025-02-11 19:21:11
yield(get_tree(), "idle_frame")
path = navigation.get_simple_path(position, new_destination.position, false)
yield(get_tree(), "idle_frame")
path = navigation.get_simple_path(position, new_destination.position, false)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文