Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top