Sumtinlazy
Sumtinlazy

Reputation: 347

Java Rectangle Intersect Always True

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() ;

}

Upvotes: 0

Views: 1129

Answers (1)

Matt C
Matt C

Reputation: 4555

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().

Upvotes: 2

Related Questions