1
\$\begingroup\$

This is the code I'm using to resolve collisions on the players left side. It works when I'm on the ground, but if I'm falling, it does not work.

public static boolean onWall(Rectangle rect1, Rectangle rect2){

    if(rect1.x > rect2.x && rect1.x <= rect2.x + rect2.width && ((rect1.y > rect2.y && rect1.y < rect2.y + rect2.height) || rect1.y + rect1.height > rect2.y && rect1.y + rect1.height < rect2.y + rect2.height)){
        rect1.x = rect2.x + rect2.width;
        return true;
    }

    return false;

}
\$\endgroup\$
  • 2
    \$\begingroup\$ Do not post your solution as an edit into your question, instead post it as an answer. \$\endgroup\$ – Tyyppi_77 Jan 2 '19 at 7:02
1
\$\begingroup\$

Sorry finally figured it out, heres my code just in case anyone else needs help

public static boolean yCollision(Rectangle p, ArrayList<Rectangle> a, Vector2 v){
    for(Rectangle r: a){
        if(p.y + v.y <= r.y + r.height && ((p.x > r.x && p.x < r.x + r.width) || (p.x + p.width > r.x && p.x + p.width < r.x + r.width)) && p.y > r.y + (r.height / 2)){
            p.y = r.y + r.height;
            return true;
        }
        if(v.y > 0){
            if(p.y + p.height + v.y > r.y && p.y + v.y + p.height < r.y + r.height && ((p.x > r.x && p.x < r.x + r.width) || (p.x + p.width > r.x && p.x + p.width < r.x + r.width)) && p.y > r.y){
                p.y = r.y - p.height;
                return true;
            }
        }
    }
    return false;
}

public static String xCollision(Rectangle p, ArrayList<Rectangle> a, Vector2 v){
    for(Rectangle r: a){
        if(p.x + v.x <= r.x + r.width && p.x + v.x >= r.x && ((p.y > r.y  && p.y < r.y + r.height) || (p.y + p.height > r.y && p.y + p.height < r.y + r.height))){
            p.x = r.x + r.width;
            return "left";
        }
        if(p.x + p.width + v.x >= r.x && p.x + p.width + v.x <= r.x + r.width && ((p.y > r.y  && p.y < r.y + r.height) || (p.y + p.height > r.y && p.y + p.height < r.y + r.height))){
            p.x = r.x - p.width;
            return "right";
        }
    }
    return "none";
}
\$\endgroup\$
  • 2
    \$\begingroup\$ This answer would be even better if it explained what you changed and why that solved the problem. \$\endgroup\$ – DMGregory Jan 3 '19 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.