joejoethemonkey
joejoethemonkey

Reputation: 63

checking if 2 rectangles WILL overlap

I have been trying to do player intersection with a small project i'm doing, and I can't seem to make it work. I got the Intersection to work with the player and the wall, but it's very buggy, by buggy I mean, it draws the player in the wall then moves him back instantly. (Check Gyazo for gif of this). I'm pretty sure the problem is that it only checks if the player is in the wall, and not WILL be in the wall, but I can't seem to figure out how to check this. This is what I have so far:

public void intersectsBox2(Rectangle r, Rectangle r2) {
    P1 = new Point((int) r.getMinX(), (int) r.getMinY());
    P2 = new Point((int) r.getMaxX(), (int) r.getMaxY());
    P3 = new Point((int) r2.getMinX(), (int) r2.getMinY());
    P4 = new Point((int) r2.getMaxX(), (int) r2.getMaxY());
    if ((P2.y < P3.y || P1.y > P4.y || P2.x < P3.x || P1.x > P4.x)
            && !intersectsBox(playerRectangle(), noWalls[0])) {
        isInsideWalls = true;
    }
}

// Gets the players rectangle
public Rectangle playerRectangle() {
    return new Rectangle(9 + dx, 23 + dy, 54, 90);
}

This is to make the player Move:

public void playerMovement() {
    if (isInsideWalls) {
        System.out.println("YOU ARE IN THE BOX!");
        if (animation == down) {
            dy -= moveSpeed;
            isInsideWalls = false;
        } else if (animation == up) {
            dy += moveSpeed;
            isInsideWalls = false;
        } else if (animation == left) {
            dx += moveSpeed;
            isInsideWalls = false;
        } else if (animation == right) {
            dx -= moveSpeed;
            isInsideWalls = false;
        }
    } else {
        // Moves the player
        if (moving == downMove) {
            dy += moveSpeed;
            moving = 0;
        } else if (moving == upMove) {
            dy -= moveSpeed;
            moving = 0;
        } else if (moving == leftMove) {
            dx -= moveSpeed;
            moving = 0;
        } else if (moving == rightMove) {
            dx += moveSpeed;
            moving = 0;
        }
    }

This is to check for the intersection:

//Checks for intersection
for (int i = 0; i < 9; i++) {
    for (int j = 0; j < 9; j++) {
        intersectsBox2(walls[i][j], playerRectangle());
    }
}

Not really sure if this is needed but heres the full Game.java if you need to see this: http://pastebin.com/GrDy689d

Also here is the Gif of the problem: http://i.gyazo.com/1f31f739897af78f81e61cf22ac772db.mp4

P.S: It's on purpose I can only go into 1 box at the moment for testing purposes.

Upvotes: 0

Views: 58

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49920

You could move the player, then check to see if it is in a wall and, if it is, undo the move (or better, calculate a new location as the result of a move, check it, and if it is good, only then move the player there). Note that this assumes a single move can't put you all the way on the other side of a wall, but then it looks like your code does, too.

Upvotes: 2

Related Questions