Right now I have three classes from following a tutorial I found online.

It draws a red square and a blue square to the screen and lets the user control the red square.
The issue I am having is getting it so that when they intersect from the bottom of the red and the top of the blue I need it to keep the Y coordinate (Which would basically be the Y coordinate of the blue square minus the height which would bee 100, correct?) and then the X coordinate should not be changed, then I would also need to do the same for intersection with the top of the red with the bottom of the blue, and the same idea for the left and right sides correct? Would there be an easier way of doing this?

public void checkCollisions()
    {
            if (player.getBounds().intersects(enemy.getBounds()))
            player.collision = true;
                else
                player.collision = false;
    }

    public void drawBuffer()
    {
        Graphics2D b = buffer.createGraphics();
        b.setColor(Color.black);
        b.fillRect(0,0,800,600);
        if (player.collision == false)
        {
            b.setColor(Color.red);
            b.fillRect(player.getX(),player.getY(),player.getWidth(),player.getHeight());
            b.setColor(Color.blue);
            b.fillRect(enemy.getX(),enemy.getY(),enemy.getWidth(),enemy.getHeight());
            b.dispose();
        }
        else if (player.collision == true)
        {
            b.setColor(Color.red);
            b.fillRect(player.getXCollide(),player.getYCollide(),player.getWidth(),player.getHeight());
            b.setColor(Color.blue);
            b.fillRect(enemy.getX(),enemy.getY(),enemy.getWidth(),enemy.getHeight());
            b.dispose();
        }

        
        
    }

That is the code for detecting when they collide and this is what I have tried doing when they intersect.

public int getYCollide()
    {
        y = y - height;
        return y;
    }
    
    public int getXCollide()
    {
        x = x - width;
        return x;
    }

Now I am having trouble getting it to do the x of the red square (player) subtracting the x of the blue square (enemy) minus the width.

The "getXCollide()" and all the other code relating to those are in a class called entity and the other chunk of code is from DrawPanel.
DrawPanel creates Entity Player and Entity Enemy.
Should I be doing the X and Y subtractions in the DrawPanel class?

Thank you for the quick reply, I didn't completely look over the source code from that website yet but I did compile it to see what was going on and this does look like I could get a lot of use out of it! Thank you!

EDIT: That website did help me come up with an idea that makes it so when the sides hit they just collide, I am now just having trouble with the Y's colliding.

Right now when the sides hit they just hit and nothing happens, with both the left and the right sides. However when I try to do the same with the Y's it jumps to the side.

public void checkCollisions()
    {
            if (player.getBounds().intersects(enemy.getBounds()) && player.x < enemy.x){
            player.collision = true;
            player.x = enemy.x - 100;
        }
        else if (player.getBounds().intersects(enemy.getBounds()) && player.x > enemy.x){
            player.collision = true;
            player.x = enemy.x + 100;
        }
        else if (player.getBounds().intersects(enemy.getBounds()) && player.y < enemy.y){
            player.collision = true;
            player.y = enemy.y - 100;
        }
        else if (player.getBounds().intersects(enemy.getBounds()) && player.y > enemy.y){
            player.collision = true;
            player.y = enemy.y + 100;
        }        
                else
                player.collision = false;
    }

Edited 5 Years Ago by Zabzacon: n/a

Hello Zabzacon,

I too had a problem with my collision detection but it is now fixed.

What exactly is supposed to happen when player.collision = true?
What I did for my program (an applet)is a simple if statment for my collision detection. (if the x coord of the player is the same (give or take a few pixles)as my monster, the monster got moved 500,000 pixles off screen)

But first try making your

else if (player.collision = true)
an if statment.

Try that and see if anything is different!

javanoob101

I have attached my 3 classes so that you may see exactly what is going on.

When they collide it is changing the player's x coordinate so that it is 100 (the width) less than the X of the enemy so that it is being stopped. (Basically think of the enemy as a wall).
So when player.collison == true it redraws the player at the new x coordinate

Attachments
import javax.swing.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.Graphics2D;
import java.awt.*;

public class DrawPanel extends JPanel implements KeyListener
{
    BufferedImage buffer;
    Entity player;
    Entity enemy;
    
    public void Initialize()
    {
        buffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
        player = new Entity(100,100);
        enemy = new Entity(400,400);
    }

    public void update()
    {
        player.move();
    }

    public void checkCollisions()
    {
            if (player.getBounds().intersects(enemy.getBounds()) && player.x < enemy.x){
            player.collision = true;
            player.x = enemy.x - 100;
        }
                        else
                player.collision = false;
        if (player.getBounds().intersects(enemy.getBounds()) && player.x > enemy.x){
            player.collision = true;
            player.x = enemy.x + 100;
        }
                        else
                player.collision = false;
        if (player.getBounds().intersects(enemy.getBounds()) && player.y < enemy.y){
            player.collision = true;
            player.y = enemy.y - 100;
        }
                        else
                player.collision = false;
        if (player.getBounds().intersects(enemy.getBounds()) && player.y > enemy.y){
            player.collision = true;
            player.y = enemy.y + 100;
        }        
                else
                player.collision = false;
    }

    public void drawBuffer()
    {
        Graphics2D b = buffer.createGraphics();
        b.setColor(Color.black);
        b.fillRect(0,0,800,600);
        if (player.collision == false)
        {
            b.setColor(Color.red);
            b.fillRect(player.getX(),player.getY(),player.getWidth(),player.getHeight());
            b.setColor(Color.blue);
            b.fillRect(enemy.getX(),enemy.getY(),enemy.getWidth(),enemy.getHeight());
            b.dispose();
        }
        else if (player.collision == true)
        {
            b.setColor(Color.red);
            b.fillRect(player.getX(),player.getY(),player.getWidth(),player.getHeight());
            b.setColor(Color.blue);
            b.fillRect(enemy.getX(),enemy.getY(),enemy.getWidth(),enemy.getHeight());
            b.dispose();
        }

        
        
    }

    public void drawScreen()
    {
        Graphics2D g = (Graphics2D)this.getGraphics();
        g.drawImage(buffer,0,0,this);
        Toolkit.getDefaultToolkit().sync();
        g.dispose();
    }

    public void startGame()
    {
        Initialize();
        while(true)
        {
            try
            {
                update();
                checkCollisions();
                drawBuffer();
                drawScreen();
                Thread.sleep(15);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

    public DrawPanel()
    {
        setIgnoreRepaint(true);
        addKeyListener(this);
        setFocusable(true);
    }

    public void keyTyped(KeyEvent e)
    {

    }

    public void keyPressed(KeyEvent e)
    {
       int key = e.getKeyCode();
       if (key == KeyEvent.VK_LEFT)
           player.left = true;
       if (key == KeyEvent.VK_RIGHT)
           player.right = true;
       if (key == KeyEvent.VK_UP)
           player.up = true;
       if (key == KeyEvent.VK_DOWN)
           player.down = true;
    }

    public void keyReleased(KeyEvent e)
    {
              int key = e.getKeyCode();
       if (key == KeyEvent.VK_LEFT)
           player.left = false;
       if (key == KeyEvent.VK_RIGHT)
           player.right = false;
       if (key == KeyEvent.VK_UP)
           player.up = false;
       if (key == KeyEvent.VK_DOWN)
           player.down = false;

    }
}
import java.awt.Rectangle;

public class Entity
{
    int x,y,speed,width,height;
    int rightside, leftside;
    boolean up, down, left, right, collision;
    
    public Entity(int x, int y)
    {
        this.x = x;
        this.y = y;
        rightside = 100;
        leftside = 100;
        speed = 3;
        width = 100;
        height = 100;
        up = false;
        down = false;
        left = false;
        right = false;
        collision = false;
    }
    
    public int getX()
    {
        return x;
    }
    
    public int getY()
    {
        return y;
    }
    
    public int getWidth()
    {
        return width;
    }
    
    public int getHeight()
    {
        return height;
    }
    
    public int getYCollide()
    {
        y = y - height;
        return y;
    }
    
    public int getXCollide()
    {
        x = this.x - 5;
        return x;
    }
    
    public Rectangle getBounds()
    {
        return new Rectangle(getX(),getY(),getWidth(),getHeight());
    }
    
    public void move()
    {
        if (up)
        y -= speed;
        if (down)
        y +=speed;
        if(left)
        x -= speed;
        if (right)
        x += speed;
    }
}
import javax.swing.*;

public class Gui
{
     JFrame window;
     DrawPanel panel;

    public Gui()
    {
        window = new JFrame("Collision detection, movement, double buffering, and a game loop!");
        panel = new DrawPanel();
        window.setSize(800,600);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.getContentPane().add(panel);
        window.setVisible(true);
    }

    public void go()
    {
    panel.startGame();
    }
    
    public static void main(String[]args)
    {
        Gui game = new Gui();
        game.go();
    }
}

It occurs because of the order you are checking the relative locations once they have intersected.

You first check the x values and update them +- 100, so consider how that affects the intersects() check in the two blocks that check the y values.

This article has been dead for over six months. Start a new discussion instead.