Saturday, April 21, 2018

algorithm - How do I generate random mountains in a sidescroller?


So I'm trying to make a simplified Terraria style world using Perlin noise. I got that working and the terrain is randomized. However the terrain always looks like like a weird cosines curve. Hills and bottoms with different heights repeating. The effect I want to achieve is something similar to this:


enter image description here



Where the terrain is mostly flat looking and sometimes a large mountain will appear.


Is there an algorithm or "noise" that could achieve this effect or what other things do I need to do?



Answer



You want to mix different wave lengths with different levels of intensity.


E.G. Have one long wave, that has a high intensity, and a short wave with low intensity.


Now add the two(or more) waves together. enter image description here Black line being long waves and high intensity.


Red line being short waves with low intensity.


Green being the final result.


float getHeight(float x) {
float longWave = getNoise(x / 100) * 20;

float shortWave = getNoise(x);

return longWave + shortWave;
}

Eventually add more waves, maybe using different noise algorithms. Play around with the values. To get more interesting results you could also try to multiply different wave lengths, make them exponential etc...


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...