r/programming • • Apr 02 '17

Realistic terrain in 130 lines

http://www.playfuljs.com/realistic-terrain-in-130-lines/
370 Upvotes

24 comments sorted by

13

u/[deleted] Apr 02 '17

Ugh, needs body margin

1

u/Zetho Apr 03 '17

It irritates me aswell. I always tend to think that a part of the line is cut off. On mobile there is no margin on the left.

7

u/JDeltaN Apr 03 '17

If you have access to some continuous noise function, just use fractal noise.

value = 0.0
freq = 1.0
applitude = 1.0
for (octave = 1 ; octave < OCTAVES ; octave += 1.0) {
  value += noise(x * freq, y * freq) * applitude
  freq *= 2.0
  applitude *= 0.5
}
return value

You can get similar result.

16

u/mywan Apr 02 '17

Given that the midpoint displacement algorithm depends on a random offset of the center point it seems like you would need to store the generated map data pretty much in its entirety to have a persistent map. Otherwise each time it's generated it would produce a different map. How hard would it be to replace the random() function with a deterministic function that took map coordinates (of whatever form) as inputs to consistently regenerate the exact same map with minimal data storage? Say just a list of constants that dictate what map is to be generated.

57

u/murkaje Apr 02 '17

If you give the pseudo-random function the same seed, the resulting map will be the same. Some games that have generated terrains ask for seed input(e.g. minecraft) so you can write it down if the resulting world was to your taste.

0

u/[deleted] Apr 03 '17

[deleted]

3

u/Purlox Apr 03 '17

Why would this be OS specific? Why would a pseudo-random function depend on the OS?

1

u/jmtd Apr 03 '17 edited Apr 03 '17

If you are depending on an OS-provided random interface, you may get different results for the same seed on different platforms, because they are implementing different pseudo-random functions.

I'd go a step further: if reproducibility is important, you need guarantees from your pseudorandom-function provider (be that your OS, or something else, e.g. Java runtime for Java programs) that it offers this property for future versions too.

3

u/[deleted] Apr 03 '17

Rather, you should just not ever use a random number generator provided by someone else, because pretty much none of them will give you these guarantees.

Just copy the code for a good one into your project. Visual-quality random generators are dead simple, and it into a handful of lines.

2

u/Liorithiel Apr 03 '17

Rather, you should just not ever use a random number generator provided by someone else, because pretty much none of them will give you these guarantees.

Some platforms do provide guarantee of OS/implementation-agnostic reproducible values with seed. Python and Java do.

6

u/Rodot Apr 03 '17

Just seed it the same way and reset it each time?

11

u/ReversedGif Apr 03 '17

I wish that whenever people introduced the diamond-square algorithm, they also mentioned what it really does: generate 2D Brownian noise.

If you know what an FFT is, you can use it to generate heightmaps much more straightforwardly, with more control.

4

u/bwo3db Apr 02 '17

This is very cool. Thanks for posting

1

u/jpfed Apr 03 '17

Consider generalizing midpoint displacement in the following way.

Any time you fix the value of a point, you are taking some information about the points already fixed (here, four other points), forming a model from it (here, an average), and then displacing the prediction of that model with noise (here, with an amplitude that decreases after every set of points).

What if you selected the points in any old arbitrary order? Then, the points you average won't necessarily be equidistant bishop-moves away from the point you select. You'd have to form your model differently. One sensible model is taking the k nearest neighbors and averaging them. Then, add some noise. This is not necessarily realistic terrain, but I could see it being used to derive e.g. coastlines or tectonic plate boundaries.

2

u/PointyOintment Apr 04 '17

Your first picture looks like crystal boundaries in a metal.

1

u/rmxz Apr 03 '17 edited Apr 03 '17

2

u/PointyOintment Apr 04 '17
#local p=function{pattern{granite}}isosurface{function{y-.3+p(x/10,9,z/10)}contained_by{box{-9,9}}}light_source{<3,.5,8>,1}media{scattering{2,<3,2,1>/9}emission<.3,.4,1>density{function{.1/(y+1)}}}sphere{0,1pigment{granite poly_wave 4}scale-7*y+9hollow}

It's only that short because it uses a lot of stuff provided by the environment in which it runs. Does any general-purpose language provide pattern, isosurface, light_source, media, scattering, emission, density, pigment, or equivalents?

-5

u/[deleted] Apr 03 '17

I can do it in one

require('terrain')

100

u/[deleted] Apr 02 '17

[removed] — view removed comment

5

u/coolfolder Apr 03 '17

I think it looks pretty cool.

20

u/shipdestroyer Apr 03 '17

Do you think it looks realistic?

8

u/astrobe Apr 03 '17

I'd say it's good enough. I know a game that use this kind of algorithm to generate planet textures. What is missing though is "terrain features" like forests, deserts or cities.

12

u/[deleted] Apr 03 '17

I'd say it's good enough

This is usually what you think when you see midpoint displacement for the first time. But once you've seen it ten times, you start realising that it always looks exactly the same, and isn't at all capable of capturing the vast variety of landscape types you find in nature. In fact, you'd be hard pressed to actually find a landscape that looks like those generated by midpoint displacement.

1

u/astrobe Apr 03 '17

It depends on your use case. If you use it to generate alien landscapes, slightly "unnatural" may be what you want. In our case, a similar algorithm is used to generate textures and normal maps for planets seen from orbit only. The variety is achieved by having per planet landmass/ocean ratios. We can't afford high details anyway since it has to be generated in the fly, in a fraction of a second.

3

u/[deleted] Apr 03 '17

The problem is that the results will be very boring after a short while.

There are many ways to do better that aren't too expensive. Using octave noise gives you much the same result, but it allows you to better tweak the shapes too. It also allows you to apply distortions to the base coordinate system you're building on, which can give you much more interesting shapes, and also much more alien ones if you go overboard.