- Home /
Adding rivers to procedural generated 2D map
I'm using a noise function to procedurally generate a tile map, I was wondering if there is any way to extend what I'm already using to add the ability to create rivers to the map generation.
Currently I'm getting this:
And I'd like to find a method of doing this: Where large bodies of water are connected to one another as if they were a single continuous flow.
I have a Noise class that makes the values for the map.
public class Noise {
int seed;
float frequency;
float amplitude;
float lacunarity;
float persistance;
int octaves;
public Noise (int seed, float frequency, float amplitude, float lacunarity, float persistance, int octaves)
{
this.seed = seed;
this.frequency = frequency;
this.amplitude = amplitude;
this.lacunarity = lacunarity;
this.persistance = persistance;
this.octaves = octaves;
}
public float[,] GetNoiseValues(int width, int height)
{
float[,] noiseValues = new float[width, height];
float max = 0f;
float min = float.MaxValue;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
noiseValues [i, j] = 0f;
float tempA = amplitude;
float tempF = frequency;
for (int k = 0; k < octaves; k++) {
noiseValues [i, j] += Mathf.PerlinNoise ((i + seed) / (float)width * frequency, (j + seed) / (float)height * frequency) * amplitude;
frequency *= lacunarity;
amplitude *= persistance;
}
amplitude = tempA;
frequency = tempF;
if (noiseValues [i, j] > max)
max = noiseValues [i, j];
if (noiseValues [i, j] < min)
min = noiseValues [i, j];
}
}
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
noiseValues [i, j] = Mathf.InverseLerp (max, min, noiseValues [i, j]);
}
}
return noiseValues;
}
}
And in my main class the map is created with the following function:
float[,] noiseValues = noise.GetNoiseValues (width, height);
for (int x = 0; x < Width; x++) {
for (int y = 0; y < Height; y++) {
tiles[x,y] = new Tile(x, y);
if (noiseValues[x,y] < 0.2f) {
tiles [x, y].Type = TileType.Shallow;
} else if (noiseValues[x,y] < 0.4f) {
tiles [x, y].Type = TileType.Grass;
} else if (noiseValues[x,y] < 0.55f) {
tiles [x, y].Type = TileType.Scrub;
} else if (noiseValues[x,y] < 0.6f) {
tiles [x, y].Type = TileType.Marsh;
} else if (noiseValues[x,y] < 1f) {
tiles [x, y].Type = TileType.Bare;
}
}
}
I'm very new to Unity and C#, I've done some reading around generating biomes but the math is somewhat above my head. Is it possible to extend what I have to accomplish river generating? Or could I use something like PathA* to join two points on a map to get the result I want? Thanks!
It's mostly a math problem I figure. I also figure that you will be able to find several example mathematical equations that simulate a river with bends.
Answer by ThibaultCoutaz · Dec 22, 2017 at 10:23 AM
I think A* would do perfectly the work for you
to train PathFinding with A* i made a 2 grids with some wall (in your case the wall can be the black pixel )
Picture explication :
Black pixel = wall; White pixel = free Space where the way can pass. pink pixel = free Space visit by the A* yellow pixel= Wall visit by the A* blue/cyan pixel = Way to go from A to B Red pixel = Point B : End Green Pixel = Point A: Start
Your answer
Follow this Question
Related Questions
2d tilemap with biomes and perlin noise 1 Answer
Advice on 2D Surface Terrain Generation Using Noise 1 Answer
Better perlin noise map generation. 0 Answers
How do I make an interactive map on Unity? 0 Answers
2D Animation does not start 1 Answer