如何阻止AStar改变方向

时间:2019-02-19 12:40:49

标签: path-finding a-star game-development godot gdscript

我可以使内置的AStar选择具有最小方向更改的最短路径吗?

我目前以如下方式建立图形:

extends GridMap

var _astar = AStar.new()

func _ready():
    var id = 0
    for c in get_used_cells():
        var weight = 1.0
        if _get_cover(c.x, c.y, c.z):
            weight = 9999.0 # impassable tile
        _astar.add_point(id, Vector3(c.x, c.y, c.z), weight)
        id += 1

    for c in get_used_cells():
        var center = _astar.get_closest_point(Vector3(c.x, c.y, c.z))
        var above = _astar.get_closest_point(Vector3(c.x, c.y, c.z + 1))
        var right = _astar.get_closest_point(Vector3(c.x + 1, c.y, c.z))
        assert(id > 0)
        if above >= 0:
            _astar.connect_points(center, above, true)
        if right >= 0:
            _astar.connect_points(center, right, true)

似乎您只能加权点,而不是边缘,所以我不确定如何偏向一个方向。 它选择的路径似乎总是使方向变化最大化:

jagged path

1 个答案:

答案 0 :(得分:0)

当您看到3个节点时,如果方向改变了,则增加最后一个节点的F值

Three Nodes will tell you that the direction changes.