0

EDIT:here is the full code: https://dl.dropboxusercontent.com/u/65678182/assignment1.rar Any possible help is highly appreciated!


I am trying to make a copy of the breakout game, but I have problems with checking if two objects (the ball and paddle) intersects.

I have this method for collision detection for now:

public static void handleCollisions()
    {

        System.out.println(ball.getBounds());        // just to check that they                  
        System.out.println(paddle.getBounds());      //are there and moving correct

        if (ball.getBounds().intersects(paddle.getBounds()))
            {
        System.out.println("collision");
            }
    }

I'm pretty sure the getBounds works as they should since I get these outputs from the println: java.awt.Rectangle[x=393,y=788,width=14,height=14] java.awt.Rectangle[x=350,y=350,width=100,height=10]

getBounds code:
    public static Rectangle getBounds()
    {
        return new Rectangle(x, y, radius*2, radius*2);                     
    }

and I can see them moving at one point they are overlapping, but the method never detects it.

I'm pretty new to this so hopefully its just some stupid mistake somewhere, and any help is appreciated. I can post some more code if necessary, but would prefer to not.

user3483682
  • 51
  • 1
  • 7
  • `Rectangle.intersects()` works and what you have shown us so far seems OK. The problem is elsewhere, in parts that you have not shown us. For example, are you sure you are actually calling `handleCollisions()` when you should be? Are you sure that `ball` and `paddle` correctly refer to the objects in question? When you print out their bounding box values at every frame, do you actually see them overlap? Try, as a test, initializing your ball position to directly on top of your paddle, and disabling movement; this will make it easier to see if your collision detection is working. – Jason C Apr 01 '14 at 05:33
  • If the output you've shown us is your *only* output then that suggests that you are not calling `handleCollisions()` on every frame, because you would expect to see more than one print out (plus the example you showed us doesn't actually intersect, but I'm assuming that's because it was just an arbitrary example). – Jason C Apr 01 '14 at 05:35
  • Got a runnable example? – MadProgrammer Apr 01 '14 at 05:36
  • @Jason I can see the ball moving towards the paddle, then over it. Tried to move the ball to paddle and not movement, still no detection even though i can see the ball on top of the paddle visually. – user3483682 Apr 01 '14 at 06:04
  • is there any way to pm? could give the program to you then MadProgrammer – user3483682 Apr 01 '14 at 06:04
  • See [Collision detection with complex shapes](http://stackoverflow.com/q/14574045/418556) for a robust approach in which Java-2D does all the 'heavy lifting'. – Andrew Thompson Apr 01 '14 at 07:01
  • I suppose you could just post a link to it here or something. – Jason C Apr 01 '14 at 07:39
  • just did that, thanks for your time :) – user3483682 Apr 01 '14 at 10:24

1 Answers1

3
java.awt.Rectangle[x=393,y=788,width=14,height=14] 
java.awt.Rectangle[x=350,y=350,width=100,height=10]

As you can see the second rectangle's y/height 350/10 but the first's y=788

Obviously they have no intersection one is above another

UPDATE One more thing

public static Rectangle getBounds()
{
    return new Rectangle(x, y, radius*2, radius*2);                     
}

If x and y are the ball's center the code should be

public static Rectangle getBounds()
{
    return new Rectangle(x-radius, y-radius, radius*2, radius*2);                     
}
StanislavL
  • 56,971
  • 9
  • 68
  • 98