Using A* Pathfinding Notes

I made another demo a few days ago to realize the automatic pathfinding function. I saw that it was generally A*.

My main loop code:

isFindEndPoint = false;
//Main loop
do
{
CreateOutSkirtsNode(currpoint);//Create peripheral point
auto temppoint =SelectNextNode(currpoint);//Select the next current point from the periphery
currpoint = temppoint;
SetNodeToSelection(currpoint);// Add the selected current point to _SelectCollection and remove it from _unSelectionCollection


} while (!isFindEndPoint/*if the end point is outside the current point*/);

 

The algorithm is encapsulated in an algorithm class, and can be called at will by passing in several parameters when using it.

The main loop is about 3 things:

 1. Create peripheral points, that is, 8 points around point A, and use their coordinates to create a custom type MapNodeClass. This type stores some key information used in the calculation.

For example, fghxy Ispass parent, H simply uses the xy displacement difference. I believe that anyone who has seen A* will understand. My approach is to create MapNodeClass when the path needs to be detected.  

2. Select the next current point from the periphery

It is well understood that the selection is based on the value of f, which involves the problem of updating the value of F. This method has been wrapped in MapNodeClass

3. Add the selected current point to _SelectCollection and delete it from _unSelectionCollection

_SelectCollection is the colseList in the algorithm, indicating that there is no need to retrieve

_unSelectionCollection is the openlist that is to be retrieved

are well understood

---------------------

It looks almost the same, but there is a problem with it running. Because there is an alley around the starting point in my tile map test, there will be an error that the pointer is empty, and it is all errors that appear in SelectNextNode().

That is, in the second link, the found current point currpoint is empty. The reason is that after the algorithm falls into a dead end, there is no point in the openlist outside the currpoint, that is, there is no point that can be selected.

In this case, most of the A* introductions on the Internet did not mention this situation, and did not explain the dead end situation.

In fact, after thinking about it, the solution is also simple. It is nothing more than taking a step back and executing SelectNextNode() from the parent node of currpoint. So you need to modify SelectNextNode().

When it cannot detect the peripheral nodes of currpoint, it directly changes the detection range to openlist. Why is it not the periphery of the parent node, mainly considering that if the dead end is relatively deep, the detection range of the periphery of the parent node is too large.

It should be noted here that if there is a peripheral node in currpoint, the F value of the peripheral node (mainly the G value) will be updated, and when the detection range is expanded, the F value cannot be updated. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325113234&siteId=291194637