您的位置:首页 > 其它

两层A *路径搜索之草译

2015-06-18 00:00 363 查看
摘要: Patrick Lester's Two-Tiered A* Pathfinding

两层A *路径搜索

Two-Tiered A* Pathfinding

作者:帕特里克·莱斯特(更新2003年1月9日)

By Patrick Lester ( Updated January 9, 2003)

在我的主打文章A *路径搜索入门(http://my.oschina.net/u/660460/blog/467603)里,我非常笼统得描述了A*,并描述了如何创建一个单一的通用路径搜索功能。创建只有一个路径搜索功能,但是,可以是不必要的限制。

In my main article, A* Pathfinding for Beginners, I described A* in very general terms, and described how to create a single all-purpose pathfinding function. Creating only one pathfinding function, however, can be needlessly limiting.

考虑下面的RPG情况,剑客想找到绕过附近的墙的路:

Consider the following RPG situation, and a swordsman who wants to pathfind around a nearby wall:



鉴于这种地图,可以以各种方式和使用各种密度放置节点。在这个例子中,让我们使用了高密度节点网络,如下所示。

Given this kind of map, you could place nodes in a variety of ways, and use a variety of densities. In this example, let's use a high-density node network, as is shown below.



在此图中,有白点的能走的。没有点的地儿不能走。在这个例子中我们做一些有点不同的事情。在这个例子中,你被允许“抄近路”绕不能走方格。另外,“方格”本身不是方形了。因为这是一个等距例子,我们已决定了Y(垂直)轴有X(水平)轴两倍多节点。这允许适当的等距路径。

In this graphic, the white nodes are walkable. Spots where there are no nodes are unwalkable. We also do some things a little differently in this example. In this example you are allowed to "cut corners" around unwalkable squares. Also the "squares" themselves aren't square anymore. Because this is an isometric example, we have decided to pack twice as many nodes on the Y (vertical axis) as there are on the X (horizontal) axis. This allows proper isometric paths.

正如你所看到的,使用这种紧凑节点的网络,我们可以不仅在附近的墙上找到路径,但也墙上和附近的桶的过程之间。我们的密集节点的网络路径搜索也可以让周围的站立的火炬,其中在最基础的节点是不能走。这,总之,是精密路径搜索。

As you can see, using this tightly packed node network, we can pathfind not only around the nearby wall but also between the wall and the nearby barrel in the process. Our dense node network also allows pathfinding around the standing torch, where the node at the very base is unwalkable. This, in sum, is precision pathfinding.

嗯,这是在短距离的情况下很酷,但我们需要在整个地图的路径搜索?用密密麻麻的网络节点像这样很容易让你用A *循环搜索通过只是一个单一的路径超过10,000以上个节点。这会带来几乎任何PC嘎然而止。

Well, that is pretty cool in short-distance situations, but what do we do if we need to pathfind across the entire map? Using a densely packed node network like this could easily leave you searching through 10,000+ nodes on just a single pass through the A* loop. That will bring just about any PC to a grinding halt.

所以,让我们来看看另一种选择。如果我们选择创建一个更密集的节点的网络,就像一个你见下文?

So let's look at an alternative. What if we chose to create a much less dense node network, like the one you see below?



在这个例子中,节点是在的大等距菱形的中心。关于菱形的能走的数据存储在沿边缘相同的数组,并表示在这个图形与红色的小圆点。从一个等距瓦移动到其相邻的8相邻的砖块之一,相邻砖块本身必须是能走的,之间的路径不能被阻止。

In this example, the nodes are in the center of the large isometric diamonds. Data about walkability between the diamonds is stored in the same array along the edges, and is represented in this graphic with small red dots. To move from one isometric tile to one of its 8 adjacent macro-tiles, the adjacent tile must itself be walkable, and the path between must not be blocked.

此节点网络是比较早的一个较不密集的72倍。相反,有多达10000节点处理,在任何给定时间,在这里我们将只能与1/72的,或小于200。我们的计算机处理肯定可以搞定。整个地图路径搜索不再是一个问题。

This node network is 72 times less dense than the earlier one. Instead of dealing with as many as 10,000+ nodes at any given time, here we will only be dealing with 1/72 of that, or less than 200. Our computer can definitely handle that. Pathfinding across the map is no longer a problem.

把两者结合起来

Putting the Two Together

所以,我们选择哪种方法?两个都!

So, which method do we choose? Both!

在任何给定的搜索,你会先pathfind整个地图,用宏观层面探路。这样,你会切换到微探路者和搜索从当前位置的路径节点两种宏运节点走的道路。一旦你进入每一个新的宏观层面的菱形“方格”,你会用微观层面来探路,以pathfind宏节点两种宏运节点提前。这结束了浪费每个微通道的下半年,但你需要这样做,以确保它看起来不错 - 这是没有太大的浪费,因为你是跨越一个相对短的距离微路径搜索。一旦你获得2-3宏节点远离目标,你会完全切换到微探路者,并pathfind到最终位置。

On any given search, you would first pathfind across the map, using the macro-level pathfinder. You would then switch to the micro-pathfinder and search for a path from the current location to the node two macro-nodes away on the path. Once you entered each new macro-level diamond "square," you would use the micro-level pathfinder to pathfind to the macro-node two macro nodes ahead. This ends up wasting the second half of each micro-path, but you need to do this to make sure it looks good -- and it isn't much of a waste, since you are micro-pathfinding across a relatively short distance. Once you get 2-3 macro nodes away from the target, you would switch over completely to the micro-pathfinder, and pathfind to the final location.

使用这种方法,你会得到快速路径搜索整个地图,能够进行谈判见微知著,桶等,以现实的方式,因为在早期的微路径搜索的例子。很酷,呵呵!

Using this approach, you will get speed pathfinding across the entire map and be able to negotiate around corners, barrels, etc. in a realistic way, as in the earlier micro-pathfinding example. Pretty cool, huh!

如果你是*真的*雄心勃勃,你可以开发3探路者,如果你有一个非常大的地图,一个在一个真正宏观层面,一是中等水平,以及一个微观层面。像帝国时代游戏那样的专业人士做到了这一点。这只是取决于你的需求是什么。

And if you are *really* ambitious, you could develop three pathfinders if you have a really big map, one at a really macro level, one medium level, and one micro-level. The professionals have done this for games like Age of Empires. It just depends on what your needs are.

嗯,就是这样。与往常一样,联系到我:

Well, that's it. As always, I can be reached at:

pwlester@policyalmanac.org

在此之前,祝你好运!

Until then, good luck!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  A* Astar A星 算法