【问题标题】:A* Pathfinding in a 2D SidescrollerA* 2D Sidescroller 中的寻路
【发布时间】:2014-12-01 18:48:15
【问题描述】:

我目前正尝试在我的 2D 横向卷轴程序生成的世界游戏中使用 A* 寻路(想想类似泰拉瑞亚的地形)。

我一直在使用这个资源: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

我的问题是:在具有大型程序生成世界的 2D 横向滚动条中,我如何选择边界?到特定图块的路径可以是任意距离,显然遍历整个地图似乎是不明智的。

我正在努力有效地做到这一点,所以任何帮助都将不胜感激!

【问题讨论】:

    标签: unity3d 2d artificial-intelligence a-star procedural-generation


    【解决方案1】:

    我从来没有听说过它被称为“前沿”,它一直是“开放列表”,你把所有你可以去的节点都放在那里——邻居。

    我用过的性能方面的东西:

    1) 将计算放在另一个线程中:(不是协程)在这里查看ThreadedJob http://answers.unity3d.com/questions/357033/unity3d-and-c-coroutines-vs-threading.html 它会产生延迟 - 在你调用它之后结果将是几帧,要么:a)等待; b) 一旦结果准备好,开始朝着目标的大方向前进并改变路径; c) 转到路径的第一个节点,即使其余节点还没有准备好。

    2) 缓存结果:Dictionary&lt;Vector4, List&lt;Vector2&gt;&gt; pathCache;,其中键Vector4为startPosX,startPosY,finishPosX,finishPosY,值List是A*的结果。 因为路径是对称的,所以在寻找从 A 到 B 的路径时,还应该检查 B->A 的缓存并反转路径。

    【讨论】:

    • 我如何知道哪些节点应该放在开放列表中?仅仅是近邻吗?那么对于 2D 横向滚动条,它本质上是顶部、底部、左侧和右侧节点?
    猜你喜欢
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多