Public Group

# Drawing a nice 2D landscape using sine/cosine? How?

This topic is 4859 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm sad to admit it but I'm not very good at math. I learned the basics of sine and cosine back in college (and high school) but I have forgotten absolutely everything about them... I'm assuming I need to use them because I want to create an algorithm that generates a nice 2D lanscape (with nice rolling mountains, etc). This will be used for a Scorched Earth game clone so you have a better idea of the landscapes I'm talking about... Can anybody help me out?

##### Share on other sites
You could try taking the cosine / sine of 2 random numbers and averaging them, make them there height and smooth it, this would create an intresting effect.

##### Share on other sites
I think what you want to implement is Perlin Noise. The math required for this method is pretty simple.

##### Share on other sites
Thanks for the replies!

I'll definitely look into Perlin noise, it seems exactly what I'm looking for.

I'm still a bit confused on drawing sines and cosines. When I calculate sin(180) I get only one number. (-0.80116)

Do I need to create a loop like this: (?)

for x := 0 to WidthOfSurface do
begin
// Draw points from (0,0) to (WidthOfSurface,0)
PlotPointFunction(Round(sin(x) + 1) / 2 * waveSize))
end;

The above algorithm doesn't seem to work properly...

##### Share on other sites
The C/C++ sin() function takes its argument in Radians, not degrees. If x is being incremented by 1 in that loop then you'll notice quite a jump between values. I would suggest incrementing x by 1 degree, or (PI/180.0f) radians.

Also, I have thought about doing something like this in the past, but the one thing that has stopped me was the collision detection. My collision detection is currently Vector-based. Does anyone know what type of collision detection would be useful with randomly generated terrain such as this?

##### Share on other sites
Thanks for all of the help guys! I now have a nice landscape! It looks the same every time, but its still nice :)

I'm now trying to figure out Perlin Noise (without much luck, but I'll keep trying).

Quote:
 Original post by wyrzyMy collision detection is currently Vector-based. Does anyone know what type of collision detection would be useful with randomly generated terrain such as this?

I'm not sure if this is applicable but I'm going to generate my landscape and store each pixel in an array. I'm then going to compare collisions using that. (for example, when I calculate the trajectory for a projectile, I'll find the largest smallest (highest) y coordinate that contains land. the projectile will impact at this point)

I'm really an idiot though, so don't listen to me :)

##### Share on other sites
Ok, I just can't figure out the Perlin Noise...

Here's my code (it's Delphi). According to the Perlin Noise document, I need a random number generator (my IntNoise() function) and a way to smooth out the numbers (my CosineInterpolate() function).

Here's my logic:

1) Generate nice sine wave and store each point in an array of TPoints.
2) Filter my array by using CosineInterpolate on every other x value.

Is this stuff really complicated or am I just stupid? :(

procedure TForm1.DrawSine(i: integer);var x : integer;    points : Array of TPoint;begin  DXDraw1.Surface.Canvas.Pen.Color := clRed;  SetLength(points, DXDraw1.Surface.Width + 1);  for x := 0 to DXDraw1.Surface.Width do begin    points[x].x := x + i;    points[x].y := Round((sin((x) * pi / 180.0) + 1) / 2 * 75);  end;  for x := 1 to Floor(DXDraw1.Surface.Width / 2) do begin    points[x * 2].y := CosineInterpolate(points[x*2 - 1].y, points[x*2 + 1].y, IntNoise(Random(1000)));  end;  DXDraw1.Surface.Canvas.Polyline(points);  DXDraw1.Surface.Canvas.Release;end;function TForm1.IntNoise(x: longint): double;begin  x := round(Power((15 shr 13), x));  result := (1.0 - ((x * (x * x * 15731 + 789221) + 1376312589) and \$7fffffff) / 1073741824.0);end;function TForm1.CosineInterpolate(a, b: integer; x: double): integer;var ft, f : double;begin  ft := x * 3.1415927;  f := (1 - cos(ft)) * 0.5;  result := round(a * (1 - f) + b*f);end;

##### Share on other sites
Can anybody help me out with perlin noise...?

##### Share on other sites
Quote:
 Original post by sofakngCan anybody help me out with perlin noise...?

You'll find a lot of outdated information regarding perlin noise on the web.

Here is Perlin's own improved reference implementation http://mrl.nyu.edu/~perlin/noise/

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633298
• Total Posts
3011260
• ### Who's Online (See full list)

There are no registered users currently online

×