I'm working at a tiled game. I want to check the direction where the tiled is from the player, when is colliding.
static public boolean intersectRectangles(Rectangle rectangle1, Rectangle rectangle2, Rectangle intersection) {
if (rectangle1.overlaps(rectangle2)) {
intersection.x = Math.max(rectangle1.x, rectangle2.x);
intersection.width = Math.min(rectangle1.x + rectangle1.width, rectangle2.x + rectangle2.width) - intersection.x;
intersection.y = Math.max(rectangle1.y, rectangle2.y);
intersection.height = Math.min(rectangle1.y + rectangle1.height, rectangle2.y + rectangle2.height) - intersection.y;
return true;
}
return false;
}
I want this function to return 0 if there is no collision
1 = rect1 is in the left
2 = rect1 is in the right
3 = rect1 is in the bottom
4 = rect1 is in the top
Like this pic:
I tried doing this
// if there is a collision between player and tile
float xdif = player.x + 40 - j * 40;
float ydif = player.y + 40 - i * 40;
Direction direction = Direction.RIGHT;
if (Math.abs(xdif) > Math.abs(ydif)) {
if (player.y > i * 40)
direction = Direction.DOWN;
else
direction = direction.UP;
} else {
if (player.x > j * 40)
direction = Direction.LEFT;
else
direction = direction.RIGHT;
}
But is not working..