如何阻止AStar改变方向

问题描述 投票:0回答:1

我可以让内置的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

path-finding a-star game-development godot gdscript
1个回答
0
投票

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

Three Nodes will tell you that the direction changes.

© www.soinside.com 2019 - 2024. All rights reserved.