1、A星寻路入门双语版Astar Pathfinding for BeginnersA* Pathfinding for BeginnersThis article has been translated into Spanish and French. Other translations are welcome.While it is easy once you get the hang of it, the A* (pronounced A-star) algorithm can be complicated for beginners. There are plenty of articl
2、es on the web that explain A*, but most are written for people who understand the basics already. This one is for the true beginner.虽然掌握了A*(读作A-star)算法就认为它很容易,对于初学者来说,它却是复杂的。网上有很多解释A*的文章,不过大多数是写给理解了基础知识的人。本文是给初学者的。This article does not try to be the definitive work on the subject. Instead it describ
3、es the fundamentals and prepares you to go out and read all of those other materials and understand what they are talking about. Links to some of the best are provided at the end of this article, under Further Reading.本文并不想成为关于这个主题的权威论文。实际上它讨论了基础知识并为你做一些准备,以便进一步阅读其他资料和理解它们讨论的内容。本文的后面列出了几个最好的文章,在进阶阅读
4、中。Finally, this article is not program-specific. You should be able to adapt whats here to any computer language. As you might expect, however, I have included a link to a sample program at the end of this article. The package contains two versions: one in C+ and one in Blitz Basic. It also contains
5、 executables if you just want to see A* in action.最后,本文不是编程规范的。你应该能够改写这里的东西到任何计算机语言上。如你所期望的,同时,我包含了一个示例程序的链接,在本文后面结束的地方。这个程序包有两个版本:一个是C+,另一个用Blitz Basic语言编写。如果你只是想看看A*的行为,里面也含有可执行exe文件。But we are getting ahead of ourselves. Lets start at the beginning .但我们要超越自己。让我们从头开始 .介绍:搜索区域Introduction: The Sear
6、ch AreaLets assume we have someone who wants to get from point A to point B and that a wall separates the two points. This is illustrated in the graphic found below, with green being the starting point A, red being the ending point B, and the blue filled squares being the wall in between.我们假设某人想从A点到
7、达B点,一堵墙把它们分开了。如下图所示,绿色是开始点A,红色是结束点B,而蓝色填充的方块是中间的墙。图 1Figure 1The first thing you should notice is that we have divided our search area into a square grid. Simplifying the search area, as we have done here, is the first step in pathfinding. This particular method reduces our search area to a simple t
8、wo dimensional array. Each item in the array represents one of the squares on the grid, and its status is recorded as walkable or unwalkable. The path is found by figuring out which squares we should take to get from A to B. Once the path is found, our person moves from the center of one square to t
9、he center of the next until the target is reached.你应该注意的第一件事是,我们把搜索区域分割成了方块的格子。简化搜索区域,如你目前完成的那样,这是寻路的第一步。这个特殊方法把搜索区域简化成了一个二维数组。数组的每一个项目代表了格子里的一个方块,它的状态记录成可行走和不可行走。通过计算出从A到达B应该走哪些方块,就找到了路径。一旦路径找到,我们的人从一个方块的中心移动到下一个方块的中心,直到抵达目标。These center points are called nodes. When you read about pathfinding else
10、where, you will often see people discussing nodes. Why not just refer to them as squares? Because it is possible to divide up your pathfinding area into something other than squares. They could be rectangular, hexagons, or any shape, really. And the nodes could be placed anywhere within the shapes ?
11、 in the center or along the edges, or anywhere else. We are using this system, however, because it is the simplest.这些中心点称作“节点”。当你在其它地方阅读关于寻路时,你将经常发现人们讨论节点。为什么不直接把它们认为是方块呢?因为有可能你要把你的寻路区域以非方块的东西来分割。它们可能是矩形,六角形,或任何形状,真的。而节点可以放到形状内的任何位置。在中心,或者沿着边缘,或其它地方。然而我们使用这个系统,因为它最简单。开始搜索Starting the SearchOnce we h
12、ave simplified our search area into a manageable number of nodes, as we have done with the grid layout above, the next step is to conduct a search to find the shortest path. In A* pathfinding, we do this by starting at point A, checking the adjacent squares, and generally searching outward until we
13、find our target.一旦我们把搜索区域简化成了可以管理的大量节点,就象我们上面所做的那样采用格子的布局,下一步就是引导一个搜索来找出最短路径。在A*寻路的做法,我们从开始点A做起,检查它周围的方块,并且向外普通的搜索,直到找到目标。We begin the search by doing the following:我们这样开始搜索:1. Begin at the starting point A and add it to an open list of squares to be considered. The open list is kind of like a shopp
14、ing list. Right now there is just one item on the list, but we will have more later. It contains squares that might fall along the path you want to take, but maybe not. Basically, this is a list of squares that need to be checked out. 2. 从开始点A起,添加它到待考虑的方块的“开放列表”。开放列表有点象购物列表。此时只有一个项目在里面,但很快我们会得到更多。它包
15、含了你可能取用的沿途的方块,也可能不用它。基本上,这是需要检查的方块的列表。3. Look at all the reachable or walkable squares adjacent to the starting point, ignoring squares with walls, water, or other illegal terrain. Add them to the open list, too. For each of these squares, save point A as its parent square. This parent square stuff
16、is important when we want to trace our path. It will be explained more later. 4. 观察开始点邻近的所有可到达或可行走的方块,忽略有墙,水或其他非法地形的方块。也把它们添加到开放列表。对每一个方块,保存A 点作为它们的“父亲”。这个父亲方块在跟踪路径时非常重要。后面会更多的解释。5. Drop the starting square A from your open list, and add it to a closed list of squares that you dont need to look at a
17、gain for now. 6. 把开始方块A从开放列表中取出,并放到“封闭列表”内,它是所有现在不需要再关注的方块的列表。At this point, you should have something like the following illustration. In this diagram, the dark green square in the center is your starting square. It is outlined in light blue to indicate that the square has been added to the closed
18、list. All of the adjacent squares are now on the open list of squares to be checked, and they are outlined in light green. Each has a gray pointer that points back to its parent, which is the starting square.在此,你应该有了类似下图的东西。在这个图中,中间的深绿色的方块就是开始方块。它有浅蓝色的外框,表示它被添加到封闭列表了。所有的相邻方块现在都进入要检查的方块的开放列表中了,它们有浅绿的
19、外框。每一个都有灰色的指针指回它的父亲,它就是开始方块。图 2Figure 2Next, we choose one of the adjacent squares on the open list and more or less repeat the earlier process, as described below. But which square do we choose? The one with the lowest F cost.下一步,我们从开放列表中,选出一个相邻的方块,然后多多少少重复早先的过程,下面会说到。但是我们选择哪一个呢?具有最小F值的那个。路径排序Path
20、ScoringThe key to determining which squares to use when figuring out the path is the following equation:找到形成路径的方块的关键是下面的等式:F = G + Hwhere这里 G = the movement cost to move from the starting point A to a given square on the grid, following the path generated to get there. G = 从开始 点A到格子中给定方块的移动代价,沿着到达该方
21、块而生成的那个路径。 H = the estimated movement cost to move from that given square on the grid to the final destination, point B. This is often referred to as the heuristic, which can be a bit confusing. The reason why it is called that is because it is a guess. We really dont know the actual distance until
22、we find the path, because all kinds of stuff can be in the way (walls, water, etc.). You are given one way to calculate H in this tutorial, but there are many others that you can find in other articles on the web. H = 从格子中给定 的方块到最终目标 B点的评估移动代价。这种方式通常称作试探法,有点让人混乱。因为这是一个猜测,所以得到这个称谓。在找到路径之前,我们真的不知道实际的距
23、离,因为途中有各种东西(墙,水,等等)。在本教程里给出了一种计算H的方法,但在网上你能找到很多其他的文章。 Our path is generated by repeatedly going through our open list and choosing the square with the lowest F score. This process will be described in more detail a bit further in the article. First lets look more closely at how we calculate the equa
24、tion.我们需要的路径是这样生成的:反复的遍历开放列表,选择具有最小F值的方块。这个过程在本文稍后会详细描述。先让我们看看如何计算前面提到的等式。As described above, G is the movement cost to move from the starting point to the given square using the path generated to get there. In this example, we will assign a cost of 10 to each horizontal or vertical square moved, an
25、d a cost of 14 for a diagonal move. We use these numbers because the actual distance to move diagonally is the square root of 2 (dont be scared), or roughly 1.414 times the cost of moving horizontally or vertically. We use 10 and 14 for simplicitys sake. The ratio is about right, and we avoid having
26、 to calculate square roots and we avoid decimals. This isnt just because we are dumb and dont like math. Using whole numbers like these is a lot faster for the computer, too. As you will soon find out, pathfinding can be very slow if you dont use short cuts like these.如上所述,G是经由到达它的路径,从开始点到给定方块的移动代价。
27、在本例中,我们为每个水平/垂直的移动指定代价为10,而斜角的移动代价为14。我们使用这些值,因为斜角移动的实际距离是2的平方根(别害怕),或者大概1.414倍的水平/垂直的移动代价。出于简化的目的使用了10和14。比例大致是正确的,而我们却避免了方根和小数的计算。倒不是我们没有能力做或者不喜欢数学。使用这些数字也能让计算更快一些。以后你就会发现,如果不使用这些技巧,寻路的计算非常慢。Since we are calculating the G cost along a specific path to a given square, the way to figure out the G co
28、st of that square is to take the G cost of its parent, and then add 10 or 14 depending on whether it is diagonal or orthogonal (non-diagonal) from that parent square. The need for this method will become apparent a little further on in this example, as we get more than one square away from the start
29、ing square.既然我们沿着到达给定方块的路径来计算G的值,找出那个方块的G值的方法就是找到其父亲的G值,再加上10或者14而得,这依赖于他处于其父亲的斜角或者直角(非斜角)而定。这在本例后面会更加清晰,随着我们从开始点离开而得到更多的方块。H can be estimated in a variety of ways. The method we use here is called the Manhattan method, where you calculate the total number of squares moved horizontally and verticall
30、y to reach the target square from the current square, ignoring diagonal movement. We then multiply the total by 10. This is called the Manhattan method because its like calculating the number of city blocks from one place to another, where you cant cut across the block diagonally. Importantly, when
31、calculating H, we ignore any intervening obstacles. This is an estimate of the remaining distance, not the actual distance, which is why its called the heuristic. Want to know more? You can find equations and additional notes on heuristics here.H能通过多种方法估算。我们这里用到的方法叫做Manhattan方法,计算从当前方块经过水平/垂直移动而到达目标
32、方块的方块总数。然后将总数乘以 10。这种方法之所以叫做Manhattan方法,因为他很象计算从一个地点到达另一个地点的城市街区数量计算,此时你不能斜向的穿越街区。重要的是,当计算H的时候,要忽略任何路径中的障碍。这是一个对剩余距离的 估算值,而不是实际值,这就是试探法的称谓由来。想知道更多?关于试探法的更多说明在这里。F is calculated by adding G and H. The results of the first step in our search can be seen in the illustration below. The F, G, and H scores are written in each square. As is indicated in the square to the immediate right of the starting square, F is printed in the top left, G is printed in the bottom left, and H is printed in the bottom right.G和H相加就算出了F。第一步搜索的结果见下图的描述。F,G
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1