Reputation: 4885
i am making a game in which i have a 3d map stored int an array int integers:
int width = 256;
int height = 256;
int depth = 64;
int[] map = new int[width * height * depth];
i need to be able to get get the x, y, z of an index. the current method i came up with is:
private int getX(int blockID) {
return blockID % width;
}
private int getY(int blockID) {
return (blockID % depth) / height;
}
private int getDepth(int blockID) {
return blockID / (width * height);
}
the methods for getting the x and the depth seem to work but i cannot get the getY() to work correctly and i keep getting and ArrayIndexOutOfBoundsException like the one below:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at game.Level.updateLighting(Level.java:98)
at game.Level.generateMap(Level.java:58)
at game.Level.<init>(Level.java:21)
at game.mainClass.main(Minecraft.java:6)
please help if you know how to do this.
Upvotes: 0
Views: 444
Reputation: 500903
Try
private int getY(int blockID) {
return (blockID / width) % height
}
Upvotes: 1
Reputation: 360026
Dude. Just use a 3D array.
int[][][] map = new int[width][height][depth];
Then you don't have to worry about demuxing x
, y
, and z
indices from the blockID
. They are lineary independent, so keep them that way.
Upvotes: 3