1

How can I generate rolling hills of different height and width like Tiny Wings? Right now I'm using sin() but it obviously generates uniform hills repeating forever and ever.

What's the secret?

Thanks!

Bruce
  • 13
  • 3

3 Answers3

1

Certainly not a procedural generation expert - but you could try additively combining multiple randomly generate sin/cos functions with different periods. The more you add, the more random seeming it will be.

Something like this.

Community
  • 1
  • 1
James
  • 8,512
  • 1
  • 26
  • 28
  • How can I modify it's "period"? Is that what it's called when it's at 0 and the next time it reaches 0 on the y? I think that'll work perfectly for modifying the width of each hill. – Bruce Jan 09 '12 at 05:59
  • Generally, a sinusoidal function is: magnitude * sin(x / (period)). Changing period in the function changes how often it bounces, changing magnitude changes how tall the waves are. Try the link I sent, play with the parameters, and see if you find anything you like. – James Jan 09 '12 at 06:01
  • Maybe I'm confused on what X is and that's why I'm not able to do it. Isn't it like a time variable or something? – Bruce Jan 09 '12 at 06:32
1

Simplex noise, or any other 2d noise function that looks the way you like.

user57368
  • 5,675
  • 28
  • 39
0

If you're set on accomplishing this with sine waves to create this variation, here's a basic overview of how to manipulate the sine function:

Say you have an x axis, a y axis, and a sine wave y = sin(x)

y = sin(x * 0.5) makes the sine wave cross the x axis half as often (frequency)

y = 0.5 * sin(x) makes the sine wave reach a height half as high (amplitude)

y = 0.5 + sin(x) moves the central axis of the sine wave up the y axis by 0.5 (translation / offset)

Using these three properties, you can construct a wide variety of different looking waves.

Now, the trick is you'll have to overlay these waves to create variation over time. An easy way to do this is to just add the waves together,

y = sin(x * 0.5) + 0.5 * sin(x) + (0.5 + sin(x))

Or, you could define different waves over a specific subset of the x axis, and create a piecewise function. This may be more controllable / art-directable:

y = {
sin(x * 0.5) for x in (0, 5],
0.5 * sin(x) for x in (5, 10],
0.5 + sin(x) for x in (10, 5] }

However, f you try this, you'll notice this produces discontinuities in your wave. You should investigate different blending options (look up linear blending first) to see if you can create smooth transitions between these piecewise functions.

BUT, in my opinion, you should also investigate spline curves and see if they would accomplish what you want. Splines are well documented part of graphics and game programming, so it would be easy to implement (or grab someone else's code), and might provide a more intuitive way to author your levels (Generate a random set of points representing knots/control points, and pass a spline through it).

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
Ben P
  • 129
  • 9
  • Splines are good, but that would just move the problem from "how do I generate a function to generate rolling hills" to "how do I generate a function to generate rolling hills, so I can generate points to use as control points that generate rolling hills" - I believe the point is that the levels are somewhat randomly generated. – James Jan 09 '12 at 06:20
  • Though, you could actually do both. A spline for the overall feel of the level, and then something sinusoidal to modulate it to give the hills feel. – James Jan 09 '12 at 06:24
  • Lots of info to take in, thanks. Reading up on spline curves now. – Bruce Jan 09 '12 at 06:27
  • What's X in sin(x) and what should I put in to make sure my curves always line up? – Bruce Jan 09 '12 at 06:53
  • X in this case represents the x axis of your screen. You're going to loop over a range of x values, perhaps from the left side of the screen the right, and at each value, calculate y. Then you'll have an (x,y) pair at which to draw a pixel. The end result will look like a curve. But it sounds like reading up a bit on geometry / trigonometry might be in order before you attempt this. – Ben P Jan 09 '12 at 07:10
  • @James, with enough control points, you could describe all of the hills with just a spline, no need to layer a sine on top of it. I agree, you still need to figure out a set of heuristics for generating those random control points so that it's a reasonable looking curve. – Ben P Jan 09 '12 at 07:17