Skip to main content
GameDev.net gamedev.net
🔒 Locked

2D terran generating

Started by Ilankt Mar 28, 2009 at 7:34 AM 3 replies 1.4k views
Original Post
Ilankt
Ilankt
I'm trying to randomly generate a 2D terrain (in worms-like 2D games) but so far I'm not pleased with my result. what I do is randomly generate points, and draw lines between them. I tried to limit the delta between 2 near points, increasing\decreasing the distance between points, and still, I get rough, edgy results. is there a way to "smooth" the results? Thanks! here is the code:

    m_Points[0].x = 0;
    m_Points[0].y = hge->Random_Float(200,250);
    for (int i=1; i<Length; i++)
    {
        m_Points.x = i * 8;

        newy = hge->Random_Float(200,400);
        while (abs(newy-m_Points[i-1].y)>4)
        {
            newy =  hge->Random_Float(200,250);
        }

        m_Points.y = newy;
    }
here is a URL for the screenshot. http://www.largeimagehost.com/img/new-gallery-name/ehQsUhU/example.JPG.html
How appropriate, you fight like a cow!
Molle85
Molle85
You can use a variation of Perlin Noise to get a smooth landscape
eppo
eppo
You could draw a Catmull Rom spline between all points. That should give some nice curves.
I also remembered this thread.
Wan
Wan
First of all, you don't really need the while loop, you can generate a random number in the range (based on the previous point) you want directly. At least if the arguments of Random_Float mean what I think they mean (lower and upper bound of the random number generated).

For smoothing, you can just take some average of nearby points to the left and the right and modify the point's height. Or you can initially generate less points and add vertices inbetween them by means of some form of spline interpolation.
Ilankt
Ilankt
Ok, thanks guys, I'm think I'm going to try what WanMaster said about taking the average points near a point.
I just dont have the mathematical background or the time to learn it to implement splines or noise...
I dont want to complicate it too much, just having some fun :)
so what I thought to do is to pass after the initially generated terrain on all points, and check that the average delta is not too big...
well, I'll hope I could think of something.
Thanks!
How appropriate, you fight like a cow!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.