- Home /
Detecting need of generating terrain (Minecraft-like) [Perlin Noise]
Hey! I've got a problem and only unsatisfactory solutions come to my mind. Ok, let me explain this.
I am using Perlin Noise to generate the terrain. I want to spawn the terrain blocks piece by piece (just like Minecraft does). I don't want to instantiate big terrain at once, because on each piece I will make calculations to make it more differential. How can I detect, if I need to generate particular part of terrain at given moment?
Imagine yourselfs the terrain divided into squares. How can I check if I need to instantiate each of squares?
Here's my not-exactly-solutions: a) Four variables (north, east, south, west) for keeping informations, how far player went in given direction and if it is less that current position (+ draw distance) I am spawning chunk. Defect of this method is, I can generate terrain only in four directions, I cannot detect anything between that (it looks like +).
b) Triggers on the terrain borders detecting player collider. When the trigger is fired, I am generating terrain part behind it. It is hard to explain with my english skills... Defect is, each square part of the terrain can be generated by touching four surrounding borders, so it might generate terrain even four times.
Have you got any sugestions or ideas?
Answer by Cherno · Sep 13, 2015 at 12:01 AM
Your first suggestion is the right way. I see no reason why your terrain generation should be limited to a cross shape. All you need is some simple code that tells you which chunks are to be generated and which are to be destroyed depending on the player's new chunk position.
X = existing chunks P = Player walking northward N = new chunks D = destroyed chunks
XXX
XXX
XXX
NNN
XPX
XXX
DDD
Corrent me, if I am wrong.
So I have to keep all the instantiated chunks in the two-dimensional array (or any other container) of game objects. When the player will reach the Draw Distance, I must Destroy chunks behind him and Instantiate chunks in front of him.
I am not sure if searching the array is good and fast solution. I may be wrong of course.