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;
    }

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

Could anyone compile those classes and tell me what im doing wrong?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.