Creating 2D Chunk Loading.

Started by Setlock, December 12, 2016, 23:18:02

Previous topic - Next topic

Setlock

Hello everyone so far this site has been extremely helpful in helping me to figure out issues with the game im currently making. It is basically Terraria and right now im having an issue. I have a algorithm to make a terrain and it works very well but unfortunately it takes too long to make over 1000 x and y coordinates, so my main question is how would I go about making only certain x and y coordinates generate and when you move it generates them as you go instead of all at once when the game starts. Thanks in advance for your help.

abcdef

Can you show us your generation method for the 1000 x,y coordinates? And also what you define as too long? I can't see 1000 x,y coordinates being that difficult to generate.

Setlock

Random rand = new Random(seed);
                 final float STEP_MAX = 1f;
                 final float STEP_CHANGE = 1;
                 final int HEIGHT_MAX = 100;
                 double height = HEIGHT_MAX;
                 double slope = 2 - STEP_MAX;
                 for (int x = -WORLDSIZE; x < WORLDSIZE; x++) {
                      height += slope;
                      slope += (rand.nextDouble()* STEP_CHANGE) * 2 - STEP_CHANGE;
                      if (slope > STEP_MAX)  slope = STEP_MAX;
                      if (slope < -STEP_MAX) slope = -STEP_MAX;
                 
                      if (height > HEIGHT_MAX) {
                          height = HEIGHT_MAX;
                          slope *= -1;
                      }
                      if (height < 0) {
                          height = 0;
                          slope *= -1;
                      }
                     
                      Tile newTile = new Tile(x*25,(int)height*25,25,25,TileType.Grass);
                      tiles.add(newTile);
                      for(int i = 25; i < 100; i+= 25)
                      {
                              Tile newTile2 = new Tile(x*25, (int)height*25 + i,25,25,TileType.Dirt);
                              tiles.add(newTile2);
                      }
                      for(int i = 0; i < STONEAMOUNT; i++)
                      {
                              Tile newTile3 = new Tile(x*25, (int)height*25 + 100 + i*25,25,25,TileType.Stone);
                              tiles.add(newTile3);
                      }



That code is how I make the word currently and you can vary the stone amount, and worldsize. The reason I say too long is that the for loops take a long time to generate all the coordinates(by a long time I mean like 30 seconds) but in games like minecraft It makes millions of xyz coordinates in less than that time which is what I would like to happen in what I'm making. Basically I want the initial load time to be quicker.

abcdef

So what are the values of WORLDSIZE and STONEAMOUNT?

Kai

You obviously need to know what part/section of your whole world is visible at any given time.
(Minecraft also does not generate the whole world at once).
Once you know what is actually visible, you should only generate the parts that actually are visible. Additionally you should have some sort of "margin" next to the visible parts which you generate in addition such that there won't be any "popping" effects once the visible area changes as the player moves around - and as a result new (now visible) parts of the world need to be generated.

Setlock

The values of WORLDSIZE is normally 500 so that it makes 500 blocks left and 500 blocks right from 0,0 and I do have an if statement already that only draws textures to the blocks if they are in the screen so hopefully I can just tweak that a bit to make it only generate them instead

Setlock

My issue is I dont know how to go about only generating blocks as needed not all at once. I run my the world generation algorithm once at startup then just load textures to the blocks. I cant think of a way to generate as needed, I know its possible but I just cant