0

Can I make the builtin AStar choose the shortest path with the least direction changes?

I currently build my graph like so:

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)

It seems like you can only weight points, not edges, so I'm not sure how to prefer one direction over another. The path it chooses always seems to maximize direction changes:

jagged path

rcorre
  • 6,477
  • 3
  • 28
  • 33

1 Answers1

0

When you see 3 Nodes, if the direction is changed, then increase the F value of last Node.

Three Nodes will tell you that the direction changes.

Min
  • 1
  • 1