Pathfinding Algorithm Simulation

Hi Developers,

Recently I have been exploring Artificial Intelligence and came across pathfinding algorithms. I decided to take this deeper and implement them inside of Roblox. It was a lot of fun to work with all these algorithms and see how they differed from each other. After implementing some algorithms, I turned it into a cool little game where you can visualize them and decided to open source it. I hope you can learn from it and even have a little fun with it too!

Implemented Algorithms:

  • Depth-first Search
  • Breadth-first Search
  • Greedy Best-first Search
  • Dijkstra’s
  • A*

Since the graph in the game is unweighted, some algorithms may look very similar to each other. You can find all the code for the algorithms in the “Algorithms” module located in StarterPlayerScript–>Client–>Modules.

Bonus: I also made modules for different data structures(queues, stacks) which can be found in ReplicatedStorage–>Modules–>DataStructures

Open Sourced Game: https://www.roblox.com/games/5136293371/Pathfinding-Algorithm-Simulation

Video:

84 Likes

I’ve removed the tag from your title–that’s what the “optional tags” section is for. :slight_smile:

2 Likes

How would someone go about putting this into 3D?

8 Likes

Me and my friends are about to try and figure that out

Hi there, I apologize for the late reply. I’m going to mention you too, @EternalEthel, because I apologize for replying so late, after four years. (I’m not very active on here or Roblox in general anymore)

Anyway, before I answer your question, I would like to address a few things I haven’t mentioned in this open-source game.

Efficiency

When I made this game, I was just starting to learn about pathfinding algorithms. I did not intend to write all those algorithms for the sole purpose of putting them together into an open-source simulation(that happened later on when a friend approached me with the idea).

The main issue is that the algorithms are not as optimized as they should be. They use way more memory than necessary, and the path reconstruction is particularly inefficient. It’s storing more information(all parent nodes) than it should, which isn’t the most efficient way to backtrack in 2D. What it should’ve done is overwrite the grid with the cardinal directions of where each node was explored from. This meant you can easily reconstruct the path by following the parent without using extra memory(because you are just overwriting your existing grid). If this interests you, you can probably find some resources online on this.

Unweighted Grid

Some algorithms in the simulation don’t particularly show their strengths(and weaknesses) because of how the grid is constructed. You can easily see this by trying out both Dijkstra’s and BFS on the same grid. Spoiler: They will behave the same way.

This happens because the grid is unweighted, or in other words, the cost of each node is the same. Going to an adjacent node costs the same. This means that Dijkstra’s algorithm, which was made specifically for weighted graphs, behaves the same as BFS on unweighted graphs. I’m surprised no one has pointed this out after a couple of years.

Pathfinding in 3D

Now, before I answer your question, I want to mention that I won’t go into the details on this, but rather give you some guidelines on how to approach this.

First of all, do you need a 3D version of this? Now, this might sound like a dumb question, but think it through because in a lot of scenarios, you can just use a 2D grid. If you have NPCs that only move in the same plane, that’s just a 2D solution.

Once you answer that question, you should think about how to convert your game into a grid. There are many ways to do this, and some are more efficient than others. Some people like to use quadtrees/octrees, while others find that just splitting their game into fixed chunks works.

Finding fast yet accurate ways of constructing a grid is essential to make sure your pathfinding algorithm works efficiently. Otherwise, you are better off just using Roblox PathfindingService.

To give you an example, I’ll use @Ultraw’s Restaurant Tycoon 2. I chose this particularly because of the way the building system is designed, and this is one of the cases where making your pathfinding algorithm can show some benefit. Just to be clear, I’m not saying that Restaurant Tycoon 2 uses a custom pathfinding algorithm(I don’t know that), but the game is a good example for my point.

In the image above, you can see how the floor is split into fixed chunks, which can easily be turned into a 2D array(this is most intuitive, but you can also explore adjacency list representations) where each element is the state or cost of each chunk. Since you want to make sure your 2D array is up to date with what you see in the game, you would update this array every time a player placed new furniture.

Once you have a 2D array, using a pathfinding algorithm is pretty much trivial after that. There are lots of tutorials, pseudocode, and other information online that you can use for this part.

Anyway, I hope this helps guide you in the right direction. If you have any questions, I’m happy to help, although I can’t guarantee when I’ll reply :sweat_smile:.

1 Like

Yeah we use grid-based A* pathfinding, where each tile in the grid is a node, and the cost of traversing from one node to another is given a cost.

2 Likes