# 2D Tiled Map - Small tiles or big tiles with pixel-precise movement

I am using Java - LWJGL - Slick2D . I have tried to do a proper collision detection with 32 x 32 tile size but failed three times. I just cant get it to work! I found a youtube video that was using ligbdx (but the code was understandable) that used small tiles and achieved something like pixel-precise movement , so here is the question , NOT opinion based. Keep trying to make a legit collision system or use small tiles? Also , please give me some info related to your answer like "tiles need to have X times Y size" or "a good resource is X Site for pixel-precise movement".

You probably had some problem in your code, because basically collision detection can always be resolution independent, so it won't matter whether your tiles are 8x8 or 1024x1024.

The following would be just one possible workflow to achieve this. Note that I didn't care for strict casting etc. so there might be cases where you'd have to cast an int to float go get the needed precision or where you'd have to cast a float to int to get the modulo operator to work.

Determine the tile where to check for a collision. How you'll have to do this really depends on your coordinate system. It could be as easy as rounding down your coordinates:

int tile_x = (int)(x / tile_width_in_world);
int tile_y = (int)(y / tile_height_in_world);


These coordinates can be used to get a first estimate, whether there's more work to be done, i.e. whether there's just sky, some ground (i.e. pixel testing needed) or only ground.

If you need pixel perfect testing, the next step should involve determining the position within the tile you'd be looking at:

float check_x = (x % tile_width_in_world) / tile_width_in_world;
float check_y = (x % tile_height_in_world) / tile_height_in_world;


This would result in two values, each ranging from 0 to 1 based on where in the tile your position is. This can then be converted to pixel coordinates within the actual tile texture:

float real_check_x = tile_offset_x + check_x * tile_width;
float real_check_y = tile_offset_y + check_y * tile_height;

• Ι will study these stuff , can you please take a look at what I have already tried to do? link Ι Have already done the first snippet you posted , but I think I have implemented the second snippet's logig differently. Commented Jan 3, 2014 at 10:22
• You're doing it similar, just without the scaling I added. Also note that you're rounding to the next bigger number (Math.ceil()) and then round again (Math.round(); which won't do anything anymore). You can skip the whole scaling if your tile size in your game world is the same as your tile/texture dimensions. Commented Jan 3, 2014 at 10:28
• The whole problem is that it does not work properly Commented Jan 3, 2014 at 10:59
• Think the problem might be your basic collision detection approach. First of all, determine "vertices" where you'll check for collisions, e.g. one for the feet. Then try to always highlight the tile this vertex is on (without doing any real collision checking). Once that's working fine, you can do the next step and try to lookup the actual texture contents. Commented Jan 3, 2014 at 11:36