Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
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  
  [SOLVED]Random Generated 2D world  (Read 1962 times)
0 Members and 1 Guest are viewing this topic.
Offline Rakso

Junior Member


Medals: 1



« Posted 2013-05-02 09:18:35 »

I got inspired of the small game "MiniCraft".

How is world randomly generated by code?

Example: I have a world which is the size 2000,2000
How can i make by code that for example trees are randomly placed around on this world?

Im thinking using nested for loops to make it go place on random locations, But also if i manage to for example 10 trees gets randomly placed on the map how do i make not something else spawn where the tree spawns.. Maybe an if statement that checks (array[posx][posy]==0) and then its ok to place for example a rock here?
Offline HeroesGraveDev

JGO Kernel


Medals: 239
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2013-05-02 09:24:08 »

Look into java.util.Random

There are two basic ways to randomly populate a world with trees.

-Use random numbers to find the positions to place trees.
-Random chance to place a tree for each position.

Each has opportunities for more randomness.

Other options include smooth noise functions (Perlin & Simplex etc.) with a threshold to decide tree/no tree.

Offline Rakso

Junior Member


Medals: 1



« Reply #2 - Posted 2013-05-02 10:04:51 »

Alright thank you,
I looked through the java.util.random but i dont see any method to get a random int between a certain number..

Lets say i want an int between 0 and 5000.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Roquen
« Reply #3 - Posted 2013-05-02 10:31:12 »

nextInt(int)

For not explicitly storing the location of all random placeables look here: http://www.java-gaming.org/topics/uniform-feature-points/28000/view.html
Offline Rakso

Junior Member


Medals: 1



« Reply #4 - Posted 2013-05-02 12:34:36 »

I mean how to make nextInt(int) only give back a fixed number for example:
I only want a random number between 0-100..
Offline RobinB

JGO Ninja


Medals: 44
Projects: 1
Exp: 3 years


Spacegame in progress


« Reply #5 - Posted 2013-05-02 12:39:48 »

I mean how to make nextInt(int) only give back a fixed number for example:
I only want a random number between 0-100..

Thats what he sayd.
int value = nextInt(100); for values between 0-100
Offline Rakso

Junior Member


Medals: 1



« Reply #6 - Posted 2013-05-02 13:01:21 »

Oh wow, Sorry i didnt understand that.

So nextInt(100) as an example would give me a number between 0-100 or could it give me negative numbers too?
Offline ctomni231

JGO Wizard


Medals: 98
Projects: 1
Exp: 7 years


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


« Reply #7 - Posted 2013-05-02 13:02:15 »

It'll give you values from 0 - 100... no negative values Wink

Offline Rakso

Junior Member


Medals: 1



« Reply #8 - Posted 2013-05-02 13:12:00 »

Great! Big thanks to everyone!!
Offline Roquen
« Reply #9 - Posted 2013-05-02 13:34:35 »

nextInt(100) will return values between 0 and 99...so 100 possible results.  (Hint: read the documentation..if you're using an IDE it should automagically display it for you)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline rvandenberge

Junior Member


Projects: 1



« Reply #10 - Posted 2013-06-30 21:01:25 »

for values between 0 - 1 you could use .nextBoolean()  Wink
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.

CogWheelz (16 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

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

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

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

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

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
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!