Tile based river generation
I have made a map generation in my tile based game. It almost exactly does what I want, except for 1 thing. The rivers look like this:
The problem with this is, it's not realistic. It are just some spots random over the world. I want to make a realistic river generation. Which would include different sizes (between 5 and 20 block wide), branches, lakes and different depths. What is the best way to do that?
Here an example from Simcity 4 of what I mean with realistic rivers:
This is my current code:
waterOffsetX = Random.Range (10f, 300000f);
waterOffsetX = Mathf.Round (waterOffsetX) + 0.5f;
waterOffsetY = Random.Range (10f, 300000f);
waterOffsetY = Mathf.Round (waterOffsetY) + 0.5f;
for (float x = -gridX; x < gridX; x += 1)
{
for (float y = -gridY; y < gridY; y += 1)
{
Vector3 spawnPos = new Vector3 (x, y, 0);
float perlinWater = Mathf.PerlinNoise (x * seedWaterX + waterOffsetX , y * seedWaterY + waterOffsetY);
if (perlinWater > perlinThresholdWater) {
GameObject waterTile = Instantiate (water, spawnPos, Quaternion.identity) as GameObject;
}
}
}
Values:
seedWater (X & Y) = 0.1
Grid (X & Y) = 50
perlinThresholdWater = 0.6
@DutchGames I would personally make a "fake" heightmap and then start at the highest point and add the next lowest point/neighbour to a list, if there are several that are approximately the same I would start a new iteration from each (branching). Water flows down using (usually) the path of "least resistance" and reinforce the path (e.g. erosion of a mountain).
There might also be some kind of fractal-noise that produces good rivers. Look up LibNoise for Unity and Libnoise website.
@DutchGames Here is a mountain-range I recently made using LibNoise. To produce this mountain I use a ridged multifractal noise with low frequency as the base, then I add a higher frequency perlin noise to create the peaks within the bounds created by the base. On top of this I add a a higher frequency reversed (multiplied by -1) ridged multifractal noise to create recesses in the mountain.
Although this is in 3D it should give you some hints on were to start with your heightmap. Then use some kind of hill-climbing algorithm to get your path for the water in a 2D space.
Wouldn't that cause some severe lag? I want my game to generate areas while playing.
@DutchGames Then its a whole other problem :P Although, you could try with a reversed (multiplied by -1) ridged multifractal from Libnoise and create water were the output is greater than 0. The recursive hill-climbing might not take that long though, and you could always limit it. I also noticed that a single ridged multifractal noise is enough to create good looking mountains. Remember that Deep Blue could evaluate 2 million positions per second and an A* pathfinding algorithm of $$anonymous$$e could look through 10,000s of nodes in a matter of milliseconds. Given that you will just create a primary path with the hill-climbing it shouldnt be a problem.
Otherwise it's probably better to just code your own algorithm for creating rivers and lakes.
@DutchGames Also, if you don't do it already: Look up how to use chunks and only load/create visible chunks and pool your gameobjects.
@DutchGames To sum it up, this is how I would do it: Create your area using nested for-loops and make a height map for the area using Ridged $$anonymous$$ultifractal, here we should keep track of the highest point and it's coordinates.
When that's done, take the highest point and start with the hill-climbing. The hill-climbing is basically: Add the current coordinate to a list of nodes, get all the neighbours of this node. Pick the neighbour with the smallest height-value according to our height map. Do this recursively until either all neighbours have higher or the same height as the current node or until a preset amount (say 50 nodes).
But that's just how I would do it :) Depending on the size you're trying to create and how long you want your rivers this can take some time.