Q: How do you flatten a 3D array? I have a 3D array that contains integer values for each different block in my game; int[x][y][z] = blockid;
I need to flatten the array into a one dimensional array; int[VALUE] = blockid;
I have looked through the site for an answer most of the answers that I saw were incorrect and gave really bad results, For example my world is 2048x2048x128 blocks, That is 536870912 blocks. I need a mathematical equation that would give me 536870912 different results for 536870912 different values.
This is an example ( that doesn't work ) that I found on the site: value = x+y * maxX+z * maxZ * maxY
The world is a square, The height is a fixed 128 though.
Thanks.
EDIT:
setter code:
world.blocks[x+y*world.maxSize+z*world.maxSize*world.maxSizeY] = Block.DARK_STONE.getId();

it used to be
world.blocks[x][y][z] = Block.DARK_STONE.getId();
getter code:
World.blocks[x+y*world.maxSize+z*world.maxSize*world.maxSizeY];

instead of
World.blocks[x][y][z];

the blocks[]
blocks = new int[2048*2048*128];

the maxSize and maxSizeY
maxSize = 2048;
maxSizeY = 128;


A: Your example seems pretty close. It should be
x + (y * maxX) + (z * maxX * maxY);
With int[2][3][4] that would give me:
0 0 0 = 0 + 0 + 0 = 0
1 0 0 = 1 + 0 + 0 = 1
0 1 0 = 0 + 2 + 0 = 2
1 1 0 = 1 + 2 + 0 = 3
0 2 0 = 0 + 4 + 0 = 4
1 2 0 = 1 + 4 + 0 = 5
0 0 1 = 0 + 0 + 6 = 6
1 0 1 = 1 + 0 + 6 = 7
0 1 1 = 0 + 2 + 6 = 8
1 1 1 = 1 + 2 + 6 = 9
0 2 1 = 0 + 4 + 6 = 10
1 2 1 = 1 + 4 + 6 = 11

... you get the idea.
