Random Terrain Generator Question

Ok, so I want to be able to create a random terrain in a 2D game.
Now I know that I can store the values into a multi-dimensional array, with different numbers meaning different things, for example, 0 = air, 1 = stone.

I could also probably use a loop and a random to generate these values.

However, I want it to follow a set of specific rules, because I don’t want to have floating bits of stone in mid air, so I want the first couple of rows to be air and I want the last couple of rows to be a mixture of stone and possible ores.

How can I go about doing this in Java2D?
I don’t need to know about rendering it, just actually generating it to follow pre-defined rules.

Toward the upper right hand side of the forum is a text box with a button next to it labeled “search”. :point:

When I use it, I can not find the answer to the part that I am looking for help with, getting it follow guidelines, whilst making it somewhat random

You could set a maximum distance between a type of material and the another. For example, if there is stone on y = 3, you don’t want a stone on y = 5 in the next column if there’s air on y = 4.
Generate a random material for that position and then check if it’s ok to be there, if not, generate another one as often as needed until the random material fits the requirements to be there.
I’ve never generated random terrain though, probably really better have a look at the existing topics.

I’m not blowing you off…but in that case you’re going to be better off doing a web-search. You’ll get to info faster for multiple reasons…one you don’t have to wait for people to reply, you’ll get a bagillion hits instead of a half-dozen vague responses and most importantly you’re likely to find something that vaguely fits the look you’re going for. Like you say “2D”, but that doesn’t really narrow the problem down much.

One possible solution is to use some combination of noise…most likely connected in a fractal brownian motion configuration to give you a 2D height field, then filter than information to determine your tiles. Slightly more involved would be to lay-out some base features and used a domain deformation in conjunction with the fBM.

I’m just going to say this. You need to know basic Java before you move onto game programming. What you’re asking has to do with checking a value in an array, its something that everyone, besides newbies, knows how to do. If you need to ask this question, you really need to find a book or an online tutorial and read it.

To answer your question. Just check if the ‘y’ value in the 2D array is equal to a certain number (like 6). If the ‘y’ is at that value, do not let stone tiles spawn, only air. Simple. And look into noise generation, like perlin noise.

And please, Google and tutorials. Please.

Opiop, its not the checking of the array, I have done tiled maps before and anyway I think I might have just figured out the solution to my own problem, I would just need someone to confirm how practical it would be.

With help from what you said dermetfan,

I think I should be able to check the y values of the map, and then when I loop through them I could have a set of if statements, and depending on the y value, I can have the random only work between a certain range of values, so for example the bottom layer will have no air so I could have a range of values 1 - 5 that can be selected randomly

Calculate one value for the x coordinates (height).

Then loop between all y coordinates below this value to set the material.

So you are creating terrain for a 2D game, and you want it to generate randomly, and make sense in the real world when it does.

If I were you, I’d just apply gravity to the particles I create. In other words, I’d let them appear all over the screen, then apply gravity to them until they reach a hard surface. (The ground, or floating pieces of stone). (For added realism, I’d then look at the y coordinate and any pieces floating in mid- air I’d use to make caves.)

If you want a more human-like terrain, you are probably going to have to look into using Perlin-Noise to control create the locations of the terrain and the chances of a certain terrain appearing more controlled.

using the gravity method wont work along with my guidelines technique, and i’ll need to google the other way

Im currently also creating a generated 2d terrain.
For me this code works really well:


        for(int px = 0; px < SIZE; px += 1){
            float val = (450 + gen.noise((x+px)*0.001f, 0, 0, 5, 3, 0.3f) * 200);
            int starty = (int)val - y;
            if(starty < 0){ starty = 0; }
            
            for(int py = starty; py < SIZE; py += 1){
                float terraintype = SimpleMath.fastAbs(gen.noise((x+px)*0.001f, (y+py)*0.001f, 0, 5, 3, 0.4f))*10 + 15 - ((y+py))*0.02f;
 
                [snip]

                m = terraintype <= 0 ? TerrainMaterial.DIRT : TerrainMaterial.GRASS;

                [snip]
        }

  • As you can see im using an system with chunks, this code generates exactly one chunk.
    **gen is just my SimplexNoise implementation, you could jsut use Rand() for testing, if you interpolate the values.