Hey, I’m working on a simple 2D Terraria clone game. I have a variable called “groundHeight”, which controls the highest block of grass/dirt that will spawn on that column. I have that set to a Random.nextInt(), which works pretty well, but I want to have it more smooth between the columns. Right now, it looks very jagged from column to column. Is there a better way to do this, or a way to fix this smoothing?
Couldn’t you make rough steps of random parabolas in your terrain? That should get more hilly.
You want perlin noise. I’d add links, but google can do waaaaay better than me.
In the game I’m working on, I spaced out the points that I randomize and then just “connected the dots”.
Screenshot: https://twitter.com/#!/gary_beebe/media/slideshow?url=pic.twitter.com%2F9erkaAMH
In your case it might be easier to randomly pick 0 or 1, 0 meaning that the next section is the next row down from the last section and 1 being the next row up. That will be your groundHeight for the next couple of columns which you can randomize how long the section is (in columns), maybe ranging from 1 to 10. This would probably give you a landscape similar to this:
http://terraria-servers.webs.com/terraria-steam-wiki.png
Spacing out random points and “connecting the dots” using something like cosine interpolation is pretty much the definition of perlin noise (I’m not clear on what I’d call the interpolation, it’s certainly not linear). You should definitely look it up – it’s not that hard to implement, and there are a lot of perlin heightmap generators that are ready-to-use that you can more or less drop in.
@sproingie After looking up Perlin Noise, I guess what I’ve been using is Linear Interpolation. I’ve been looking for a way to smooth out the landscape and I think Cubic Interpolation could be very cool in a game like mine. (Thanks for adding that to my vocabulary).
Here’s a good article:
http://freespace.virgin.net/hugo.elias/models/m_perlin.htm
Note: that web page is actually value noise and not Perlin.
Either way, I still appreciate you pointing it out.
Thanks so much, guys! What I did was very simple. All of my “columns” have ids, the first ever column has an id of 0. I have an ArrayList of columns. I have it check the ArrayList for columns. If the column being generated has an id of less than 0, i check columns with a greater id and compare the generation, and vice versa. I then take that groundHeight, and add or subtract 1 (maybe more in the future), or keep it the same, by random. This works great for now.
Maybe this aritcle will help you a little bit: http://javacooperation.gmxhome.de/LandscapeEng.html
I think it is a pretty simple way to generate 2D Terrain
use fractional brownian motion (fBm). It’s based on a Noise function (Perlin noise is fine) but gives you more control over the character of the terrain.
I have generated some random terrain a few days ago and posted a blog entry about it including my (bad) code.
After I had read about Perlin Noise, I wanted to try to generate a map for a game I plan to do.
Because I thought it would be hard to fill the terrain below the line created by the perlin noise, I just used an image of terrain and shifted parts of its pixels downwards to create valleys. Probably not the best approach but it works.
FTFY.
p.s. your code looks pretty normal; good for you for being open and sharing it along with your thoughts.
Thanks, still not used to forum tags.