Polishing our Creatures
Hello,
I have been reworking our pathfinding system in the past week.
The old system was quite slow and resulted in mobs freezing in place because they had to wait a long time for a path to be found.
Handeling pathfinding on large maps is generally quite difficult but i found a way to increase the performance massively.
We have 16 million pathfinding nodes on the map. Each node stores a position and whether it is occupied or not. Loading all of these nodes into memory on start would be very inefficient.
That is why i switched to serialization for my new pathfinding system.
Previously i solved this memory issue by storing these nodes in a texture map but that allowed for the system to only find one path at a time. So whenever a group of pigs got scared and wanted to find a path then each one of them had to wait for every other pig to have found a path. Sometimes pigs would wait up to ten sconds until they got their path.
My new system allows for multiple paths to be calculated at the same time. I have set the system to run six pathfinding processes at a time and i run them on seperate threads. Most of Unity‘s classes are not thread-safe but you can run an A* function in a seperate thread without big issues.
Running the calculations on seperate threads is also reducing lag on the main thread which was another limiting factor for the old pathfinding system.
The Results:
Finding a path with a distance of 80 meters took the old system 0.342 seconds or 17 frames (with 60fps).
The new system takes only 0.038 seconds or 2 frames for the same path. That is 10 times faster than the previous system(or an 800% increase in performance)! And since the new system can find six paths at a time it would end up to be more like a 4800% increase.
However when 100 creatures request paths at the same time we would still have some creatures that freeze in position because they need to wait until their path is calculated.
To hide this waiting period I added a neat loop that makes creatures run in a circle when they are waiting for a path. This is way better than having them freeze in place and you might find the same behaviour in mobs of other games.
This patch will come to the staging branch in a couple of days. I already started to work on the android systems but I want this patch out before i continue workin on it.
I have been reworking our pathfinding system in the past week.
The old system was quite slow and resulted in mobs freezing in place because they had to wait a long time for a path to be found.
Handeling pathfinding on large maps is generally quite difficult but i found a way to increase the performance massively.
We have 16 million pathfinding nodes on the map. Each node stores a position and whether it is occupied or not. Loading all of these nodes into memory on start would be very inefficient.
That is why i switched to serialization for my new pathfinding system.
Previously i solved this memory issue by storing these nodes in a texture map but that allowed for the system to only find one path at a time. So whenever a group of pigs got scared and wanted to find a path then each one of them had to wait for every other pig to have found a path. Sometimes pigs would wait up to ten sconds until they got their path.
My new system allows for multiple paths to be calculated at the same time. I have set the system to run six pathfinding processes at a time and i run them on seperate threads. Most of Unity‘s classes are not thread-safe but you can run an A* function in a seperate thread without big issues.
Running the calculations on seperate threads is also reducing lag on the main thread which was another limiting factor for the old pathfinding system.
The Results:
Finding a path with a distance of 80 meters took the old system 0.342 seconds or 17 frames (with 60fps).
The new system takes only 0.038 seconds or 2 frames for the same path. That is 10 times faster than the previous system(or an 800% increase in performance)! And since the new system can find six paths at a time it would end up to be more like a 4800% increase.
However when 100 creatures request paths at the same time we would still have some creatures that freeze in position because they need to wait until their path is calculated.
To hide this waiting period I added a neat loop that makes creatures run in a circle when they are waiting for a path. This is way better than having them freeze in place and you might find the same behaviour in mobs of other games.
This patch will come to the staging branch in a couple of days. I already started to work on the android systems but I want this patch out before i continue workin on it.