2

这是我的节点网格:

替代文字

我正在使用 A* 寻路算法在其上移动一个对象。它通常可以正常工作,但有时会出现错误:

  • 从 3 移动到 1 时,它正确地通过 2。但是,当从 1 移动到 3 时,它通过 4。
  • 在 3 和 5 之间移动时,它会在任一方向上通过 4,而不是通过 6 的较短路径

有什么问题?这是我的代码(AS3):

    public static function getPath(from:Point, to:Point, grid:NodeGrid):PointLine {

        // get target node
        var target:NodeGridNode = grid.getClosestNodeObj(to.x, to.y);

        var backtrace:Map = new Map();
        var openList:LinkedSet = new LinkedSet();
        var closedList:LinkedSet = new LinkedSet();

        // begin with first node
        openList.add(grid.getClosestNodeObj(from.x, from.y));

        // start A*
        var curNode:NodeGridNode;
        while (openList.size != 0) {

            // pick a new current node
            if (openList.size == 1) {
                curNode = NodeGridNode(openList.first);
            }
            else {
                // find cheapest node in open list
                var minScore:Number = Number.MAX_VALUE;
                var minNext:NodeGridNode;
                openList.iterate(function(next:NodeGridNode, i:int):int {
                    var score:Number = curNode.distanceTo(next) + next.distanceTo(target);
                    if (score < minScore) {
                        minScore = score;
                        minNext = next;
                        return LinkedSet.BREAK;
                    }
                    return 0;
                });
                curNode = minNext;
            }

            // have not reached
            if (curNode == target) break;
            else {
                // move to closed
                openList.remove(curNode);
                closedList.add(curNode);

                // put connected nodes on open list
                for each (var adjNode:NodeGridNode in curNode.connects) {
                    if (!openList.contains(adjNode) && !closedList.contains(adjNode)) {
                        openList.add(adjNode);
                        backtrace.put(adjNode, curNode);
                    }
                }
            }
        }

        // make path
        var pathPoints:Vector.<Point> = new Vector.<Point>();
        pathPoints.push(to);
        while(curNode != null) {
            pathPoints.unshift(curNode.location);
            curNode = backtrace.read(curNode);
        }
        pathPoints.unshift(from);
        return new PointLine(pathPoints);

    }

NodeGridNode::distanceTo()

    public function distanceTo(o:NodeGridNode):Number {
        var dx:Number = location.x - o.location.x;
        var dy:Number = location.y - o.location.y;
        return Math.sqrt(dx*dx + dy*dy);
    }
4

2 回答 2

1

我在这里看到的问题是这条线

if (!openList.contains(adjNode) && !closedList.contains(adjNode))

可能的情况是,adjNode 可能更容易(更短)通过当前节点到达,尽管它之前是从另一个节点到达的,这意味着它在 openList 中。

于 2010-05-19T09:29:04.397 回答
0

发现错误:

                openList.iterate(function(next:NodeGridNode, i:int):int {
                    var score:Number = curNode.distanceTo(next) + next.distanceTo(target);
                    if (score < minScore) {
                        minScore = score;
                        minNext = next;
                        return LinkedSet.BREAK;
                    }
                    return 0;
                });

(其return LinkedSet.BREAK作用类似于常规循环中的 break 语句)不应该存在。它会导致始终选择打开列表中的第一个节点,而不是最便宜的节点。

于 2010-05-19T09:32:03.697 回答