0

I am working on a project for school and have implemented a way to check if the two objects intersect. I have defined two rectangles at but when it checks to see if they are intersecting it is always true, even if they are on opposite sides of the JFrame. Boolean Collision is set to false at the start and x and y are the coordinates of two photos on the screen.

public Rectangle Bounds() {
    return (new Rectangle(x, y, 225, 225)) ;
}
public Rectangle TrollBounds() {
    return (new Rectangle(x, y, 24, 24)) ;
}
Rectangle hitbox = Bounds() ;
Rectangle Hitbox2 = TrollBounds() ;

The part that checks for intersections

if (hitbox.intersects(Hitbox2)) {
            collision = true ;
            System.out.println("collision") ;
        } else {
            collision = false ;

The whole Class

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class Screen extends JPanel implements Runnable, KeyListener {

int health = 1200;

boolean collision = false ;

BufferedImage Troll ;
int cubex = 600 ;
int cubey = 100 ;

int cubexx = 200 ;
int cubeyy = 200 ;

public boolean running = true ;
BufferedImage Player;
int x = 100 ;
int y = 100 ;

boolean Jumping = false ;
int startJump = 10 ;

public void Jump() {

}

boolean up = false;
boolean down = false;
boolean left = false;
boolean right = false;

public Screen() {
    loadImages();
    Thread thread = new Thread(this);
    thread.start();
}

private void loadImages() {
    try {
        Player = ImageIO.read(getClass().getResource("/sanic.png"));
        Troll = ImageIO.read(getClass().getResourceAsStream("/Trollface.png")) ;
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void keyPressed(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_W) {
        up = true ;
    }
    if (e.getKeyCode() == KeyEvent.VK_A) {
        left = true ; 
    }
    if (e.getKeyCode() == KeyEvent.VK_D) {
        right = true ;
    }
    if (e.getKeyCode() == KeyEvent.VK_S) {
        down = true ;
    }
    if (e.getKeyCode() == 31) {
        Jump() ;
    }


}

public void keyReleased(KeyEvent e) {

    if (e.getKeyCode() == 87) {
        up = false ;
    }
    if (e.getKeyCode() == 65) {
        left = false; 
    }
    if (e.getKeyCode() == 68) {
        right = false ;
    }
    if (e.getKeyCode() == 83) {
        down = false ;
    }

}

@Override
public void keyTyped(KeyEvent e) {


}

@Override
public void run() {

    while(running) {
        //gravity

        //gravity
        //Player movement
        if (left) {
            if (x <= -10) {
                x = 1464 ;
            }
            x = x - 2 ;
        }
        if (up) {
            if(y <= -112) {
                y = 910 ;
            }
            y = y - 2 ;
        }
        if (right) {
            if (x >= 1416) {
                x = -24 ;
            }
            x = x + 2;
        }
        if (down) {
            if (y >= 900) {
                y = -10 ;
            }
            y = y + 2 ;
        }
        //Player movement

        //ball movement
        if (cubey > y) {
            cubey-- ;
        }
        if(cubey < y) {
            cubey++ ;
        }
        if (cubex > x) {
            cubex-- ;
        }
        if (cubex < x) {
            cubex++ ;
        }
        //ball movement

        if (hitbox.intersects(Hitbox2)) {
            collision = true ;
            System.out.println("collision") ;
        } else {
            collision = false ;
        }

        if (collision) {
            health -- ;
        }
        System.out.println(collision) ;

        repaint() ;

        try {
            Thread.sleep(3) ;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    //Graphical loop start
    g.drawImage(Player, x, y, null) ;   
    g.drawImage(Troll, cubex, cubey, null) ;
    g.drawString("SANIC", x, y) ;
    g2d.fillRect(220, 200, health, 50) ;
    //Graphical loop end

}

public Rectangle Bounds() {
    return (new Rectangle(x,y,cubex+225,cubey+225)) ;
}
public Rectangle TrollBounds() {
    return (new Rectangle(cubex,cubey,cubex+24,cubey+24)) ;
}
Rectangle hitbox = Bounds() ;
Rectangle Hitbox2 = TrollBounds() ;

}

Sumtinlazy
  • 337
  • 5
  • 17
  • 4
    Both of your rectangles include the same starting point `(x, y)`. Thus no wonder they're always intersect. – Tagir Valeev May 29 '15 at 04:28
  • Nope, returns `false` for me (`bounds = new Rectangle(100, 100, 225, 225)` and `trollBounds = new Rectangle(0, 0, 24, 24)`) – MadProgrammer May 29 '15 at 04:29
  • 2
    In circumstances like this, you should paint the `Rectangle`s to the screen (`Graphics2D#draw(Shape)`) to see what they actually are ;) – MadProgrammer May 29 '15 at 04:30
  • See [Collision detection with complex shapes](http://stackoverflow.com/a/14575043/418556) for a working example. – Andrew Thompson May 29 '15 at 04:54
  • Could you add the rest of your code so we can see if there are any logic errors? Perhaps a part where you just printed out collision after setting it, but without calling the intersect method again? Those sort of things. Thanks. – Matt C May 29 '15 at 05:51

1 Answers1

2

If this code is ran how it is ordered, with making hitbox and Hitbox2 right after each other and without changing x and y in between, then it should return true.

It will make a rectangle from x, y to 255, 255 and another from x,y to 24,24 and those rectangles do intersect.

Here is the JavaDoc on Rectangle & Shape.intersects().

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Matt C
  • 4,470
  • 5
  • 26
  • 44