(GIFs below)
A couple months ago I started experimenting with tree generation. They are a special type of tree, they’ll be significantly larger than regular trees in the game. Instead of just being decorations, they’ll offer dynamic explorable areas on their branches - those can vary by biome, the region of the world and also vary from nests of large eagles to a bit more fantastical and complex areas.
The core of the generator is a simple branch generation. It creates the first piece and keeps building on top of it till it’s not reasonable to continue:
Then I threw a bunch of these branches together. While it didn’t look too bad and kind of worked, it was still not up to the standards that were required. It had a random chance to generate a branch in case there was enough distance between the new branch and an old one or in case there hasn’t been one in awhile, it’ll generate one despite the random chance (the numbers on rectangles display the number of branches created).
So the question was how to generate branches in a way that they would grow naturally and also be placed in a way that doesn’t scratch the eye too much. The answer was to use a force field based on nodes (so it’s not really a field technically, but I’m rendering it as so). I can input any coordinate around the tree and it will neatly weigh in all the nodes based on their size and distance and return a single vector pointing to the direction where there are least obstructions and the length of the vector points to how much care should be given to it. If it’s a zero, we can just generate a random branch and not care about the field (though that’s almost never the case).
The force field can be seen rendered here, on the next images that include the force field it will just be rendered as points rather than actual vectors to keep things more clear, but it’ll still be in there.
The first thing I did after that was to simply make the branch growth follow the force field - avoid other branches by finding the best path around them. It’s also quite simple to make branches regrow, yay!
So finally I made the whole generation follow the force field. In case there are no nodes around, we can feel pretty free to generate a new branch. The branche’s angle is also calculated simply by using the same vector force field function. It returns the angle pointing away to the most empty area around a point, so it resembles how branches work in real life - they like to grow towards places where the sun shines.
Now the final problem that I hadn’t given enough thought to was making the trees actually prefer growing upwards rather than in the general direction of what they were created in. At the first glance this seemed hard to combine while I already have the force field, but it actually made things way easier. I simply added a small upwards value to the returned vector and voila, the branches point where they are supposed to. It’s also interesting to note that there’s a slight gradient going on in the force field. The upwards force is stronger at the bottom of the gif and weaker at the top. The reason for that is because the tree grows from bottom-up. The first branches go for the sun and the upper branches are simply forced to follow their lead despite a weaker upwards force - because the bottom branches themselves provide this force for the branches above. Things might look slightly too upwards if this wasn’t the case.
You can also play around with the tree generator yourself https://t.co/TPOByJsqzg. Edit modifiers with the mouse wheel and ctrl (works if the setting is red on hover), space button regenerates, enter randomises.
So lets end it with a couple more chaotic trees.
6 comments