r/howdidtheycodeit • • Jun 30 '22

Question How does Minecraft generate terrain split into chunks?

I understand the idea of Perlin noise. I also feel like I get the gist of random tree placement. Caves seem tricky but I could still understand, maybe making some kind of sphere move about underground that carves a cave out.

What I DON'T understand AT ALL is how the generation is done a 16x16 chunk at a time. How are all these things, Perlin, continuous caves, etc., done in such small blocks at a time, and then connect together seamlessly?

I could understand, a lot more simply, how to generate a finite world using Perlin noise. Making the world infinite and doing it a chunk at a time makes it a lot more difficult seeming, how is it done?

42 Upvotes

11 comments sorted by

View all comments

Show parent comment

2

u/ZorbaTHut ProProgrammer Jul 01 '22

You need to be able to fully generate a chunk using only the random seed, without having to generate other chunks first.

Actually, a bit of refinement: you need to be able to fully generate a chunk using a finite amount of generation.

For example, let's say I decide I have a terrain-generation system (fully local) and a tree-placement system (requires terrain generated on all adjacent chunks). This is still doable - I can generate terrain in a 3x3 square, then place trees on the center part. Then when I need to finish up one of the half-generated chunks I can just extend the terrain a bit further and use that new information to place trees.

You can go arbitrarily far with this as long as it's still finite - say, if you decide that moving-sphere-cave-carving spans at most six chunks, then now you need to generate a six-chunk-radius area, which may or may not be a problem.

1

u/Syracus_ Jul 01 '22

While that's technically feasible, you'd have to be very careful about recursion though, which can be tricky with random procedural generation.

There's also the issue of performance.

1

u/ZorbaTHut ProProgrammer Jul 01 '22

Yeah, the "finite" part is critical, otherwise it's possible to end up attempting to generate the entire map from a single tile.