我的 A* 寻路实现没有给出预期结果
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发现问题了! 没有添加
我在更改 theNode 的父母时 。我只改变了parentNode,但没有改变distS分数。
Found the problem! I didn't add
when changing parents of theNode. I only changed parentNode, but not the distS score.