TheZiggy
TheZiggy

Reputation: 11

Random, Infinite Terrain Generation

I'm trying to generate random terrain for my game (In Unity3D, C#). All I need is water and grass. No height to it (no mountains or hills.) I'd preferably like to do it by placing individual cubes. On top of that the terrain needs to be infinite.

I've searched every where for even a hint on how to do it, but everything I found had either height to it, didn't use individual cubes (edited the terrain as a whole), or wasn't infinite.

Any help would be much appreciated! :)

EDIT: I'm looking for something somewhat similar to a game called Factorio. Here's a screenshot: http://i.ytimg.com/vi/Uns15OfPWbo/maxresdefault.jpg As you can see there is a big body of water, and a ton of land. I want to create something that randomly does that every time (random bodies of water and random land shapes). Something like Minecraft without all the height.

I've heard about something called Perlin noise, but because of a lack of tutorials and documentation, I can't figure out for the life of me on how to use it to generate random terrain.

Upvotes: 0

Views: 1701

Answers (1)

Pizmovc
Pizmovc

Reputation: 67

Perlin noise would be great for your needs. It generates natural looking random noise. It is infinite in all directions, so basically how it works is you choose a random starting point and use that as a base or your (0,0) point. The you add to that a sampling point. If you pass to it the same sampling point, you will get the same result. All you have to do then is give it different sampling points to get different values and spread out in all directions like this:

for (int x = 0; x < Width; x++) {
    for (int y = 0; z < Length ; y++) {
        terrain[x,y] = Mathf.PerlinNoise(randomLocation.x + (float)x/scale, randomLocation.y + (float)y/scale);
    }
}

where you could then expand your Width/Length in the desired direction based on where the player wants to go or where you want to generate new terrain.

Your terrain[,] would then contain floats from 0 to 1 and the as another comment suggested you'd interpret everything above .5 as land and bellow as water. Then you'd just instantiate a cube at that location, color it blue(water) if terrain[x,y] <0.5 and as brown (land) if terrain[x,y]>0.5 and that would be it. Keep in mind that you can adjust scale to get bigger/smaller islands of land. But it has to be the same for X and Y, otherwise you'll get a stretched looking islands.

Hope this helps or isn't too late :)

Upvotes: 3

Related Questions