0

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;
ABOODYFJ
  • 30
  • 1
  • 9
  • 2
    it's `z * maxX * maxY` not `z * maxZ * maxY`. Just imagine how you would do it in real life, you lie row after row and if you are done with one slice of x * y you take the next slice. The order of X,Y and Z should depend on how you most often traverse it. – PeterT Oct 02 '14 at 14:07
  • @PeterT thanks for the reply, Both maxZ and maxX are the same in my world since I have a square world, It still doesn't work for me though. – ABOODYFJ Oct 02 '14 at 14:11
  • define "doesn't work" – PeterT Oct 02 '14 at 14:13
  • possible duplicate of [How to use pointer expressions to access elements of a two-dimensional array in C?](http://stackoverflow.com/questions/13554244/how-to-use-pointer-expressions-to-access-elements-of-a-two-dimensional-array-in) – adam.baker Oct 02 '14 at 14:14
  • It places blocks where they shouldn't be, But when I was using a 3D array it was working just fine, For example the grass layer is filled with wood logs mixed with air, The terrain doesn't look realistic anymore as well, Where it used to be smooth, With hills etc. – ABOODYFJ Oct 02 '14 at 14:15
  • @AbdullaAbdElrahman maybe show us the code lines that you changed? As in "how it looked before" and "how it looks now" – PeterT Oct 02 '14 at 14:17
  • 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(); – ABOODYFJ Oct 02 '14 at 14:20
  • @AbdullaAbdElrahman and the place where you read it out? You presumably changed that too. Also, when adding code it's easier to edit your initial question (since you can use proper formatting) – PeterT Oct 02 '14 at 14:23
  • @PeterT I have edited the main post to show the getter/setter. – ABOODYFJ Oct 02 '14 at 14:33
  • @AbdullaAbdElrahman then just for completeness the declaration and definition. – PeterT Oct 02 '14 at 14:36
  • @PeterT added, But that's all the code used really, The rest is drawing code, Loops in x, y, z, checks using the getter ( posted above ) and then adds it to the display list and renders it. That's all. – ABOODYFJ Oct 02 '14 at 14:53
  • @AbdullaAbdElrahman and it used to be of type `byte[2048][128][2048]` (I know that's not valid code but conceptually)? – PeterT Oct 02 '14 at 14:58
  • @PeterT I used to use byte instead of int, That's why I typed byte, But I am using int now, But that's not the problem, Unfortunately. – ABOODYFJ Oct 02 '14 at 15:04
  • @AbdullaAbdElrahman the issue is not with the formula though, if you want to see a minimal example of how it's not the problem see this example: http://ideone.com/F3RxG6 . Your issue is not with that function – PeterT Oct 02 '14 at 15:45
  • @PeterT Thank you, I just needed to at least confirm that it works so that I can move on. I'm writing a new engine anyway, So hopefully I know what I did wrong. – ABOODYFJ Oct 02 '14 at 16:07

1 Answers1

3

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.

Remco van Dalen
  • 304
  • 1
  • 6