Search code examples
javacollision-detectionrectangles

Why does the second rectangle collision not work when the first does?


I don't know why this happens. The first collision works perfectly. If I hit the bottom, It stops. If I hit the top, it stops at the top. But you can phase through the second platform. Does anybody know why it's doing this? Sorry, new to java. I should mention, its the bottom of the second platform that im having trouble with.

public void customUpdate() {  // Remember, all logic goes in here
        if(charX > 780) {
            charX = 0;
        }
        if(charX < 0) {
            charX = 780;
        }
        if(gravity) {
            charVY += 1;
            charY += charVY;
        }
        if(charY > 670-15) {
            charY = 0;
        }
        if(buttonOne) {
            exit = true;
        }

        if(charY+30 >= wallY) {
            charY = wallY-30;
            charVY = 0;
            wallCollision = true; // numOfJumps set to 0 because it increases by 1 every time you jump, allowing as many jumps as the programmer desires
            numOfJumps = 0;
            secHasPassed = false;
        }

        if(charY < platOneY &&
                charX < platOneX + platOneW && // Collision for plat 1 and so on
                charX + charW > platOneX &&
                charY < platOneY + platOneH &&
                charH + charY > platOneY) { //TODO  Get it so that it stops him when he hits the bottom
            charY = platOneY-30;
            numOfJumps = 0;
            charVY = 0;
            wallCollision = true; // Gets top of the platform collision
        }

        if(charY > platOneY &&
                charX < platOneX + platOneW && // Collision for plat 1 and so on
                charX + charW > platOneX &&
                charY < platOneY + platOneH &&
                charH + charY > platOneY) { //TODO  Get it so that it stops him when he hits the bottom
            charY = platOneY+platOneH+1;
            numOfJumps = maxJumps;
            charVY = 0;
            wallCollision = true; // Gets bottom of platform collision
        }

        if(charY > platTwoY &&
                charX < platTwoX + platTwoW &&
                charX + charW > platTwoX &&
                charY < platTwoY + platTwoH &&
                charH + charY > platTwoY) {
            charY = platTwoY+platTwoH+1;
            numOfJumps = maxJumps;
            charVY = 0;
            wallCollision = true;
            System.out.println("Bottom");
        }

        if(charY < platTwoY &&
                charX < platTwoX + platTwoW &&
                charX + charW > platTwoX &&
                charY < platTwoY + platTwoH &&
                charH + charY > platTwoY) {
            charY = platTwoY-30;
            numOfJumps = 0;
            charVY = 0;
            wallCollision = true;
        }

        if(charX < platThreeX + platThreeW &&
                charX + charW > platThreeX &&
                charY < platThreeY + platThreeH &&
                charH + charY > platThreeY) {
            charY = platThreeY-30;
            numOfJumps = 0;
            charVY = 0;
            wallCollision = true;
        }

        if(canMoveL) { // Movement Logic
            charX -= 5;
        }
        if(canMoveR) {
            charX += 5;
        }
        if(charVY < -15) {
            charVY = -15;
        }

    }

Solution

  • if (charY > platTwoY - 15) {
           charX < platTwoX + platTwoW &&
           charX + charW > platTwoX &&
           charY < platTwoY + platTwoH &&
           charH + charY > platTwoY) {
               charY = platTwoY + platTwoH + 1;
               charVY = 0;
               numOfJumps = maxJumps;
               wallCollision = true;
    }
    

    I got it working. I don't know why I didn't spot this.