# Has the ball hit my paddle?

• May 3rd, 2012, 11:52 AM
JakkyD
Has the ball hit my paddle?
Hi all, I'm creating a game of Pong and I'm having trouble seeing whether the ball has hit the paddle. My game so far has a ball that bounces around the screen, and a paddle on the left which can be moved with the up and down key.

Here is how I've drawn said objects:

Code :

``` // The ball at the current x, y position (width, height)   g.setPaint( Color.red ); g.fill( new Ellipse2D.Double( BALLX-HALF_BALL_SIZE, BALLY-HALF_BALL_SIZE, BALL_SIZE, BALL_SIZE ) );   // Paddle   g.setPaint( Color.pink ); g.fill( new Rectangle2D.Double( PADDLEX, PADDLEY, PADDLE_WIDTH, PADDLE_HEIGHT ) );```

BALLX & PADDLEX = X coordinate of the ball/paddle and same goes to BALLY & PADDLEY with the y coordinate.

What would be the best way of checking whether the ball has collided with paddle?

I tried this:

Code :

``` public Rectangle getPaddleBounds(){ return new Rectangle(PADDLEX,PADDLEY,PADDLE_WIDTH,PADDLE_HEIGHT); }   public Rectangle getBallBounds(){ return new Rectangle(BALLX,BALLY,BALL_SIZE,BALL_SIZE); }   public void checkCollisions(){ if (getPaddleBounds().intersects(getBallBounds())) collision = true; else collision = false; }```

but it didn't work (and I realise the ball isn't a rectangle but ellipse doesn't work). Any help would be greatly appreciated

Thank you
• May 3rd, 2012, 12:13 PM
KevinWorkman
Re: Has the ball hit my paddle?
Seems reasonable to me. What exactly do you mean when you say it "doesn't work"?
• May 3rd, 2012, 12:16 PM
Norm
Re: Has the ball hit my paddle?
Quote:

it didn't work
Can you describe what happens? Does one shape completly ignore the other? Or is there slight penetration befor e the collision is detected?
Is the collision variable ever set true? When does that happen? Add a println to show the values when it happens.

Try debugging the code by adding println statements to print out the values of the bounds of each shape so you see what the computer is seeing.
• May 3rd, 2012, 12:33 PM
JakkyD
Re: Has the ball hit my paddle?
Ahh my first post on this forum and it's a shambolic one. I apologise. I didn't call the checkCollisions(); function while running the main program loop.
I added this and now it's working fine.

One last thing, when I open my program I am unable to move the paddle, firstly I have to click on the Terminal window and then back onto the application to be able to play the game. Is there anyway I don't have to do this?

http://img651.imageshack.us/img651/5913/19828967.png
• May 3rd, 2012, 12:37 PM
Norm
Re: Has the ball hit my paddle?
How are you controlling the movements of the shapes?
What kind of listeners are you using? Does the component with the listeners have the focus?
There are methods you can call to request that the component get the focus.
• May 3rd, 2012, 01:14 PM
JakkyD
Re: Has the ball hit my paddle?
Quote:

Originally Posted by Norm
How are you controlling the movements of the shapes?
What kind of listeners are you using? Does the component with the listeners have the focus?
There are methods you can call to request that the component get the focus.

I am using this listener

Code :

``` class Transaction implements KeyListener // When character typed { public void keyPressed( KeyEvent e) // Obey this method { // Key typed includes specials switch ( e.getKeyCode() ) // Character is { case KeyEvent.VK_Q: // Q key PADDLEY = PADDLEY -10; break; case KeyEvent.VK_A: // A key PADDLEY = PADDLEY + 10; break; case KeyEvent.VK_UP: // Up arrow PADDLERIGHTY = PADDLERIGHTY -10; break; case KeyEvent.VK_DOWN: // Down arrow PADDLERIGHTY = PADDLERIGHTY +10; break; } // x,y could send to a server instead of calling // Call update method repaint(); }```

are you saying there is a way I can set the focus to this listener?
• May 3rd, 2012, 01:16 PM
Norm
Re: Has the ball hit my paddle?
The component with the listener that is shown in GUI must have the focus.