Hello,
I am detecting each side of collision between two rectangles, and wen I collide them, lets say, with the left side, it also detects collision on the top side when I move to the top, and the downside when I move to the bottom. I think that is because it is detecting also the top/bottom corners.
I am detecting each side of collision between two rectangles, and wen I collide them, lets say, with the left side, it also detects collision on the top side when I move to the top, and the downside when I move to the bottom. I think that is because it is detecting also the top/bottom corners.
How I can prevent this corner collision to allow to the player to slide?
Currently, my code is the next one (using xMove (-1 or 1) and yMove (-1 or 1) to know the player movement direction.
boolean isCollisionX = false;
boolean isCollisionY = false;
// Check collisions
for (Rectangle mapCollider : belongsToLevel.tiledMap.mergedColliders) {
if (playerCollider.intersects(mapCollider)) {
// Right collision
if (playerCollider.x - mapCollider.x < 0) {
isCollisionX = true;
}
// Left collision
if (playerCollider.x - mapCollider.x > 0) {
isCollisionX = true;
}
// Top collision
if (playerCollider.y - mapCollider.y > 0) {
isCollisionY = true;
}
// Bottom collision
if (playerCollider.y - mapCollider.y < 0) {
isCollisionY = true;
}
break;
}
}
if (!isCollisionX) {
pos.x += xMove;
}
if (!isCollisionY) {
pos.y += yMove;
}
Thank you in advance.