再譯《A *路徑搜索入門》之五

施上的注意事php

Notes on Implementationnode

 

在您瞭解了基本的方法,當你寫本身的程序,有一些外的事情要考。下面給出C ++Blitz Basic寫的程序,其餘有效算法

Now that you understand the basic method, here are some additional things to think about when you are writing your own program. Some of the following materials reference the program I wrote in C++ and Blitz Basic, but the points are equally valid in other languages.安全

 

1.其餘防止碰撞):若是你碰巧仔看個人演示,你會注意到它徹底忽略了屏幕上的其餘該單直接通過對方。根據遊多是能夠的,或者它可能是不能夠的。若是你想考其餘路徑搜索算法,並彼此走,另外一個,我建你只考要麼在路徑的算,理它的當前位置中止或相路徑搜索單元。於正在相元,防止碰撞能夠通過懲罰沿着各自的路徑點,從而鼓勵路徑搜索找到一個替代徑(2描述)。app

1. Other Units (collision avoidance): If you happen to look closely at my example code, you will notice that it completely ignores other units on the screen. The units pass right through each other. Depending on the game, this may be acceptable or it may not. If you want to consider other units in the pathfinding algorithm and have them move around one another, I suggest that you only consider units that are either stopped or adjacent to the pathfinding unit at the time the path is calculated, treating their current locations as unwalkable. For adjacent units that are moving, you can discourage collisions by penalizing nodes that lie along their respective paths, thereby encouraging the pathfinding unit to find an alternate route (described more under #2).less

 

若是你選擇認爲是移和不相其餘,你將須要開一種方法來預測會在任何定的時間點,使他獲得適當迴避。否,你最可能會用異的路徑,其中鋸齒狀,以免其餘,不存在了。ide

If you choose to consider other units that are moving and not adjacent to the pathfinding unit, you will need to develop a method for predicting where they will be at any given point in time so that they can be dodged properly. Otherwise you will probably end up with strange paths where units zig-zag to avoid other units that aren't there anymore.oop

 

固然,須要開一些碰撞檢測,因再好的路徑是在它的算方法,事情可能隨時間而改。當生碰撞位必要麼算新的路徑,或者若是其餘元被移,它不是一個迎面碰撞,等待其餘繼續與當前路徑以前到步驟閃開。佈局

You will also, of course, need to develop some collision detection code because no matter how good the path is at the time it is calculated, things can change over time. When a collision occurs a unit must either calculate a new path or, if the other unit is moving and it is not a head-on collision, wait for the other unit to step out of the way before proceeding with the current path.ui

 

些技巧多是足以你開始。若是您想了解更多,裏有一些你可能會發現有用接:

These tips are probably enough to get you started. If you want to learn more, here are some links that you might find helpful:

 

角色的向行

http://www.red3d.com/cwr/steer/

克雷格雷的方向上的工做是從路徑搜索有點不一樣,但它能夠與路徑搜索集成,以作出更完整的移和防撞系

Steering Behavior for Autonomous Characters: Craig Reynold's work on steering is a bit different from pathfinding, but it can be integrated with pathfinding to make a more complete movement and collision avoidance system.

 

計算機遊戲中的長轉向

http://ducati.doc.ntu.ac.uk/uksim/uksim%2704/Papers/Simon%20Tomlinson-%2004-20/paper04-20%20CR.pdf

向和路徑搜索文學的一個有趣的調查是一個PDF文件。

The Long and Short of Steering in Computer Games: An interesting survey of the literature on steering and pathfinding. This is a pdf file.

 

協調運行:

http://www.gamasutra.com/features/game_design/19990122/movement_01.htm

由帝國代的設計師戴夫·乍首先在兩部分成的系列造成和文章基於的運

Coordinated Unit Movement: First in a two-part series of articles on formation and group-based movement by Age of Empires designer Dave Pottinger.

 

實現協調

http://www.gamasutra.com/view/feature/3314/implementing_coordinated_movement.php

戴夫·乍的兩部分成的系列第二。

Implementing Coordinated Movement: Second in Dave Pottinger's two-part series.

 

2.的地造成本:在本教程中,個人程序隨行,地形僅僅是兩件事情之一走和不走。可是,若是你有地形就是走,但在更高的移動成本?沼,丘陵,地下城的樓梯,等等 - 都是地形是適合步行的全部例子,但在成本要比平坦,開地高。似地,道路可能具備低的運行成本比周的地形。

2. Variable Terrain Cost: In this tutorial and my accompanying program, terrain is just one of two things – walkable or unwalkable. But what if you have terrain that is walkable, but at a higher movement cost? Swamps, hills, stairs in a dungeon, etc. – these are all examples of terrain that is walkable, but at a higher cost than flat, open ground. Similarly, a road might have a lower movement cost than the surrounding terrain.

 

問題很容易加入,當你算任何點的G在地造成本理。只是金的成本添加到這樣點。在A *路徑搜索算法已寫入找最低成本路徑,應該很容易地問題。在簡單的例子中,我描述的,當地只是走和不走,A *找最短,最直接的途徑。可是,在可成本的地形境,以最少的成本路徑可能行走了較長的距離 - 就像把周的沼道路,而不是直接通它。

This problem is easily handled by adding the terrain cost in when you are calculating the G cost of any given node. Simply add a bonus cost to such nodes. The A* pathfinding algorithm is already written to find the lowest cost path and should handle this easily. In the simple example I described, when terrain is only walkable or unwalkable, A* will look for the shortest, most direct path. But in a variable-cost terrain environment, the least cost path might involve traveling a longer distance – like taking a road around a swamp rather than plowing straight through it.

 

一個有趣的附加考是什麼專業人士稱之「影響映射。」正如上述的可成本的地形,你能夠建一個外的分系,並將其用到AI的路徑。想一下,你有一堆山區保護位地。每當算機通通某人送的路徑上,它被重。若是你願意,你能夠建一個影響地處罰節點,其中大量的屠正在生。會教電腦偏好安全的路徑,並幫助它避免愚蠢的狀況下,它不斷通特定的路徑出兵,只是因它是短(但更危)。

An interesting additional consideration is something the professionals call "influence mapping." Just as with the variable terrain costs described above, you could create an additional point system and apply it to paths for AI purposes. Imagine that you have a map with a bunch of units defending a pass through a mountain region. Every time the computer sends somebody on a path through that pass, it gets whacked. If you wanted, you could create an influence map that penalized nodes where lots of carnage is taking place. This would teach the computer to favor safer paths, and help it avoid dumb situations where it keeps sending troops through a particular path, just because it is shorter (but also more dangerous).

 

另外一種可能的用途是懲罰沿着附近的移臺的路徑點。之一的A *的缺點之一是,當一元的全部嘗試找到的似位置的路徑,一般有一個著程度的重疊,如一個或多個試圖利用相同或似的路由到它的目的地。添加一個點球已被其餘位聲稱「點將有助於確保必定程度的分離,並減小衝突。不要把這樣點做走,可是因願意多臺設備才能夠擠過緊通道魚貫,若是有必要的。此外,你應該處罰,附近的路徑搜索單元,不是全部路徑的路徑,否你會獲得奇怪的避行,避免元的元,都不及他的路徑。此外,你應該懲罰謊言沿着一條路徑,而不是已經訪問過並留下之前的路徑點的當前和將來的部分道路點。

Yet another possible use is penalizing nodes that lie along the paths of nearby moving units. One of the downsides of A* is that when a group of units all try to find paths to a similar location, there is usually a significant amount of overlap, as one or more units try to take the same or similar routes to their destinations. Adding a penalty to nodes already 'claimed' by other units will help ensure a degree of separation, and reduce collisions. Don't treat such nodes as unwalkable, however, because you still want multiple units to be able to squeeze through tight passageways in single file, if necessary. Also, you should only penalize the paths of units that are near the pathfinding unit, not all paths, or you will get strange dodging behavior as units avoid paths of units that are nowhere near them at the time. Also, you should only penalize path nodes that lie along the current and future portion of a path, not previous path nodes that have already been visited and left behind.

 

3.理未探索區域:你曾一款PC算機是準確的知道路如何走,即便地沒有探索?根據不一樣的遊,那路徑搜索太好能夠是不現實的。幸運的是,是能夠很容易理的問題

3. Handling Unexplored Areas: Have you ever played a PC game where the computer always knows exactly what path to take, even though the map hasn't been explored yet? Depending upon the game, pathfinding that is too good can be unrealistic. Fortunately, this is a problem that is can be handled fairly easily.

 

答案是建一個獨立的「已知的通列的每一個玩家以及電腦對手的(每一個玩家,不是每個 - 那將須要更多的算機內存)。每一個列將包含有關玩家已探索區域的信息,與地的其餘部分假設爲適宜步行,直到明並不是如此。使用種方法,位將漫步死角,使似的錯誤選擇,直到他們發現的路。一旦地探索,然而,路徑搜索會正常工做。

The answer is to create a separate "knownWalkability" array for each of the various players and computer opponents (each player, not each unit -- that would require a lot more computer memory). Each array would contain information about the areas that the player has explored, with the rest of the map assumed to be walkable until proven otherwise. Using this approach, units will wander down dead ends and make similar wrong choices until they have learned their way around. Once the map is explored, however, pathfinding would work normally.

 

4.更平滑的路徑:A *會自動給出最短,成本最低的路徑,它不會自動給出看起來最平滑的路徑。看一看在我的(7算的例子最路徑。在路徑中,第一個步是下面,並開始方格的右。會不會我的道路更順暢,若是第一步是正下方的起點,而不是方形的方格?

4. Smoother Paths: While A* will automatically give you the shortest, lowest cost path, it won't automatically give you the smoothest looking path. Take a look at the final path calculated in our example (in Figure 7). On that path, the very first step is below, and to the right of the starting square. Wouldn't our path be smoother if the first step was instead the square directly below the starting square?

 

有幾種方法來解決問題。當你正在算路徑,你能夠處罰節點那裏有方向的化,增長了處罰G扣分。或者,你能夠通你的路徑運行的算後,找在那裏選擇鄰節點的地方會你看起來更好的路徑。欲瞭解更多關於整個問題向更加逼真路徑搜索,一個(免的,但須要註冊)在Gamasutra.comMacro Pinter文章

There are several ways to address this problem. While you are calculating the path you could penalize nodes where there is a change of direction, adding a penalty to their G scores. Alternatively, you could run through your path after it is calculated, looking for places where choosing an adjacent node would give you a path that looks better. For more on the whole issue, check out Toward More Realistic Pathfinding, a (free, but registration required) article at Gamasutra.com by Marco Pinter.

 

5.非方格搜索區域:在我的例子中,我使用了一個簡單的二方格佈局。你並不須要使用種方法。你可使用不規則的形狀區域。想一想棋風險,以及國家在那。你能夠設計一個路徑搜索方案行一。要作到一點,你須要建一個表,用於存鄰的國家,並與移從一個國家到下一個相關的G。你須要拿出用於估H.其餘一切會被理一在上面的例子中的方法。而不是使用相的方格,你會簡單找相國家在表中增長新的目到開啓列表

5. Non-square Search Areas: In our example, we used a simple 2D square layout. You don't need to use this approach. You could use irregularly shaped areas. Think of the board game Risk, and the countries in that game. You could devise a pathfinding scenario for a game like that. To do this, you would need to create a table for storing which countries are adjacent to which, and a G cost associated with moving from one country to the next. You would also need to come up with a method for estimating H. Everything else would be handled the same as in the above example. Instead of using adjacent squares, you would simply look up the adjacent countries in the table when adding new items to your open list.

 

,你能夠建一個固定的地形路徑的航點系。航點一般走的路徑上的點,也在一個地牢道路或隧道的關。做戲設計者,你能先指定些路點。兩個航點會被認爲是「相」彼此是否有它的直路徑上沒有障礙。因爲在風險的例子,您將省在某種型的找表接信息,並用它生成新的開啓列表。那麼你會(可能通使用的直距離)和H成本(可能使用從點到目的直距離)記錄相關的G。一切將繼續如常。

Similarly, you could create a waypoint system for paths on a fixed terrain map. Waypoints are commonly traversed points on a path, perhaps on a road or key tunnel in a dungeon. As the game designer, you could pre-assign these waypoints. Two waypoints would be considered "adjacent" to one another if there were no obstacles on the direct line path between them. As in the Risk example, you would save this adjacency information in a lookup table of some kind and use it when generating your new open list items. You would then record the associated G costs (perhaps by using the direct line distance between the nodes) and H costs (perhaps using a direct line distance from the node to the goal). Everything else would proceed as usual.

 

阿米特Patel寫了一個短的文章研一些替代品。於使用非方形的搜索區域上等距RPG搜索的另外一個例子,看看個人文章兩個次的A *路徑搜索

Amit Patel has written a brief article delving into some alternatives. For another example of searching on an isometric RPG map using a non-square search area, check out my article Two-Tiered A* Pathfinding.

 

6.一些超速提示:當你開本身的A *程序,或者改我寫的,你最發現路徑搜索使用你的CPU時間大幅大,特是若是你對路徑搜索的一臺像的數目板和一個至關大的地。若是你網上讀過西了,你會發現是真的,即便誰設計像星爭霸或帝國代遊專業人士。若是你看到的西開始放,因爲路徑搜索裏有一些想法,可能會加快速度:

6. Some Speed Tips: As you develop your own A* program, or adapt the one I wrote, you will eventually find that pathfinding is using a hefty chunk of your CPU time, particularly if you have a decent number of pathfinding units on the board and a reasonably large map. If you read the stuff on the net, you will find that this is true even for the professionals who design games like Starcraft or Age of Empires. If you see things start to slow down due to pathfinding, here are some ideas that may speed things up:

 

一個小地或更少的位。

Consider a smaller map or fewer units.

 

不要作路徑搜索以上幾個時間。相反,把它放在一個列,它分佈在幾個遊。若是你的遊戲時,好比,每秒40個週期運行,沒有人會注意到。但他發現,若是遊彷佛在一段時間放慢每一次當一束位都算路徑在同一時間

Never do path finding for more than a few units at a time. Instead put them in a queue and spread them out over several game cycles. If your game is running at, say, 40 cycles per second, no one will ever notice. But they will notice if the game seems to slow down every once in a while when a bunch of units are all calculating paths at the same time.

 

使用更大的方格(或者任何你正在使用的形狀)您的地減小了搜索以找到的路徑的點的數。若是你有雄心,能夠設計出了用於在不一樣的狀況下,取決於路徑的度的兩個或更多個路徑搜索專業人士作的,使用大面路徑,而後切到更精的使用小的方格/地區搜索,當你接近目。若是你有趣在個概念,看看個人文章兩個次的A *路徑搜索

Consider using larger squares (or whatever shape you are using) for your map. This reduces the total number of nodes searched to find the path. If you are ambitious, you can devise two or more pathfinding systems that are used in different situations, depending upon the length of the path. This is what the professionals do, using large areas for long paths, and then switching to finer searches using smaller squares/areas when you get close to the target. If you are interested in this concept, check out my article Two-Tiered A* Pathfinding.

 

於更的路徑,考是硬接到遊戲預算好的路徑。

For longer paths, consider devising precalculated paths that are hardwired into the game.

 

慮預處理地找出哪些域是從地的其他部分沒法訪問。我把域的「孤」。在現實中,他能夠是島嶼或者其餘任何地區,是另有圍牆,沒法訪問。其中A *的缺點之一是,若是你告它來找路徑等方面,它會搜索整個地,停,只有當每平方訪問/點已通打開和關單處理。會浪大量的CPU時間。它能夠通過預先肯定哪些地區是不可訪問(通洪水填充或似的程序),記錄在某種型的列信息,而後在開始路徑搜索前檢查它來防。

Consider pre-processing your map to figure out what areas are inaccessible from the rest of the map. I call these areas "islands." In reality, they can be islands or any other area that is otherwise walled off and inaccessible. One of the downsides of A* is that if you tell it to look for paths to such areas, it will search the whole map, stopping only when every accessible square/node has been processed through the open and closed lists. That can waste a lot of CPU time. It can be prevented by predetermining which areas are inaccessible (via a flood-fill or similar routine), recording that information in an array of some kind, and then checking it before beginning a path search.

 

擁擠的,迷似的境中,考慮節標記不隨地致的死角。些區域能夠手動預先指定的地圖編輯器,或者若是你有雄心的,你能夠開一個算法,自動識別域。在定的死衚衕區域點的任何集合能夠予一個惟一的識別。而後路徑搜索時,只停下來考一個死衚衕區域點,若是起始位置或目的地剛好是在特定的死衚衕區問題,你能夠放心地忽略全部的死角。

In a crowded, maze-like environment, consider tagging nodes that don't lead anywhere as dead ends. Such areas can be manually pre-designated in your map editor or, if you are ambitious, you could develop an algorithm to identify such areas automatically. Any collection of nodes in a given dead end area could be given a unique identifying number. Then you could safely ignore all dead ends when pathfinding, pausing only to consider nodes in a dead end area if the starting location or destination happen to be in the particular dead end area in question.

 

7.維護開啓列表:這實際上是A *路徑搜索算法中最耗費時間的元素之一。您能夠訪問開啓列表,都須要找到具備最小F方格。有幾種方法能夠作到一點。根據須要,你能夠保存路徑目,每次當你須要找到最小F的方格簡單的遍整個列表。簡單的,但路徑很慢。能夠通過維護一個排序的列表,每次須要最小F-成本方形時間只需抓住了第一個目從名獲得改善。當我寫個人程序,是我用第一種方法。

7. Maintaining the Open List: This is actually one of the most time consuming elements of the A* pathfinding algorithm. Every time you access the open list, you need to find the square that has the lowest F cost. There are several ways you could do this. You could save the path items as needed, and simply go through the whole list each time you need to find the lowest F cost square. This is simple, but really slow for long paths. This can be improved by maintaining a sorted list and simply grabbing the first item off the list every time you need the lowest F-cost square. When I wrote my program, this was the first method I used.

 

將工做得至關好小地,但它不是最快答案重的A *程序員誰想要真正的速度使用一種叫作二制堆,是我在個人代中使用。在個人經驗種方法將是至少2-3倍的速度在大多數狀況下,而且在幾何形狀更快(快10+次)上較長的路徑。若是你主找更多關於二叉堆,看看個人文章,在A *路徑搜索使用二制堆。

This will work reasonably well for small maps, but it isn't the fastest solution. Serious A* programmers who want real speed use something called a binary heap, and this is what I use in my code. In my experience, this approach will be at least 2-3 times as fast in most situations, and geometrically faster (10+ times as fast) on longer paths. If you are motivated to find out more about binary heaps, check out my article, Using Binary Heaps in A* Pathfinding.

 

另外一種可能的瓶是你的方式明確和維護路徑搜索調用之的數據構。我我的更喜全部列。點能夠生成,記錄並保存在一個動態的,面向象的方式,我發現建和象所需的時間量增長了外的開,沒必要要的水平會減慢速度。若是你使用數,不,你須要調用之西了。你會想在種狀況下的最後一件事就是花零時間作完一切了在調用路徑搜索後,特是若是你有一個大的地

Another possible bottleneck is the way you clear and maintain your data structures between pathfinding calls. I personally prefer to store everything in arrays. While nodes can be generated, recorded and maintained in a dynamic, object-oriented manner, I find that the amount of time needed to create and delete such objects adds an extra, unnecessary level of overhead that slows things down. If you use arrays, however, you will need to clean things up between calls. The last thing you will want to do in such cases is spend time zero-ing everything out after a pathfinding call, especially if you have a large map.

 

我避免種開過創建一個二whichListXY),其指定在每一個點上個人地任一開啓列表或關列表上。路徑搜索嘗試以後,我不零數。相反,我在每個路徑搜索呼叫復位onClosedListonOpenList的價值觀,每一個路徑嘗試尋+5什麼都增。通過這種方式,算法能夠放心地忽略垃圾從之前的路徑搜索嘗試遺留任何數據。我也喜存放FGH列的成本。在種狀況下,我只是寫在任何先存在的價和不打清除,我作的。

I avoid this overhead by creating a 2d array called whichList(x,y) that designates each node on my map as either on the open list or closed list. After pathfinding attempts, I do not zero out this array. Instead I reset the values of onClosedList and onOpenList in every pathfinding call, incrementing both by +5 or something similar on each path finding attempt. This way, the algorithm can safely ignore as garbage any data left over from previous pathfinding attempts. I also store values like F, G and H costs in arrays. In this case, I simply write over any pre-existing values and don't bother clearing the arrays when I'm done.

 

在多個列存數據佔用更多的內存,然如此,有一個衡。最,你應該使用什麼方法,你是最舒服的。

Storing data in multiple arrays consumes more memory, though, so there is a trade off. Ultimately, you should use whatever method you are most comfortable with.

 

8. Dijkstra的算法:當A *一般被認爲是最好的路徑搜索算法(面的小咆哮),存在至少一個其它的算法有其用途 - Dijkstra算法。 Dijkstra的是基本相同的A *,除了沒有啓式(H終爲0)。因它沒有啓式,它通在每個方向同樣擴大了搜索。正如你可能想象的,因爲這Dijkstra算法一般是束了探索一個更大的區域以前目發現一般使得它比A *慢。

8. Dijkstra's Algorithm: While A* is generally considered to be the best pathfinding algorithm (see rant above), there is at least one other algorithm that has its uses - Dijkstra's algorithm. Dijkstra's is essentially the same as A*, except there is no heuristic (H is always 0). Because it has no heuristic, it searches by expanding out equally in every direction. As you might imagine, because of this Dijkstra's usually ends up exploring a much larger area before the target is found. This generally makes it slower than A*.

 

那麼,什麼使用它?有候,我不知道我的目位置是。假你有一個須要去得某種源的一些源收集裝置。它可能知道幾個源區域,但它但願去最近的一個。在裏,Dijkstra的比A *更好,因不知道哪個是最接近的。我惟一的選擇是重複使用A *找到每個的距離,而後選擇這條道路。可能有無數似的狀況,我知道那種位置,我可能會找的,想找到最近的一個,但不知道它在哪裏或哪個多是最接近的。

So why use it? Sometimes we don't know where our target destination is. Say you have a resource-gathering unit that needs to go get some resources of some kind. It may know where several resource areas are, but it wants to go to the closest one. Here, Dijkstra's is better than A* because we don't know which one is closest. Our only alternative is to repeatedly use A* to find the distance to each one, and then choose that path. There are probably countless similar situations where we know the kind of location we might be searching for, want to find the closest one, but not know where it is or which one might be closest.

 

(待續)

相關文章
相關標籤/搜索