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 𝐵 Image
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 ℎ(𝑁). Image
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!!! ✨ Image
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! 😎

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Alan Zucconi

Alan Zucconi Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @AlanZucconi

9 Jan
If you are interested in learning AI and how it can be used in #gamedev, you should start with BEHAVIOUR TREES! 🌲🧠

As the cornerstone of game AI, BTs are used in:

• Halo 2 🪖
• Bioshock 💉
• GTA V 🚗
• Façade 🍈
• Alien: Isolation 👽

Let's see how they work! 👇

🧵 A screenshot of "owl-b...An example of Behaviour Tre...An example of Behaviour Tre...
Many of you are familiar with FINITE STATE MACHINES: the most popular pattern for character controllers.

While FSMs are graphs, BTs are (unsurprisingly!) trees. 🌲Their leaves are the ACTIONS 🦾 and PERCEPTIONS 👁️ your agents can perform and sense. Image
Each node in a BT can be in any of the three following states:

• ✔️ SUCCESS: the node was executed successfully
• ❌ FAILURE: the node could not be completed
• ⏳ RUNNING: the node is still being executed Image
Read 11 tweets
24 Nov 20
One of the best ways to improve your creativity is—paradoxically—to design with constraints.

Perhaps this is why the #pico8 community never failed to amaze me with their tiny creations.

So, here's a list of PICO-8 DEMAKES, starting with possibly the best one ever made...

/🧵👇
"FUZ", a #pico8 demake of @Polytron's "FEZ" by Henry Stadolnik (@Jusiv_).

jusiv.itch.io/fuz
"Low Mem Sky", a #pico8 demake of @hellogames's "No Man's Sky" by Paul Nicholas (@Liquidream).

liquidream.itch.io/low-mem-sky-ja…
Read 7 tweets
27 Sep 20
Turns out 2020 was—for many of us—one of the worst years on record.

If you are feeling stressed and need somewhere to escape for a little bit, this is a thread about some short, calming games that I hope will help you relax.

Starting with...

👇👇👇 🧵
"A Short Hike" is without any doubt the best indie title of the year. 🏔️🐦

@adamgryu has truly crafted something that can soothe your soul for a good few hours.

store.steampowered.com/app/1055540/A_…
"Kind Words" is a game like no other. 💌🤗

You can write about your own issues, and read anonymous replies from other players.

It's warming and empowering. @popcannibal created one of the few experiments on empathy that actually worked.

store.steampowered.com/app/1070710/Ki…
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!