我目前正试图在我的2D-sidescroller生成的世界游戏中使用A*路径查找(想想Terraria样的地形)。
我一直在使用这个资源:http://www.redblobgames.com/pathfinding/a-star/introduction.html
并主要使用了以下伪码:
frontier = PriorityQueue()
frontier.put(start, 0)
came_from = {}
cost_so_far = {}
came_from[start] = None
cost_so_far[start] = 0
while not frontier.empty():
current = frontier.get()
if current == goal:
break
for next in graph.neighbors(current):
new_cost = cost_so_far[current] + graph.cost(current, next)
if next not in cost_so_far or new_cost < cost_so_far[next]:
cost_so_far[next] = new_cost
priority = new_cost + heuristic(goal, next)
frontier.put(next, priority)
came_from[next] = current
我的问题是:在一个程序生成的大世界中,我如何选择边疆?到达特定瓷砖的路径可能在任何距离之外,显然遍历整个地图似乎不明智。
我正试着高效地做这件事,所以任何的帮助都会很感激!
发布于 2014-12-02 07:29:51
我从来没有听说过它被称为“前沿”,它一直是“开放列表”,你把所有你可以去的节点--邻居。
我用过的性能方面的东西:
1)将计算放到另一个线程中:(而不是共同例程)检查ThreadedJob
这里--它会造成滞后--结果将是调用它之后的几个帧,或者:(A)等待;b)在结果准备好后,开始沿着目标的总体方向和更改路径;( c)到路径的第一个节点,即使其他节点还没有准备好。
2)缓存结果:Dictionary<Vector4, List<Vector2>> pathCache;
其中key,Vector4,is startPosX
,startPosY
,finishPosX
,finishPosY
,值列表是A*的结果。因为路径是对称的,所以当您寻找从A到B的路径时,也应该检查缓存中的B->A并反转路径。
https://stackoverflow.com/questions/27235104
复制相似问题