作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在我的 2D 游戏中实现了 A* 寻路,它在有障碍物的普通 map 上运行良好。现在我正在尝试了解如何修改算法,以便将崎岖地形(山丘、森林等)计为 2 步而不是 1 步。
对于 1 移动成本,该算法在移动成本函数中使用整数 10 和 14。 我对如果一个单元格的实际移动成本为 2,如何修改这些值感兴趣?会是 20:17 吗?
这是我当前的算法当前计算 G 和 H 的方式 (adopted from Ray Wenderleich):
// Compute the H score from a position to another (from the current position to the final desired position
- (int)computeHScoreFromCoord:(CGPoint)fromCoord toCoord:(CGPoint)toCoord
{
// Here we use the Manhattan method, which calculates the total number of step moved horizontally and vertically to reach the
// final desired step from the current step, ignoring any obstacles that may be in the way
return abs(toCoord.x - fromCoord.x) + abs(toCoord.y - fromCoord.y);
}
// Compute the cost of moving from a step to an adjecent one
- (int)costToMoveFromStep:(ShortestPathStep *)fromStep toAdjacentStep:(ShortestPathStep *)toStep
{
return ((fromStep.position.x != toStep.position.x)
&& (fromStep.position.y != toStep.position.y))
? 14 : 10;
}
最佳答案
如果某些边的移动成本为 2,您只需将 2 添加到父节点的 G,而不是 1。
至于H:不用改。由此产生的启发式仍然是可接受的/一致的。
关于objective-c - A* 寻路 - 如何修改 G 和 H 以包含崎岖地形移动成本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20828839/
我是一名优秀的程序员,十分优秀!