Most #gamedev are familiar with A*, the most used pathfinding algorithm in games.

โš ๏ธ But did you know that there's a very common scenario in which A* DOES NOT find the shortest path? ๐Ÿ˜ฑ

Let's see why A* won't work in "Portal", and how we can fix it!

๐Ÿงต
All pathfinding algorithms work by iteratively exploring nearby cells in the grid.

โฌ…๏ธ Breadth First Search explores them in the order in which they are discovered.

โžก๏ธ Dijkstra's Algorithm, instead, keeps exploring the SHORTEST PATH FIRST.

(gif by @redblobgames)
That is also why Dijkstra's algorithm is sometimes referred to as the SHORTEST PATH FIRST algorithm.

Ultimately, it is that property that ensures its OPTIMALITY (= it will always find the shortest path between any two points).
A* is a variant of Dijkstra's.

Instead of just taking into account the cost to reach an already explored node, it also tries to estimate how far that node is from the target destination.

โ€ข ๐‘”(๐‘): known cost to go from ๐ด to ๐‘
โ€ข โ„Ž(๐‘): estimated cost to go from ๐‘ to ๐ต
The function โ„Ž(๐‘) is known as a HEURISTIC: an "educated guess", basically.

There are countless functions you can use, each one leading to a potentially slightly different solution.

A* will ALWAYS find a solution.
But whether that solution is OPTIMAL or not, depends on โ„Ž(๐‘).
For A* to be OPTIMAL (= guaranteed to find the SHORTEST PATH), โ„Ž(๐‘) must be ADMISSIBLE.

An admissible heuristic NEVER OVERESTIMATES the true, shortest distance from ๐‘ to ๐ต.

Below, you can see the actual shortest distance โ„Ž*(๐‘), compared to a possible heuristic โ„Ž(๐‘).
Finding an admissible heuristic is not an easy task.
But there is a scenario in which it becomes trivial.

If you are doing pathfinding in a 2D or 3D game, there is no shortest path than a straight line between two points!

The true shortest path must be longer or equal.
And because of this, virtually all games which are using A* are also using the Euclidean distance as their โ„Ž(๐‘) of choice! ๐Ÿ“

But, as hinted, there is a very common scenario in which many games which violates the admissibility constraint... TELEPORTING! โœจ
Teleporting between ๐ด and ๐ต means the existence of a path SHORTER than a straight line between ๐ด and ๐ต.

โš ๏ธ A* won't find the shortest path in Portal! ๐Ÿ˜ฑ

But there is no need to panic!
As long as the heuristic is MOSTLY respected, A* is NEARLY OPTIMAL.
If optimality this is critical for you, you can solve this problem by running A* three times:

โ€ข ๐‘‘โ‚€ = A* from ๐ด to ๐ต
โ€ข ๐‘‘โ‚ = A* from ๐ด to ๐‘‹ (closest teleport to ๐ด)
โ€ข ๐‘‘โ‚‚ = A* from ๐‘Œ (closest teleport to ๐ต) to ๐ต

If ๐‘‘โ‚+๐‘‘โ‚‚<๐‘‘โ‚€: RUN TO THE TELEPORT!!! โœจ
And for the @BostonDynamics fans reading this, here's a video of Professor Nosferatu bullying Shakey: the first robot for which A* was originally designed for!

Because AI researchers have been stopping robots from pushing boxes since 1972! ๐Ÿง›โ€โ™‚๏ธ

โœจ ๐˜๐—ต๐—ฎ๐—ป๐—ธ ๐˜†๐—ผ๐˜‚ ๐—ณ๐—ผ๐—ฟ ๐—ฐ๐—ผ๐—บ๐—ถ๐—ป๐—ด ๐˜๐—ผ ๐—บ๐˜† ๐˜๐—ฒ๐—ฑ ๐˜๐—ฎ๐—น๐—ธ โœจ

If you enjoy content about gamedev, shader coding and artificial intelligence, feel free to retweet & follow me for more! ๐Ÿ˜Ž
You can follow @AlanZucconi.
Tip: mention @twtextapp on a Twitter thread with the keyword โ€œunrollโ€ to get a link to it.

Latest Threads Unrolled:

By continuing to use the site, you are consenting to the use of cookies as explained in our Cookie Policy to improve your experience.