@Override public void updateCollision(LinkedList mapEntitys, final Player player, final int translateX, final int translateY) { this.player = player; /* Booleans to detect the players direction */ boolean movingLeft = player.getGame().mapMovingLeft; boolean movingRight = player.getGame().mapMovingRight; boolean movingDown = player.getGame().mapMovingDown; boolean movingUp = player.getGame().mapMovingUp; /* Entity's coordinates, relative to the map */ int startX = getMapEntityX() + translateX; int startY = getMapEntityY() + translateY; int endX = startX + getMapEntityW(); int endY = startY + getMapEntityH(); /* Players coordinates, relative to the map */ int pX1 = player.getAbsX(); int pY1 = player.getAbsY(); int pX2 = pX1 + player.getWidth(); int pY2 = pY1 + player.getHeight(); /* Global Booleans for Up / Down Collision */ boolean isRightInsideLeft = pX2 >= startX; // players right side is inside entity's startingX. boolean isLeftInsideLeft = pX1 >= startX; // players startingX is inside entity's startingX boolean isLeftInsideRight = pX1 <= endX; // players startingX is inside entity's endingX. boolean isRightOutsideRight = pX2 >= endX; // players endingX is greater than entity's right side. /* Global Booleans for Left / Right Collision */ boolean isBottomBelowTop = pY2 >= startY; // bottom of player is greater than entity's top. boolean isBottomAboveBottom = pY2 <= endY; // bottom of player is less than entity's bottom. boolean isTopAboveTop = pY1 <= startY; // player top is above entity's top. boolean isTopBelowTop = pY1 >= startY; // players top is below entity's top. boolean isTopAboveBottom = pY1 <= endY; // players top is above entity's bottom. /* Right / Left Collision Booleans */ boolean hittingRightSide = pX1 <= endX; /// players let side is hitting entity's right side. boolean hittingLeftSide = pX2 >= startX; // players right side is hitting entity's left side. /* Up / Down Collision Booleans */ boolean hittingTopSide = pY2 >= startY; // players bottom side is hitting entity's top side. boolean hittingBottomSide = pY1 <= endY; // players top side is hitting entity's bottom side. /* * All variables are setup O.O * Now to perform collision based off * the players moving direction. */ if (movingRight) { /* * Check if player is hitting left of entity. */ if (isTopBelowTop && isTopAboveBottom && hittingLeftSide) { /* * Players endingX is greater than entity's endingX, * even though the player's still inside of entity's Y axis * we'll return so the player doesn't get yanked backwards. */ if (pX2 > endX) { return; } /* * We've detected the Collision, prevent the player from moving * and adjust his position + 1 from the entity's. */ player.getGame().leftCollision = true; player.getGame().setMap1X(player.getGame().getMap1X() + 1); } if (isBottomBelowTop && isBottomAboveBottom && hittingLeftSide) { /* * Players endingX is greater than entity's endingX, * even though the player's still inside of entity's Y axis * we'll return so the player doesn't get yanked backwards. */ if (pX2 > endX) { return; } /* * We've detected the Collision, prevent the player from moving * and adjust his position + 1 from the entity's. */ player.getGame().leftCollision = true; player.getGame().setMap1X(player.getGame().getMap1X() + 1); } if (pY1 < startY && pY2 > endY && hittingLeftSide) { /* * Players endingX is greater than entity's endingX, * even though the player's still inside of entity's Y axis * we'll return so the player doesn't get yanked backwards. */ if (pX2 > endX) { return; } /* * We've detected the Collision, prevent the player from moving * and adjust his position + 1 from the entity's. */ player.getGame().leftCollision = true; player.getGame().setMap1X(player.getGame().getMap1X() + 1); } } if (movingLeft) { /* * Check if player is hitting right of entity. */ } if (movingDown) { /* * Check if player is hitting top of entity. */ } if (movingUp) { /* * Check if player is hitting bottom of entity. */ } }