# How do I generate a random curve for landscape (like Worms)? [duplicate]

I must build random curve line for the 2D Game on the BitMap (like in Worms, from the side).

Teacher said that I should do it using Terrain Generation through recourcy (I work in Delphi 7). I understand the main principle, but I don't know how to introduce it as code.

All measurements according to the screen resolution.

That depends on how the curve should look like.

The easiest solution that come to mind is to generate a few sine or cosine curves (I am sure delphi provides the required methods) and add them to each other. Then take a part of the resulting curve and you got your random line.

Another approach would be to randomly select a few points on the screen and calculate a spline that fits those points. How ever this is more difficult to do. For starters I'd go with the sine or cosine lines. This should be fairly simple to do.

• the second is closer... to build, using Random Fractal 2D Terrain Generation... To take the random ground with hollows and hills... random, but in the range: (clientheight div 4)+random(clientheight div 2); something like that...
– Stas
Apr 4, 2012 at 15:56
• Through the sine, cosine, tangent, cotangent, and the integrals I did ... Prof rejected them.
– Stas
Apr 4, 2012 at 16:05
• Maybe i should use the Midpoint Displacement Algorithm? but I don't know how to do it in delphi.
– Stas
Apr 4, 2012 at 16:57
• Stas: Midpoint Displacement (or a variant of it) is certainly the way to go. When you say 'I don't know how to do it in delphi', what portion don't you understand - the algorithm, or the language? If you can do it in any language, you should be able to do it in delphi; it doesn't need any particularly advanced language features... Apr 4, 2012 at 18:01

I don't know how worms did their terrain generation but there are many ways to generate terrain.

Midpoint works by taking a straight line, and pulling the center up or or down a random height. Then split the line into 2 and pull the centers up or down a smaller random number. Then split the line into 4, 8, etc etc.

Perlin Noise is a bit tricky to explain, but in essence makes black and white clouds which you can use as a height map. However recommend reading it, it serves well in all things procedural.

Faulting is similar to midpoint, you pick a random point on your terrain, raise one side, lower the other side, keep doing this until happy :)

I think L-Systems would be the best for this, abit trick again to explain but I'll give it a shot. L-Systems work with Strings and Rules. So if 'A' represented a flat line. I could have a rule that said make every 'A' 'AABB', so our string becomes 'AABB', then the next rule might be make every 'AB' 'BCB' in which case our string would become 'ABCBA'. Each letter would represent something in you landscape, which would generate the overall picture. (Bad explanation sorry.)