# Algorithm for create caves

This topic is 1052 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want to create caves on my 2d world generated its on a 2d array .

how can i gen like big caves little ones ect..?

##### Share on other sites

It sounds as though what you are looking for is land generation algorithms if that's the case there plenty of algorithms that would give you a variable landscape. One of them is Perlin Noise. It is a pseudo-random algorithm that generates a noise texture, that has several applications. There are several links from a google search that shows the process of creating the texture, and the application of it.

Marcus

##### Share on other sites

Land generator is done i want something for do caves on my generated terrain

##### Share on other sites
Perlin noise can generate caves. I use it in my game. Id suggest implementing the algorithm and developing a separate application where you can visually see the values that are generated in the 2D array. It'll make it easier for you to get an understanding of how the algorithm works and how you can use it to generate the caves.

##### Share on other sites

algos that carve out spaces from a level full of solid tiles often yield good results.

open spaces:

sort of a random splat type algo.  generate a random location and radius, set those tiles to clear. repeat as necessary.  different sizes and combos of sizes will give different results. you can also limit where you splat, big splats near the middle of the level, followed by smaller spats all over, that kind of thing.

hallways:

chains can also be used. generate two random locations, and do a bressingham's line drawing algo between them, clearing tiles as you go. you can also add a radius, clearing all tiles in a radius as you go. this will generate hallways.

winding hallways:

for winding hallways,  start with a random location, a random direction, and a random distance. move in that direction for that distance, clearing tiles as you go. then generate a new random direction and distance, and move there, clearing tiles as you go. repeat as necessary. again, a randomly generated radius can also be used. there you clear all tiles in a radius as you move in the random direction for the random distance. you can also generate a random radius at each tile along the path, to give the winding hallway a variable width.

fixing up the map when your done:

set all the tiles along the four edges of the map to solid.  place an entrance at an edge, and clear a hallway from the entrance towards the center of the map big enough to guarantee it will always connect with the randomly generated interior space on the map.

##### Share on other sites

A friend of mine had decent success by throwing random "void" metablob spheres and extracting an isoline from them. Looked quite splotchy to me but I would say maybe his sphere randomizing algo was too high.

+1 for noise. A 2d cave can be generated "simply" and it extends simply to hallways and straight lines as Norman proposes by having a base and height to be perturbed.

##### Share on other sites

what do you think about pre create 20 or 30 types of caves on arrays and just spawn theys on the terrain and clean all the tiles  on the area?

##### Share on other sites

I suppose. But to be honest that sounds like a lot of work when there are algorithms out there (Perlin Noise, but there are others) that can create a very unique, and variable landscape (including vast caverns) that look good, and only need to be coded once, as opposed to you having to statically fill in 20-30 separate arrays for a comparatively small amount of variation.

Marcus

##### Share on other sites

Some people recommend cellular automata for cave generation (as in Conway's game of life, such as e.g. this implementation) I personally have not been able to get them to do something that I really like (maybe, possibly, probably even, my implementation was bugged?), but a lot of proponents of the method seem to agree in unison that it is vastly superior to noise-based algorithms.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015832
×