If your using voxels, I assume that you are using an array. I am doing 2D collisions in my game using this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public Block getBlock(float xx, float yy) { int x = Math.round(xx); int y = Math.round(yy); int chunkX = (int) Math.floor( x/320 ); int chunkY = (int) Math.floor( y/240 ); Chunk chunk = getChunk(chunkX, chunkY); int blockX = ( (x%320)/16); int blockY = ( (y%240)/16); return chunk.getBlock(blockX, blockY); }
public Block getBlock(int x, int y) { return level[x][y]; } |
Note that the first half of the first function is for getting a world location, and the next gets the block. The second function is what the chunk uses to find that block. As you can see, it doesn't have to loop through every block. Unfortunately, this does not work in negative. Or, I haven't found out how to, others can probably find out how to do that.