So I tried doing a 2D random terrain generation myself, it worked until I made so that the first generation stays as it is.
It now instead creates a diagonal mirror effect, wich is not what I’m looking for.
displayMode(FULLSCREEN)
function setup()
worldSize=10
size=HEIGHT/worldSize
--Color related
col={color(0,255,0,255), color(0,0,255,255)}
terrGen={}
--Random color generation (decided from 1 to amount of colors)
for i=0, (HEIGHT//size) do
for j=0, (HEIGHT//size) do
--setting random numbers
terrGen[i*j]=math.random(1, #col)
end
end
end
function draw()
background(0)
-------------------------------------My mistake is probally somwhere here
for i=0, (HEIGHT/size) do
for j=0, (HEIGHT/size) do
--copying the numbers over to a variable
local c = terrGen[i*j]
--selecting the color from the col table using variable c's numbers
fill(col[c])
--drawing squares
rect(i*size,j*size, size, size)
end
end
---------------------------------------
end
Is
“terrGen[i*j]”
supposed to give unique values for ex: i=0,j=1 or i=1,j=0?
In that case, the mirror effect is created because those coordinates point to the same index, 0. If you want unique values, consider
“terrGen[i][j]”. (:
@Kire What I’m seeing in landscape mode is a 11x10 grid filled with blue or green squares in random positions. Each time I run it it’s a different random pattern. Not sure what you mean by a diagonal mirror effect.
EDIT: Never mind, I see what your referring to now.
EDIT: I think the problem is that the range of i and j are the same size and then you multiply i and j together. As you go thru the range, ij is the same as ji so I think thats what’s causing the mirror image effect.
displayMode(FULLSCREEN)
function setup()
worldSize=10
size=HEIGHT/worldSize
--Color related
col={color(0,255,0,255), color(0,0,255,255)}
terrGen={}
--Random color generation (decided from 1 to amount of colors)
for i=0, (HEIGHT//size) do
terrGen[i]={} -- create a multi dimension table
for j=0, (HEIGHT//size) do
--setting random numbers
terrGen[i][j]=math.random(1, #col)
end
end
end
function draw()
background(0)
for i=0, (HEIGHT/size) do
for j=0, (HEIGHT/size) do
--copying the numbers over to a variable
local c = terrGen[i][j]
--selecting the color from the col table using variable c's numbers
fill(col[c])
--drawing squares
rect(i*size,j*size, size, size)
end
end
end
Multi dimensional tables are an essential thing to learn, but if you really wanted to use a one dimensional table then using (j-1)*(HEIGHT//size)+i Should work instead of i*j, assuming (HEIGHT//size) is the maximum size of the table in the y dimension