Following this lesson for implementing mid-point displacement algorithm for terrain generation. I aim at generating a wavefront(obj) file of the terrain
I have an array of dimension (2^n + 1) x (2^n + 1). The corners of the 2d array is initialized with random numbers.
n = plug_in_n arr = Array.new(2**n + 1) { Array.new(2**n +1) { 0 } } n_augmented = 2**n arr[0][0] = rand(10 + 1) arr[0][n_augmented] = rand(10 + 1) arr[n_augmented][0] = rand(10 + 1) arr[n_augmented][n_augmented] = rand(10 + 1) This is the script I have written for generating a terrain.
a = arr n_augmented_copy = n_augmented while n_augmented > 1 puts "n_augmented = " + n_augmented.to_s i = 0 while(i < n_augmented_copy) j = 0 while(j < n_augmented_copy) a[i][j+(n_augmented)/2] = (a[i][j] + a[i][j+n_augmented]) / 2.0 + (rand 0.0..1.0) a[i + (n_augmented) / 2][j + n_augmented] = (a[i][j + n_augmented] + a[i + n_augmented][j + n_augmented]) / 2.0 + (rand 0.0..1.0) a[i + n_augmented][j +( n_augmented) / 2] = (a[i + n_augmented][j] + a[i + n_augmented][j + n_augmented] )/ 2.0 + (rand 0.0..1.0) a[i + (n_augmented) / 2][j] = (a[i + n_augmented][j] + a[i][j] )/ 2.0 + (rand 0.0..1.0) a[i + (n_augmented) / 2][j +( n_augmented) / 2] = ((a[i][j+(n_augmented)/2] + a[i + (n_augmented) / 2][j + n_augmented] + a[i + n_augmented][j +( n_augmented) / 2] + a[i + (n_augmented) / 2][j]))/ 4.0 + (rand 0.0..1.0) j = j + n_augmented end i = i + n_augmented end # binding.pry # p_arr(a) n_augmented = n_augmented / 2 end
Demonstrating a case for n = 2: I have omitted the random-displacement rand 0.0..1.0to check if the heightmap chucks are getting calculated fine.
Here is how the iterations look like
n_augmented = 4 9 0 5.5 0 2 0 0 0 0 0 6.5 0 5.5 0 4.5 0 0 0 0 0 4 0 5.5 0 7 n_augmented = 2 9 7.25 5.5 3.75 2 7.75 6.625 5.5 4.375 3.25 6.5 6.0 5.5 5.0 4.5 5.25 5.375 5.5 5.625 5.75 4 4.75 5.5 6.25 7 If I form a mesh using this alone, it results in a plane like: 
Considering the random displacement for this case, it results in : 
This is not desirable since I cannot see any peaks and slopes.
Here is the ruby script : CODE

