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:
// 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:
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