Generate 2D map while the player moves

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PerduGames
:warning: Old Version Published before Godot 3 was released.

I implemented this recently:

and I’m kind of lost in what is the best way to generate the map while the player moves, because I think of a really big map, could not generate it at once, and could not also create an array with its data by example. So I thought,

1 - Start the game, check the position of the player, and paint the cells in the tilemap around the position of the player, type a square around it 32x32.

2 - The player moves, and check to what direction it is moving, if it passes the half of the 32, I would paint another 16 cells in the direction that the player is going and clean the other 16 behind.

Is this the best way to do it?

and another thing, the world needs to have a fixed size, even if it is large, to be able to bear the matrix that will generate it, how would you deal with it and at the same time with the above question?

:bust_in_silhouette: Reply From: Zylann

This kind of problem is being asked/solved over and over again, it all boils down to segmentation at multiple levels: visual, physical and data segmentation.

The simplest segmentation you can do in 2D is to represent your world as a grid of square chunks, as you already thought. Then you store loaded chunks in a 2D array, or in a dictionary with Vector2s as keys (usually in a scaled-down “chunk coordinate system”, so this way their coordinates are contiguous).
Then, regularly check the chunk “slots” in a range around the player. If some aren’t present, load them, and make sure chunks outside of the range get unloaded. It can be optimized by detecting when the player crosses the boundaries of its current chunk.

I said there are multiple levels of segmentation: the simplest is to mix all of them, so 1 chunk of data, visuals and physics are all in the same object, with same grid size. But that’s not mandatory, you could have the whole map loaded in data, but only instance part of it as visual/physic chunks. It also depends which nodes you use, which technical limitations you need to solve (like neighboring). If your map is finite and small enough to fit in memory, you could have all data loaded with no segmentation. If you are unsure of the size, or if it’s infinite, everything must be segmented.

More complex segmentation involves quadtrees or LOD grids, but in 2D you might not even need that. They would be used more likely in perspective 3D because you can see at many orders of magnitude (from flower near you to mountains in the distance), while in 2D or ortho you see a fixed range regardless of where you look at.

I could not express myself very well, the problem is that the array would be too large to be created at once just with data, I’m trying to get into a form, that I just need to hand part of the array, eg if it is an array 16 000x16,000 I will, 32x32 of her, at the same time that she is created only 32x32, and not cirando her all at once.

Taking the example of minecraft, it does not create a matrix first and then it does it, I imagine it will create pieces of the matrix, because it is huge, it would never be possible to create it at once, even just in data. Well, how many millions of noise could it take for hours? How was it done?

PerduGames | 2017-09-08 13:24

That’s what my answer is about. Chunking a grid in 32x32 smaller grids is what Minecraft does, although in Minecraft data segmentation is 16x16x256.

Because data is segmented, it’s also generated on the fly, not in advance, and all is dependent on a seed (no random anywhere, and if there is, it’s seeded). There is litterally somewhere a code that takes an empty chunk, fills it with what it should contain, and returns it. There is no “bigass array” anywhere, all is chunked, so your generator has to be able to work on pieces of world instead of having to know the whole picture.

Now, if what confuses you is the actual noise algorithm, you need to come up with a system that doesn’t require a fixed-size array. Well known algorithms do this, like Simplex, OpenSimplex or Perlin noises.

Zylann | 2017-09-08 19:46

It was no problem with noise algorithm, the problem I was having was with the logic of the algorithm that generates the terrain, but I got it here. Now I only need to have the seed, all matrix and world is generated to so many, I am still implementing the part of the player walking and generating, but is walking, there are some issues to deal with. Thanks for the help.

PerduGames | 2017-09-08 22:38