简体   繁体   中英

A* 3D Path-finding - Unity

I have been playing around in Unity and I have implemented the A* algorithm for some path finding on a 2D square node grid. Since then, I have extended my path-finding to a 3D node grid, so I basically have a 2D grid node for the ground, with another 2D grid node on top of it to act as the sky.

The plan for this was to create airports, and air travel using A*. So far, my two grids are linked together through the airports. For example, when I search for neighbor nodes on the ground grid of a node which is an airport, I also add what would be the adjacent nodes in the sky grid to the list. This allows "take off" from the airport, to an adjacent node in the sky. Also when in the sky, I search adjacent nodes, and if one of the adjacent ground nodes is an airport, I add that to the neighbor list. This allows the plane to "land" into the airport from the sky.

So far I have movement costs associated with various terrains for ground travel, and my air travel is currently 0, however there is a big movement cost during the "take off" and "landing" movements to and from the sky. This was in hopes to create a faster path through the sky, making it worthwhile, but adding biggish one time movement costs for balance, so going between two airports say 4 nodes away would be pointless.

This is all working fine. The guy chooses to go by airplane if the travel cost is less by air that if he would have to walk it on the ground. However, my problem is that sometimes the ground path, and therefore the path with any cost associated with it is sometimes chosen over the airport, when it is clear that there are more ground nodes to cover doing this than taking a plane.

I had a feeling that this may be due to path searching towards the goal, and not the airport, which could be in another direction and because of this the airport not being a neighbor node of any node that is searched, and therefore not even considered even though it could be a faster path solution.

Does this sound like it could be the problem I am having? Am I using the wrong type of algorithm if I am wanting to do something like this? Is there anything I could do to help nearby airports either always be checked, or give them more attention? I guess I could add roads leading to them with a lower movement cost or something...

Thanks for any help!

A* is based on heuristics. As such it is not guaranteed to produce global optimum solutions unless the heuristic is carefully chosen to have certain admissibility qualities. Without that A* is relaxed into a form that produces locally optimal solutions (hence the sometimes in your post).

If you want your algo to always find the guaranteed optimal path then you could look at improving the heuristic or at a different algorithm.

You could probably get the result you want by modifying your heuristic. For example, if you reduce the heuristic value at a node based on the distance from that node to the nearest airport, you will bias the A* algorithm to search for paths that go towards airports.

For example, say you're using a specific heuristic, like Manhattan. So when calculating the heuristic value for any node, take the lesser of:

  1. The Manhattan distance to the target node
  2. The Manhattan distance to the nearest airport + cost of flying to the airport nearest the target + Manhattan distance from the destination airport to the target.

Some caution is advised. Tweaking the heuristic to bias the algorithm to test certain paths may end up giving you poor results when those paths aren't the correct shortest path. That's just sort of the nature of the beast when doing this.

Several other answers have stated that A* is a heuristic, and as such will not always give optimal results. This is incorrect. A* uses a heuristic, but it is not itself a heuristic. It will always give the optimal result.

Assuming, that is, you're using it correctly. The heuristic you pass to A* must never overestimate the cost of reaching the destination . So, if there is a nearby airport that reaches your destination with cost 50, and your heuristic says the cost is ~100, A* won't work correctly. Given the description you gave, I think this is the most likely culprit.

(Yes, this means you can set your heuristic to always be 0. In that case, A* devolves into Dijkstra's algorithm )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM