My goal this week was to to improve the performance of my flock simulation by improving the main update loop.
Spatial Partitioning
Currently, the main bottle neck is how I implemented the Separation method for the flock controller which needs to do a distance check against all of flocks in the scene. This quickly becomes really expensive because each additional flock added increases the number of calculations needed in update for everyone.
In order to improve this I wanted to try and implement Spatial Partitioning. How it would work would be to split the map into grids where the distance check for each flock would only be against the other flocks in the same cell and ignoring the ones outside of it.
I used OnDrawGizmos to visualize the different cells in the editor and then made both a list for each cell as well as a method to calculate which cell the flock is in so it can access the correct list.
I added line visualizations so that the flock draws a line to the other flocks that it be checking the distance to try and help with both testing/debugging.
Setting up the flow so that it automatically adds itself to the correct list when it moves to a new cell as well as removes itself from the previous list in its previous cell position took a lot longer then I expected. I think the reason why it took me so long was because I tried splitting up the process into different methods too early and it made following the logic harder to follow/debug.
To try and make this more flexible, it was made so that it automatically resizes appropriately for changes in scale.
Flock Request Manager
Another improvement was implementing a request manager for all updates. So instead of having the calculations occurring in each flocks individual update every frame. What happens instead is that they create a flock movement request which is then sent to the flock manager which puts their request into a stack. The flock manager then every update dequeues a specified number of request to process every frame. This puts a hard limit on the number of flock movement requests that can occur every frame and helps improve performance so that there can be more of them in the scene.
Putting everything together I found that I could easily increase the number of spawn flock members over the previous iteration without a huge drop in performance.
I also found that one of the biggest benefits of the current implementation over the previous set up is that there is more parameters I can change to try and improve performance. I can decrease the number of flock request processed every frame, increase the preferred distance between the flocks as well as make the spatial partitioning more dense.
Looking for answers to your CBD questions? Weed Viral is here to help you get the accurate, evidence-based information you need.
ReplyDelete