【问题标题】:navigation tilemaps without placing walkable tiles manually导航瓷砖地图,无需手动放置可步行瓷砖
【发布时间】:2019-05-10 03:27:21
【问题描述】:

我在 godot 中有一个瓷砖地图,但所有瓷砖都是障碍物并提供碰撞。只有没有任何瓷砖的单元格可以步行。我现在正在尝试通过 Navigation2D 节点添加导航。据我所知,没有办法告诉它“一切都可以步行,但不是这些瓷砖在哪里”(只能说“瓷砖的这一部分是可步行的”,但在我目前的设置中没有瓷砖步行空间)。

作为一种解决方法,我尝试将没有瓷砖的每个单元格设置为“虚拟瓷砖”,使用以下代码可以完全步行:

func _ready():
    for x in size.x:
        for y in size.y:
            var cell = get_cell(x, y)
            if cell == -1:
                set_cell(x, y, WALKABLE)

但 Navigation2D 节点无法识别这些图块。如果我手动放置 WALKABLE 磁贴,一切都会按预期进行。

我想我可能会打到this issue,需要致电update_dirty_quadrants(),但这并不能解决问题。

我在 3.0.6stable、3.1Alpha2 版本和最近提交的 9a8569d (provided by godotwild) 上进行了尝试,结果始终相同。

有没有办法在不事先手动放置每个图块的情况下使用 tilemaps 进行导航?

【问题讨论】:

  • 你是怎么克服这个问题的?你是不是到处都用瓷砖?
  • 我正在使用以帖子中概述的方式生成的每个“空图块”的节点进行自定义寻路。所以从某种意义上说,我根本没有解决这个问题。

标签: godot


【解决方案1】:

对于将来遇到此问题的任何人,“虚拟导航图块”解决方案现在可以使用(使用 Godot 3.2.3)。将此脚本放在相关的瓷砖地图上:

extends TileMap

# Empty/invisible tile marked as completely walkable. The ID of the tile should correspond
# to the order in which it was created in the tileset editor.
export(int) var _nav_tile_id := 0

func _ready() -> void:
    # Find the bounds of the tilemap (there is no 'size' property available)
    var bounds_min := Vector2.ZERO
    var bounds_max := Vector2.ZERO
    for pos in get_used_cells():
        if pos.x < bounds_min.x:
            bounds_min.x = int(pos.x)
        elif pos.x > bounds_max.x:
            bounds_max.x = int(pos.x)
        if pos.y < bounds_min.y:
            bounds_min.y = int(pos.y)
        elif pos.y > bounds_max.y:
            bounds_max.y = int(pos.y)
    
    # Replace all empty tiles with the provided navigation tile
    for x in range(bounds_min.x, bounds_max.x):
        for y in range(bounds_min.y, bounds_max.y):
            if get_cell(x, y) == -1:
                set_cell(x, y, _nav_tile_id)

    # Force the navigation mesh to update immediately
    update_dirty_quadrants()

【讨论】:

    【解决方案2】:

    我在 autotiler 中找不到我的 tile 的 ID(在编辑器中检查时,选择了 tilemap,这些都显示相同的 imagename.png 0)。因此,我没有使用 ID,而是使用 Tileset 中可行走的空白瓷砖的坐标。代码与 LukeZaz' 相同,但将 set_cell 替换为:

    set_cell(x, y, 0, false, false, false, Vector2(7,4));其中 Vector2(7,4) 是我的瓦片集中具有导航多边形的空白瓦片的坐标。 (记住坐标从 0 开始)

    (Godot 3.2.3.stable)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 2017-08-18
      • 2018-05-28
      相关资源
      最近更新 更多