The several concerned text messages I received from family and friends after my last entry have taught me two things:
- I shouldn’t spend over a thousand words expressing an unhealthy affection for a math function.
- I should spend several thousand words expressing an unhealthy affection for several math functions.
That is, I should document, with painful detail, my journey in determining whether Terrain Generation is more productive use of my time than trying to fake a beard by growing really long nose hair. Day 36 of the nasally-rooted beard pursuit* has yielded disappointing results so far, so for now let’s talk about terrain.
More specifically, let’s try to explain what that function I was gushing over actually is.
Later we’ll get into what I’m actually coding (some stuff) and why (a reason? probably?), but for now I’m going to try to explain what exactly Perlin Noise is and why it can raise continents and fill oceans.
Perlin Noise was invented by a special effects artist in the 80s while he was working on Tron. Yes, this means a function for realistically simulating natural phenomena was invented for a film about life being created inside computers because the universe is that guy and can’t do anything without six layers of irony. The math behind it is pretty… annoying, but let’s see if we can get through the basic version using an unrelatable metaphor:
You’re a peasant living in a gigantic, flat kingdom. Conveniently, this kingdom is laid out on an even grid of square cells. Each intersection of the grid is ruled by a local governor, who has control over all four cells sharing their corner. Additionally, since each cell has four corners, each cell is partially ruled by 4 governors. As a peasant, you must pay taxes to each governor, but how much do you pay to each?
In this kingdom, the amount you pay to each governor is determined by how physically close you live to the governor’s palace. Should be simple enough, but there’s a twist: the City Planning department in this kingdom is horrible at their jobs, and governors don’t live directly on their corners. So, the amount of taxes you pay to each governor is a combination of how close you live to their corner and how close they live to their corner. This has the double-edged side effect that if you live far away from all the governors, you pay fewer taxes! Hooray! You also don’t have any plumbing and there are only 0.5 cable companies that’ll service your area, but it’s important to think positively.
I know that’s a lot of abstract stuff to go through without an image or diagram or anything, but I don’t have access to Photoshop right now. As a reward for your persistence, here’s a picture of my cat with some moral support as a caption:
Now that we’ve cleansed our palate and we understand Perlin Noise enough to complain about not understanding it enough, let’s talk about how it works to make worlds. If we use the “taxes” from our metaphor above to represent brightness of pixels, we get something like this image that I only took from Wikipedia because it was the first thing to come up in a Creative Commons search stop judging me and just look at the picture:
If you slap an edgy Instagram filter on that, it looks surprisingly like real clouds, but that’s not why we’re here. We’re here because if you pretend there’s no such thing as gray (insert political joke of your choosing), blobs come out!
This picture is from my first attempt at implementing The Noise™, so everything brighter than 50% gray is green, and everything darker is blue. Already you can sort of see where the Terrain Generation concept comes from, but it lacks detail. For my next trick, I zoomed in and specified more than two layers:
Boom. With almost no effort, semi-realistic (if low-resolution) islands are born. But this, dear reader (hi, me from the future!), is merely the tip of the snow-cap on the low resolution island in the bash terminal. In Chapter 2, the project gets ported over to an actual graphics framework, the world gets a lot bigger and a lot prettier, and forests will be born!
* The nasally-rooted beard pursuit attempted by the non-hirsute to absolutely refute the resolute brutes scooting around their routes in their highfalutin zoot suits and making the “cuteness” of their face-roots moot.
Please send help.