RSS

Random 2d Terrain

06 Sep

About a week ago I started wondering how random levels are achieved in games like Tiny Wings. The terrain in these games are basically just smooth rolling hills that can go on forever. After learning about some basic ways this could be implemented but after a few days of searching I couldn’t find any publicly available samples or tutorials that did this in Unity.

As a result I set out to develop this functionality myself 😀

The Plan

After doing a lot of research into how this could be achieved I came up with what I thought was a pretty simple plan…

Generate Base Points:

First a list of (x,y) coordinates are generated randomly. To do this I generated a number of random values for the y coordinates and made the x coordinates automatically by adding a set amount each time.

This resulted in a list of evenly spaced y coordinates.

Interpolating the Points:

These random coordinates were a good starting point but they don’t give me the smooth look I’m going for. The solution to this problem is to interpolate between these points. this process basically generates a set amount of points between the existing ones in order to generate a smooth curve.

I got the algorithm to do this from http://paulbourke.net/miscellaneous/interpolation/

The function is as follows

double CosineInterpolate(
   double y1,double y2,
   double mu)
{
   double mu2;

   mu2 = (1-cos(mu*PI))/2;
   return(y1*(1-mu2)+y2*mu2);
}

This function takes the 2 y values you are interpolating between (y1 and y2) as well as the location bewteen the points we want to get a value for (must be between 0 and 1). The result is a y value for that x coordinate!!!

Creating the Mesh:

For me this step was the hardest since I have very little knowledge of direct graphics programming. I took the list of coordinates generated from the interpolation and used them to create a 2d mesh of the terrain.

First I made a bunch of vertices. Each coordinate produced 2 vertices (a top and bottom one) which acted as corners of rectangles.

Next I made triangles by assigning which vertices belonged to each triangle. In Graphics programming everything is made out of triangles so for each rectangle I needed 2 triangles.

PRESTO!!!

Unity Web Player  Random 2d Terrain - Google Chrome_2013-09-06_13-16-28I learned a lot from this project and hope to use it someday in a game. I posted information about this as well as a link to the source code on the Unity Forums here in hopes that someone more knowledgeable than I might improve it and so that the Unity community can benefit from it 😀

 

Playable Demo

In case anyone wants to see this project in action. Here is the link to the webplayer demo.

http://forum.unity3d.com/threads/199287-Random-2d-Terrain

Advertisements
 
1 Comment

Posted by on September 6, 2013 in Other Stuff

 

Tags: , , , ,

One response to “Random 2d Terrain

  1. Dave

    September 10, 2013 at 1:55 am

    Interesting post. Makes me wonder. Maybe discreet math is worth something after all.

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: