# Tilemap collision detection strangeness [closed]

I have used this code, or code very similar to it, to detect collisions between rectangle shaped entities and a tilemap for a long time. When I read the code it seems to me that it is impossible for an entity to wind up intersecting with a non-traversable (solid, as I call it in the code) tile but sometimes when I'm testing a game a character gets stuck half-inside of a wall or tree or something.

Here's the code, can anyone figure out how the entity could possibly wind up in a solid tile?

``````    public void moveX(float mx, Level level) {
//tile(float pos) returns the tile for the position, in this case since tiles are 16x16 it returns Math.floor(pos / 16)
float newx = x + mx;
if(tile(x + width) != tile(newx + width)) { //if the right side of the new position isnt on the same tile as the previous position
for(int Y = tile(y); Y <= tile(y + height); Y++) { //check this once for every vertical tile the entity touches
if(level.isSolid(tile(newx + width), Y)) { //check if the new tile is solid of traversable
isTerrainCollision(level, tile(newx + width), Y); // if it is, call terraincollision function and
newx = x;                                         // move back to previous position
}
}
}else if(tile(x) != tile(newx)) { // do the same for the left side of the entity
for(int Y = tile(y); Y <= tile(y + height); Y++) {
if(level.isSolid(tile(newx), Y)) {
isTerrainCollision(level, tile(newx), Y);
newx = x;
}
}
}
x = newx;
}
``````
-
 -1 for vagueness and lack of research. What exactly are you trying to achieve? What have you tried? Where do you think the problem is? – Anko Jan 3 at 1:05 This question is pretty localized; you're just asking us to debug your code for you. Try stepping through it with a debugger and inspecting state during a frame when the problem manifests, you'll probably find problem pretty quickly. – Sean Middleditch Jan 3 at 2:30

## closed as too localized by Sean Middleditch, Josh Petrie, Trevor Powell, Sam Hocevar, michael.bartnettJan 9 at 7:28

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

 if the right side of the entity has entered a new tile then the left side has either not entered a new tile or entered a tile that the entity was already occupying. (The left side follows the right sides movement and vice versa) – Erik Bergsten Jan 2 at 15:57 The thing that confuses me the most is that in the code it says if your new positions collides the level then move back to your previous position, how can it possibly get stuck then? It only happends rarely and like you said, seems to be related to high mx's that result from high time deltas. But it should still be impossible for it to get stuck in a wall. – Erik Bergsten Jan 2 at 15:58 @ErikBergsten I still think your `else if` is incorrect, because with high `mx` values the right corner can move 2 tiles while the left moved 1 tile (even though it's the same distance - if the rectangle is in the center of a tile, and end's up moving 1.5 tiles to the right, then the tile-division will be underneath the center of the rectangle). – Liosan Jan 2 at 16:02 that would require unrealistically high mx, but thanks for the help anyway – Erik Bergsten Jan 2 at 19:08