I've got a tile based map, which is divided in chunks. I got a method, which puts tiles in this map and with positive numbers it's working. But when using negative numbers it wont work. This is my setTile method:
This is the setTile method in the chunk class (CHUNK_SIZE is a constant with the value 64):public static void setTile(int x, int y, Tile tile) { int chunkX = (int) Math.floor((float) x / (float) Chunk.CHUNK_SIZE), chunkY = (int) Math.floor((float) y / (float) Chunk.CHUNK_SIZE); IntPair intPair = new IntPair(chunkX, chunkY); world.put(intPair, new Chunk(chunkX, chunkY)); world.get(intPair).setTile(x - chunkX * Chunk.CHUNK_SIZE, y - chunkY * Chunk.CHUNK_SIZE, tile); }
The tiles array is a 2 dimensional 64*64 array
public void setTile(int x, int y, Tile t) { if (x >= 0 && x < CHUNK_SIZE && y >= 0 && y < CHUNK_SIZE) tiles[x][y] = t; }
The setTile method places a tile in a chunk. But when using a negative value a weird result is created. With e.g. x 53 it should place the tile in the chunk at the x position 0 and the chunk-array position 53. With the x value 87 it should place it in the chunk at x position 1 and the chunk-array position 23. These two examples work. But when using e.g. -45 it should place it in the chunk at x -1 and chunk-array position 18 which is not the case.
What's wrong with my code?