tfstorm
tfstorm

Reputation: 13

Java: Help determining a collision between two rectangles

I'm creating a game, pacman specifically as part of my coursework and I'm having problems when it comes to collision-detection using Rectangles.

The problem I'm experiencing is that even though on screen it is clear to see that the characters aren't colliding checking the intersection is practically always returning true. The output below explains what I mean:

Pacman details: x 17.0 y 16.0 Inky details: x 22.0 y 13.0 Collision after calling intersects(): true Pacman details: x 18.0 y 16.0 Inky details: x 23.0 y 13.0 Collision after calling intersects(): true

I have the Rectangles set up as follows:

public Rectangle pacmanBounds(){
    return new Rectangle(pacRow, pacColumn, 22, 22);
}
public Rectangle ghostBounds(){
    return new Rectangle(ghostRow, ghostColumn, 22, 22);
}

The height and width have been hardcoded for testing purposes, but these are the actual image size.

I'm checking for collision as follows everytime the JPanel is repainted:

public boolean checkCollision(){
    Rectangle pacmanBounds = pacman.pacmanBounds();
    //currently commented out for testing
    /*if(pacmanBounds.intersects(inky.ghostBounds()) || pacmanBounds.intersects(blinky.ghostBounds())
            || pacmanBounds.intersects(pinky.ghostBounds()) || pacmanBounds.intersects(clyde.ghostBounds())){
        System.out.println("Oh no!");
        return true;
    }*/

    System.out.println("Pacman details: x " + pacmanBounds.getX() + " y " + pacmanBounds.getY() + " ");
    System.out.println("Inky details: x " + inky.ghostBounds().getX() + " y " + inky.ghostBounds().getY());
    System.out.println("Collision after calling intersects(): " + pacmanBounds.intersects(inky.ghostBounds()));
    return false;
}

At this point in time I've pretty much ran out of ideas on how to solve this problem so any advice you guys can give would be greatly appreciated!

Upvotes: 1

Views: 1538

Answers (1)

Gordon Gustafson
Gordon Gustafson

Reputation: 41209

Assuming getX() and getY() return the point of the upper-left coordinate of the rectangle, then these will be the bounds of the rectangles for each call:

Pacman details: x 17.0 y 16.0 Inky details: x 22.0 y 13.0 Collision after calling intersects(): true

The Pacman rectangle would be its upper-left coordinates plus 22 in each direction, giving you a rectangle with lower-right corner (39.0, 38.0), which most definitely intersects with Inky's if his upper-right corner is (22.0, 13.0) and lower-left corner is (44.0, 35.0)

That's what it looks like to me. Do you mean the 22.0's to be the bounds of the Pacman and Inky rectangles in pixels or in squares? If those assumptions are correct, then Inky's lower-left corner (22.0, 35.0) is literally located completely inside Pacman, which probably isn't what you want. The problem could be a number of things, and its hard to say what it might be without knowing seeing some more of the code and knowing what exactly the rectangles mean. :D

Upvotes: 1

Related Questions