Blocky Terrain Generation

I recently got back into game development and tried to make a blocky terrain generation. but I could not grasp how will I do this. I tried to do something like:

local heights = {10, 7, 5, 2}

local y = get_closest(heights, noiseValue)

but it comes out as a flat terrain? even thought the noiseValue were fine.

this is how It shoud look like:


(the green boxes represent the terrain cells)

so logically variable y which i assume is the height are all the same.

1 Like

no, i forgot to say that the code provided is a pseudocode. the height of the cells is based on the closest height in heights table

can i see your get_closest function code

i deleted the script a few hours ago because it was too messy, but this should be the same:

local function get_closest(t, v)
   local closest, closest_diff = nil, math.huge

   for _, height in t do
      local diff = math.abs(height-v)
      if diff < closest_diff then
         closest_diff = diff
         closest = height
      end
   end

   return closest
end

(edit i messed up the if)

alright, now what’s noise_value like? i mean give me a range of numbers of the noise_value

its mostly a range of 1-8. though it may vary

holdon,

and

are you seriously comparing a number to infinity?

yes?
image

no no no, your code shows math.huge < 2

sorry i got a bit confused recalling the code, it should be the same as before now.

alright now im asking what get_closest always returns? in this context

it mostly returns 1-5 (7 rarely)

does it returns different numbers every time?

yes, but not always

ignore this

so it must be a problem with your terrain making script, since it returns a different y coordinate.

It seems so, I’ll try to get this reworked by tomorrow. I’ll let you know when fixed

1 Like