Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Random Terrain Generator Question  (Read 649 times)
0 Members and 1 Guest are viewing this topic.
Offline _Scyth_

Junior Member





« Posted 2013-05-27 11:52:51 »

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.
Offline Roquen
« Reply #1 - Posted 2013-05-27 12:07:02 »

Toward the upper right hand side of the forum is a text box with a button next to it labeled "search".   Pointing
Offline _Scyth_

Junior Member





« Reply #2 - Posted 2013-05-27 12:33:31 »

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline dermetfan

Senior Member


Medals: 11



« Reply #3 - Posted 2013-05-27 13:12:20 »

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.

Offline Roquen
« Reply #4 - Posted 2013-05-27 13:28:04 »

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.
Offline opiop65

JGO Kernel


Medals: 153
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #5 - Posted 2013-05-27 13:29:54 »

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.

Offline _Scyth_

Junior Member





« Reply #6 - Posted 2013-05-27 13:39:14 »

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
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #7 - Posted 2013-05-27 15:41:10 »

Calculate one value for the x coordinates (height).

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

JGO Wizard


Medals: 98
Projects: 1
Exp: 7 years


Not a glitch. Just have a lil' pixelexia...


« Reply #8 - Posted 2013-05-27 20:49:32 »

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.

Offline _Scyth_

Junior Member





« Reply #9 - Posted 2013-05-27 21:44:27 »

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #10 - Posted 2013-05-27 23:57:50 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
        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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (36 views)
2014-07-24 01:59:36

Riven (37 views)
2014-07-23 21:16:32

Riven (25 views)
2014-07-23 21:07:15

Riven (27 views)
2014-07-23 20:56:16

ctomni231 (57 views)
2014-07-18 06:55:21

Zero Volt (49 views)
2014-07-17 23:47:54

danieldean (39 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!