# Java chunk negative number problem

• October 17th, 2013, 11:54 AM
Bingo90
Java chunk negative number problem
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:
Code :

```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); }```
This is the setTile method in the chunk class (CHUNK_SIZE is a constant with the value 64):
The tiles array is a 2 dimensional 64*64 array

Code :

```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?
• October 17th, 2013, 11:59 AM
Norm
Re: Java chunk negative number problem
Quote:

But when using negative numbers it wont work.
What does "wont work" mean?
If you get an error message, copy the full text and paste it here.
Otherwise explain what happens.

If needed, post a small simple program that compiles, executes and shows the problem.
• October 17th, 2013, 12:03 PM
Bingo90
Re: Java chunk negative number problem
Quote:

But when using a negative value a weird result is created.
Sometimes the result for the chunk-array position is 128 sometimes 64.

It's kind of hard to explain. I'm using chunks in a tile based game. When using a absolute x or y value, it should place it in a relative x or y value in the chunk. However, when using a negative value as x position (maybe y too, but I don't need negative y positions) the result is a strange value out of the bounds of the array.
• October 17th, 2013, 12:24 PM
Norm
Re: Java chunk negative number problem
Please post a small simple program that compiles, executes and shows the problem.
• October 17th, 2013, 12:43 PM
Bingo90
Re: Java chunk negative number problem
Wow... When programming the problem-showing-program the problem somehow solved itself
• October 17th, 2013, 01:24 PM
Norm
Re: Java chunk negative number problem
That makes it easier when you are able to find and solve your problems.