UPDATE: I ran a test with more realistic colors, crudely applied. Before reading, view the results here (click to enlarge)!
Someone recently responded to a post of mine by asking if they could see my code. This sent me into a deep existential conflict! Fact is, I am a huge proponent of open source. I believe it to be the wave of the future and the only way that the digital part of our civilization can progress. But I also don't want to give away all my secrets :(
Instead, I sort of picked a middle ground. It's a bit of a copout, but there are a few reasons why I would rather not just post a thousand lines of code. Firstly.... mine. Mine mine mine mine mine. But seriously, I am a bit protective, because I know how easy it is for someone from the outside to scrounge forums and blogs for code to snatch. I've seen it before, and I bet others in here have horror stories of people presenting copied code as their own. I don't like that. Secondly, the code is a mess right now, and much of it only barely works. I'd rather not spend too much time discussing or, gasp, explaining code that is poorly put together and will soon be obsolete. I don't like to share what I can't defend honorably. And thirdly, I really like to discuss the ideas and science behind it much better, and if people get the code, they want to discuss that code, instead. And IIIIII dun wanna!! In time, though, I hope to open up more. Share my feelings, talk about my dreams and such. *group hug*
And now........... SCIENCE (biatch)!!
The images atop this entry are from the latest test run of the world creation engine. They show mountain ranges, rift canyons, cliffs, and a variety of ways those things interact with each other (the typical one: You run face first into a sudden wall of rock). The entire thing is based on tectonic plates, which is the accepted theory of geology on how continents move over time. To make a long science short, our planet's surface is broken into many distinct 'plates', which are moved by, among other things, molten rock moving around beneath the planet's surface (the semi-liquid 'mantle' under the solid 'crust' that we live on). Where they move into each other, they push their material up to become mountains and other raised features. Where they pull apart, 'rifts', deep cracks in the surface, form, until molten rock from beneath it rise up and cool to become new rock on the edge of the plates. Where they just slip by one another, the plates cause earthquakes due to friction, but this is not simulated here, because this particular bit of world generation creates a static, unchanging planet. For now.
So how does one create a planet of tectonic plates and have them shape the surface? First, let's divide the surface into plates. Simply generate a number of random points on the surface. We call these 'cratons', the name of the first hardened pieces of rock that long ago formed the plates. Whichever craton is closest to a point on the surface, that point is assigned to. The points assigned to a craton make up that one tectonic plate. Easy enough.
Next, how do we make things form along the edges of a tectonic plate? First, we set a maximum distance of the effect. Then we have every point on the surface check not just which craton is the closest, but also which one is the second closest. The closer the distance between the point and the closest craton matches the distance between the point and the second closest craton, the more we raise the point above the surface. That means that points close to mdiways between closest and second closest craton get raised a lot, while points farther away continually get raised less. The result: Basic mountains. The greater the maximum distance of the effect, the flatter (wider, not lower) the mountains become.
But that gives us mountains along all tectonic edges. We want some places to meet as mountains, but others to glide past each other, and others yet to pull apart and create deep rifts! How that??
Firstly, every craton gets a vector that defines its movement over the last millions of years. Once it has that, we can take the dot product of that vector against the vector we get from the closest craton to the point exactly between it and the second closest. In more layman's terms, we check how closely the plate's (craton's) movement matches the direction from it to the point right between the two plates. The greater the result, the more the points on the surface rise (i.e. you multiply the dot product with the amount of rise). And since a craton moving away from the other craton means a negative result, points that used to rise will drop, creating rifts, like valleys.
It took a little while to put all these things together, put for basic tectonics, they seem to work quite nicely. I am now trying to figure out how to make mountains and valleys more interesting, without compromising the simple but scientifically based generation. In real geology, converging plates (plates that move into each other, creating mountains) crack and splinter at the edge, making pieces of them shoot up at different rates, creating not a single straight line of mountain, but jagged ridges and splintered paths of mountain ranges. Rifts, on the other hand, have chunks break off at the edges, forming a kind of 'crumbling stairway' effect, rather than just a clean slope. And where plates slide along each other, they snake up and down against one another, creating weird patterns of rock shooting up and then dropping down, like two rags tangled up in one another. I am hoping all of this can be boiled down to a single, coherent algorithm, much like the stuff above. But science is a cruel mistress, and she will no doubt have her wicked way with me.....
Other than posting long sections of code, I am very open for questions on this, including thoughts on geological effects I have not yet implemented. The practice of converting science to computer algorithms is hard, but amazing to be part of, and I love discussing it and learning the perspectives of other people! I hope before long to have a more solid methodology for converting science into games, but for now, I must take it bit by bit, making each work the best I can. Once it truly clicks, I will let you know..!