我的 A* 寻路实现没有给出预期结果

发布于 2024-11-28 21:06:06 字数 1194 浏览 1 评论 0原文

我的 AS3 A* 寻路实现有时不会返回最有效的路线,就像这样:(

[E][X][ ][ ][ ]
[.][X][.][.][ ]
[ ][.][ ][i][S]

其中 . 是走过的节点,X 是墙壁。S = 开始,E = 结束,i = 我的假想标记)

问题:我的总分应该是(到终点的距离)30 +(到起点的距离)10 = 40,而我上面的图块的总分应该是(到终点的距离)40 +(到起点的距离)14 = 54。为什么选择 54 而不是 40,我不知道 - 我用它来查找开放列表中总分最低的节点:(

        var lowestTScore:int = 10000;
        var pointerTo:PathNode;
        for each (var Cur:PathNode in openList) {
            //loops through each node in openlist and finds the one with lowest total score.
            if (Cur.distS + Cur.distE < lowestTScore) {
                lowestTScore = Cur.distS + Cur.distE;
                pointerTo = Cur;
            }
        }

我看不到任何问题。)

我想,也许我计算到终点的距离是错误的。所以我检查了我的代码:(

theNode.distE = (Math.abs(theNode.xpos - endPts[0]) + Math.abs(theNode.ypos - endPts[1])) * 10;

我再次看不到任何问题。)

我真的对此感到困惑。

Main.as: http://pastebin.com/ZKQJwY4S PathSearcher:as: http://pastebin.com/KnmWGbQw

(我明白最好直接发布问题代码,但我不知道问题代码在哪里:(抱歉)

感谢您的帮助!

My AS3 A* pathfinding implementation sometimes doesn't returns the most efficient route, rather like this:

[E][X][ ][ ][ ]
[.][X][.][.][ ]
[ ][.][ ][i][S]

(where . is the node walked, and X is walls. S = start, E = end, i = my imaginary marker)

The problem: i should have a total score of (distance to end) 30 + (distance from start) 10 = 40, while the tile above i should have a total score of (distance to end) 40 + (distance from start) 14 = 54. Why is 54 being picked instead of 40, I don't know - I use this to find the node with lowest total score on the open list:

        var lowestTScore:int = 10000;
        var pointerTo:PathNode;
        for each (var Cur:PathNode in openList) {
            //loops through each node in openlist and finds the one with lowest total score.
            if (Cur.distS + Cur.distE < lowestTScore) {
                lowestTScore = Cur.distS + Cur.distE;
                pointerTo = Cur;
            }
        }

(which I can't see any problems with.)

I thought, maybe it's a error with me calculating distance to the end. So I checked my code for that:

theNode.distE = (Math.abs(theNode.xpos - endPts[0]) + Math.abs(theNode.ypos - endPts[1])) * 10;

(which, again I can't see any problems with.)

I'm really stumped on this.

Main.as: http://pastebin.com/ZKQJwY4S
PathSearcher:as: http://pastebin.com/KnmWGbQw

(I understand that it is better to post directly the problem code, but I don't know where is the problem code :( Sorry)

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人骨 2024-12-05 21:06:06

发现问题了! 没有添加

    theNode.distS = theNode.parentNode.distS + cost;

我在更改 theNode 的父母时 。我只改变了parentNode,但没有改变distS分数。

Found the problem! I didn't add

    theNode.distS = theNode.parentNode.distS + cost;

when changing parents of theNode. I only changed parentNode, but not the distS score.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文