# How do I detect a tile map collision's direction? [duplicate]

I'm implementing collision detection with the map for my platformer. I have a method that generates a polygon on every tile and checks if the player hits it -- that's all fine. The problem is that when ever a player touches a wall in front of him the d key which moves him to the front gets disabled so he can't walk any further that way.

This method works great for preventing him from walking into walls, but I also want him to be able to jump on the walls and there is the problem. So he falls on the tile(polygon) the game senses that and applies the same method for the d key, preventing the character from walking left or right, because the game thinks it is hitting the tile in the front but it is hitting from above.

Long story short, the game handles collision from above same as from the front or back. How do I make it recognise collisions from the side separately from above?

My code is along these lines:

if(PlayerPolygon.interesects(TiledPolygon) && dKey==1) {
moveX = 0;
}
if(dKey==1) {
player.x +=moveX
}

• Code would be nice. Initial idea is to have two functions. One to detect vertical collision, and one to detect horizontal collision. – mobo Oct 6 '13 at 19:50
• i added some code... not the ideal code but i hope you get the idea but hwo woudl i make a method that knows when i hit it form the side of top using the same hitboxes – jan vos Oct 6 '13 at 20:00
• That isn't much code, What is your intersects method? Whats dKey? How is moveX used? – mobo Oct 6 '13 at 20:18
• i'm using slick2d's intersect method so i don't really know much about that dKey is just the d key on your keyboard i will add the move method too. – jan vos Oct 6 '13 at 20:31

You ask how to detect collision on both sides. Well, that answer is simple. Detecting collision on multiple sides is simply a matter of saying 'Okay is he colliding on the left? What about the right? Top? Bottom?' The method you're asking for checks for collision on every side and then gives responses as to where collision occurs.

You're also handling your movement incorrectly. This can be a little bit tricky, but I'll walk you through it. In a game, it might sound easy to just write a piece of code like this to handle your movement

if (a key is pressed
{
player.move();
}


But this ends up giving you a glitchy response and bad results with collision. Instead, you need to have several methods. One for detecting when the key is up, when for when the key is down, and one to detect the movement. Here's a rought sketch:

boolean keyIsPressed;

void isKeyUp()
{
keyIsPressed = false;
}

void isKeyDown()
{
keyIsPressed = true;
}

void detectMovement()
{
if (keyIsPressed)
{
player.move()
}
}


This will give you correct movement.

Now for collision that you're asking for. Essentially what you want is to detect collision on all 4 sides. So with the correct movement that I displayed above, you should make a method like

if (collisionOnLeft)
{
handleCollision(left);
}

if (collisionOnRight)
{
handleCollision(right);
}

if (collisionOnBottom)
{
handleCollision(bottom);
}

if (collisionOnTop)
{
handleCollision(top);
}


So because you're detecting collision on all 4 sides and then handling it accordingly, with proper movement, you will get proper collision response with multiple points. Note, I use an if statement for each, instead of using if, elseif, or else. The reason for this is because collision on each side should be handled separately, instead of grouping it.