# How would you generate details like trees, water or caves in a 2D terrain?

Yesterday I made a post about generating a 2D planet, which resulted in this:

I did it by subtracting a radial gradient from simplex noise (explained in my other post).

Now of course thats a bit boring, what technique should I use to add details like trees, lakes or caves? Would be able to do something like this (but of course across the whole planet):

I know doing it on a planet would make it a bit harder, because you would have to know if f.E. the tree is on the north of the planet or the south. To make it easier, if you want, just assume a flat 2D surface like this:

I think you can achieve water by creating a wave. Then take a radial sweep of the planet, following the wave linearly, and cutting out the land and replacing with water according to the height of the wave. The wave would need to be able to sample for the circumference of the planet and also need to be able to wrap to the beginning. I think trees can be done in a similar manner, ignoring the water, place trees according to some sort of wave that acts as a tree density. I'm unsure about caves specifically, but I suspect a similar solution may also be applied, but on a smaller scale. Simply choose a start, and create some sort of wave that determines whether or not you should move up/down during the generation of a single cave.

So you would start with angle = 0 and rotate around the planet. The wave to the left(backed by a 1D array or similar structure) would be used to determine whether or not you should place water at that location. Similarly, you can follow this convention to place trees. I think it would be more difficult to directly turn this into a cave system, but I think it may have some sort of application for doing such.

• Sry I dont really understand what you mean by creating a wave and taking a radial sweep? Could you just add a mockup of ur idea or something? Sry, just trying to understand – zebleckDAMM Aug 26 '16 at 14:02
• @zebleckDAMM I've updated my post with a small diagram. – ndenarodev Aug 26 '16 at 17:30
• sounds interesting, will try it – zebleckDAMM Aug 26 '16 at 22:57

This isn't so much an answer as a potential idea, but I'll post it anyway. Keep in mind I'm not very experienced in actually implementing these things.

What you could do for the flat planet is make a list of all the surface pixels, and then allow a function such as a random number generator to choose from this list some pixels to be populated with trees. Lakes could be done in a similar fashion, choosing a pixel and then using some kind of algorithm to generate it. Not as sure about caves, sorry. As far as actually detecting surface pixels, you could do this most efficiently by detecting them at generation, although I'm not sure of what your algorithm is. I guess you could also potentially check if it's a ground pixel by looping through all pixels and finding whether or not there is a sky pixel above them. As a third suggestion, if the layer of grass is only ever going to be 1 pixel thick, you could simply fill the list with all grass pixels

Any feedback to this response would be appreciated, as as I said I'm not overly confident myself. Nonetheless, I hope this helped even slightly.

Good luck, and happy coding 🙂

• Checking surface pixels is quite easy and I also have the list of pixels, thought of this too, but this method would allow for little customization, for example I wouldnt know how to do forests with this method, trees would probably just be evenly distributed across the planet. – zebleckDAMM Aug 26 '16 at 14:04
• This can be easily modified to do forests as zones rather than individual trees. Instead of checking locations individually, check multiple randomized locations nearby in the same radial sweep. – Pikalek Oct 4 '16 at 15:19

I am assuming the world as flat.

Regarding water - you could decide on a sea level. When generating the terrain you place it on a distance from the center of the round world or from bottom of a flat world so that X % is above sea level and the rest below sea level. Then you fill all "air" spaces below sea level with water. With your current generation that would give a lot of tiny lakes, you probably want larger and smoother variations to get larger lakes (and hills).

If you want to have mountain lakes etc, you could try to randomly test blocks in the air and test to "flood fill" horizontally and downwards from that block and see if the size of the resulting lake would be of a good size (most attempts will result in flooding the earth).

For caves, you can: 1. Pick a random starting position (x, y). 2. "Dig" - replace that block with air 3. Generate a trajectory (dx, dy) and a delta trajectory (ddx, ddy). 4. Proceed in the trajectory (x = x + dx, y = y+dy). Have a small probability of changing the trajectory delta (ddx, ddy). Also have a small probability of stop digging, and a small probability of branching (MUST be smaller than the probability of stop digging). For branches, RECURSIVELT start from step 1 on the branched block. 5. If we decided to stopped dig, or if the block we end up on is air, we have reached the surface again or reached a different cave. Stop looping. Otherwise, start over from #2.

If you wanted the cave to start on top, let the starting block be at the surface. If you want a subterranean block, start anywhere and run the process twice for opposite directions of the starting point. If you want the block to be more than one block wide, "expand" it during or after the digging.

For trees, I guess you want them lumped in forests, you could do something like this: 1. Randomize a forest size (effectively length): l 2. Randomize a location: x 3. Check if the location x, x+1, x+2, ..., x+l is suitable for a forrest: are all ground pieces (not water), check other criteria (maybe not put forrest on a steep slope, or above tree level, etc). Start over from step 1 if the spot was not suitable for a forest. 4. Within the forest area, randomize the trees, for a certain probability of a tree on each ground block on the surface (but do not allow trees on adjacent surface blocks). Near the edges of the forest area, have a gradient probability of trees to hide the edge of the forest area.

In a flat world this should be fairly simple, question is how it translates to circular world. You may want to create many common functions so that you can get things like: * distance of block from center of earth * is the block a surface block * get adjacent surface block * get gravitationally adjacent surface block * get angle of block relative to north pole (needed to know in what angle to grow a tree, once you have picked a block suitable for a tree)

For the caves you should be able to disregard that the earth is round, except if you want to avoid having too steep caves etc in which case you need to account for angle of the block when determining angle.