gpt4 book ai didi

objective-c - A* 寻路 - 如何修改 G 和 H 以包含崎岖地形移动成本?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:24:26 26 4
gpt4 key购买 nike

我在我的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com