October 13, 2015

Forest Fire Simulator Update - improved trees and fire

2 comments
A few week ago I finished the first version of a forest fire simulator in Unity. I used the real physics equations, which worked perfectly fine, but what didn't work fine was the performance of the simulation. The physics equations were not the problem - the problems were the number of trees and the fire and smoke. I even had to add a special button so the user could remove the smoke and flames because everything was so slow. This is how it looked like:


You can see in the image above that the simulation is running at 12 frames per second, which is not that good! To improve that number I first decided to improve the trees. The problem was that the trees were all individual objects, and to improve performance you have to combine them into fewer objects. But I also have to remove trees that have burned down and add trees that are more darker. After a few experiments, I realized that the fastest way was to once in the beginning combine all trees of each tree type, and then cheat by moving trees that are not to be seen to positions the user can't see. It looks like this behind the scenes:


You can see that all black trees are hidden below the ground. And when one of those trees are supposed to be visible, I just move the vertices to above the ground. This is super fast, but tricky to figure out how to do it and no one else had really discussed it when I made a Google search for it. So I decided to make my own tutorial on how to do it. You can find it here: Dynamic Mesh Combining. It took a while to write it, but I'm a big believer in the idea that you should share your knowledge, and if no one else has done so, then people will find you through Google and you might get links and sell more of your products! Moreover, if you do good you will get comments like this on Reddit, which is good for the self esteem:


Anyway, the last problem was the fire and smoke. I used to have one fire and one smoke particle in each square. But when the entire forest is on fire, this will be really slow. A better way is to try to have fewer, but larger fire and smoke particles. So I wrote an algorithm that searches through the map and tries to build larger squares, up to 12 squares by 12 squares. The result is this:


You can clearly see that the entire forest is on fire, but the simulation is running at a fantastic 30 frames per second thanks to the improved trees and fire. I also think the smoke is looking more realistic, so that's a good side-effect: faster and better. 

If you want to test the forest fire simulator you can test it here: Forest Fire Simulator