- Home /
Generating terrain radially
So I've been spending a few hours now looking for an algorithm to help me radially generate terrain. All I've been able to find is articles of people asking how to generate terrain like in Terraria, however, that's not what I'm after
Specifically I'm going to use this to random generate asteroids of various sizes, with various minerals and such. From looking at the above mentioned articles, I've been able create Terraria like terrain using the following code. Which is a slightly modified version of the popular tutorial found here
GameObject Stone = (GameObject)Resources.Load("Stone");
GameObject Grass = (GameObject)Resources.Load("Grass");
byte[,] blocks = new byte[x, y];
for (int px = 0; px < blocks.GetLength(0); px++)
{
int stone = NoiseGenerator.Noise(px, 0, 20, 20, 1);
stone += NoiseGenerator.Noise(px, 0, 50, 30, 1);
stone += NoiseGenerator.Noise(px, 0, 10, 10, 1);
stone += 25;
int grass = NoiseGenerator.Noise(px, 0, 100, 35, 1);
grass += NoiseGenerator.Noise(px, 0, 50, 30, 1);
grass += 25;
for (int py = 0; py < blocks.GetLength(1); py++)
{
if (NoiseGenerator.Noise(px, py, 12, 16, 1) > 10)
{
Instantiate(Grass, new Vector3(px, py), Quaternion.identity);
}
else if (py < stone && py > -stone /*&& NoiseGenerator.Noise(px, py*2, 16, 14, 1) < 10*/)
{
Instantiate(Stone, new Vector3(px, py), Quaternion.identity);
}
else if (py < grass && py > -grass/*&& NoiseGenerator.Noise(px, py * 2, 16, 14, 1) < 10*/)
{
Instantiate(Grass, new Vector3(px, py), Quaternion.identity);
}
}
}
What I can't for the life of me wrap my head around, is how would one modify this to create an asteroid-like shape.
Thanks in advance :)
Answer by VesuvianPrime · Sep 14, 2014 at 03:47 PM
I suppose one way of generating random asteroid shapes would be to create a map and repeatedly draw random circles to it:
Create a 2d integer array the size of your asteroid. Zeroes will be empty space, Ones will be filled with tiles.
Get a random number of circles.
Calculate the maximum radius for a circle to fit in the array.
For each circle: 5. Calculate a random radius up to the max radius 5. Calculate a random position such that a circle of given radius will fit in the array. 6. "Draw" the circle to the 2d array (for each pixel of the circle, set a 1 to the array), looks like this should work: http://stackoverflow.com/questions/1201200/fast-algorithm-for-drawing-filled-circles
You should now have a 2d array of 1s and 0s marking the shape of your asteroid. You can use this as a lookup table in your tile generation to see if a tile should be created or not.
Thanks, that worked out great! $$anonymous$$uch less painful than I thought it wouldn've been :)
If anyone is interested in the code I got from this, here it is;
GameObject Stone = (GameObject)Resources.Load("Stone");
int[,] intArray = new int[ArrayX, ArrayY];
for (int y = -Radius; y <= Radius; y++)
{
for (int x = -Radius; x <= Radius; x++)
{
if (x * x + y * y <= Radius * Radius)
{
Instantiate(Stone, new Vector3(x, y), Quaternion.identity);
}
}
}
Thank you $$anonymous$$ :) simple and great ;)
Aha! You were just making one circle! Well, I'm glad you figured it out. Nice work!
Thanks, if you don't $$anonymous$$d me asking, could you help me out a bit with the perlin noise? I'm practically reusing the exact same method as I used to, which is giving me symmetrical distortion.
If I were to ditch the old 2D perlin noise, how should I approach a new method, specifically for circles? After doing a bit of research I saw that someone used a 1D algorithm - is this preferred when dealing with circles, rather than terrain?