# Render rivers in a grid

I have created a random height map and now i want to create rivers. I've made an algorithm based on a* to make rivers flow from peaks to sea and now i'm in the quest of figuring out an elegant algorithm to render them.

It's a 2D, square, mapgrid. The cells which the river pases has a simple integer value with this form :rivernumber && pointOrder. Ie: 10, 11, 12, 13, 14, 15, 16...1+N for the first river, 20,21,22,23...2+N for the second, etc. This is created in the map grid generation time and it's executed just once, when the world is generated.

I wanted to treat each river as a vector, but there is a problem, if the same river has branches (because i put some noise to generate branches), i can not just connect the points in order.

The second alternative is to generate a complex algorithm where analizes each point, checks if the next is not a branch, if so trigger another algorithm that take care of the branch then returns to the main river, etc. Very complex and inelegant.

Perhaps there is a solution in the world generation algorithm or in the river rendering algorithm that is commonly used in these cases and i'm not aware of.

Any tips? Thanks!!

• I guess you could use some algorithms used for maze generation and just cut some egdes to create a more realistic river map. – Ali1S232 Jun 29 '11 at 16:21
• I have the river path, but do not know how to handle branches when rendering them, connecting points. – Gabriel A. Zorrilla Jun 29 '11 at 16:45
• Have a point at the confluence (meeting point of two rivers) which is treated as a special kind of node? – The Communist Duck Jun 29 '11 at 17:34
• I once saw an article about createing how to render roads on a tilemap. It selected tiles based on which neighbor tiles also have road property on. it created something around 16 diffrent tiles( including all the rotations). this step was done only during scene generation. don't know if you could do the same thing or not. – Ali1S232 Jun 29 '11 at 18:35
• Can you give us a screenshot with some sketches over it to better show what you are trying to do? – AttackingHobo Jun 29 '11 at 19:38

## 2 Answers

This tutorial/game dev blog provides an excellent method for generating rivers, and they render very nicely. I'm not sure if this can be applied effectively in your situation, as you've taken a different approach (and have a grid-based map) but it's worth looking into, for sure.

• That's really nice. – Tim Holt Jul 6 '11 at 22:12
• More details that may be useful: Rivers in that project flow from mountains to the oceans. The volume increases as it flows downhill, and rivers that join together add their volumes. The line width on the map is the sqrt of the volume. Every location in the river points downhill; a fork in the river means two or more locations point to the same downhill spot. I think this could be used on a grid as well. – amitp Aug 10 '11 at 0:39

One thing you could do is have each individual node handle itself. Make a pass over it when after generation but before displaying and give each river square a list of entrances (lower number, same river) and exits (higher number, same river).

From this data you should be able to figure out where the entrance(s) and exit(s) are (storing relative location would probably help) and you can from there render that square's river based on the connections. The simplest way to do a branch is to connect the entrances/exits to the center of the square.

The graphical representation of this could be taken many different ways, and you haven't given enough information about it's use for me to recommend one directly; so instead I will just mention a way involving a particleish system where each segment of water flows dynamically "downhill".

Have the root square start some number of "water pieces" at each of it's entrances and have them randomly decide from the available exits at creation. When it hits the exit, spawn one at the tile it hit's entrance and return the water piece to the start of it's square. Since it has already spawned into the next square (only do this once per piece in a square), and you already know the settings and calculations (don't re-randomize), you can let this piece loop in it's square with very little calculation, and if you wanted, likely using a shader.

This has the advantage of also letting your rivers go to smaller rivers after branches, and the two resulting rivers do not have to be the same size. It will take a moment for the river to populate fully from the top to the bottom, but the resulting effect should be rather pleasant as you could end up with something like this simply based on which nodes have rivers.

10--10--6--6--2--2
|      |
4--4   4
|   |
4---6--6--6
|
2


It would also, depending on seed and code, have the potential to look different each time you ran it, even if the node placement was the same.

Hope this example is somewhere along the lines you are looking for.