Pong Paddle Not Stopping Ball At Certain Speed
First off, sorry for the long thread title. I was trying to convey my error.
I'm making a pong-like game, and I have everything set up. There is one paddle which the player controls by moving his mouse; if the cursor is on the left half of the screen, the paddle follows, and vice-versa.
Now, the paddles reflect the ball perfectly as long as the ball's "speed" (in pixels per repaint) is less than three. The game starts the ball with a speed of 1, and each time the ball is hit, it increases by one. When the speed reaches three, the ball just flies right past my paddles.
A few notes before my code:
- getXPos() and getYPos() get the top left pixel. I add to or subtract from that amount to get other pixels of the shape.
- The ball is a circle with a radius of 10. The rectangle is 5x40 (5 wide, 40 high).
- A negative value for xSpeed denotes travel to the left; positive to the right.
Here is my code for paddle collision detection:
Code:
// If ball collided with player's paddle...
if(ball.getXPos() < windowWidth / 2)
{
if(ball.getXPos() == paddle.getXPos() + 5 &&
(ball.getYPos() + RADIUS >= paddle.getYPos() &&
ball.getYPos() + RADIUS <= paddle.getYPos() + 40))
{
// ... turn ball around and increase speed.
ball.setXSpeed(-(ball.getXSpeed() - 1));
}
}
// If ball collided with opponent's paddle...
else
{
if(ball.getXPos() + (RADIUS * 2) == paddle.getXPos() &&
(ball.getYPos() + RADIUS >= paddle.getYPos() &&
ball.getYPos() + RADIUS <= paddle.getYPos() + 40))
{
// ... turn ball around and increase speed.
ball.setXSpeed(-(ball.getXSpeed() + 1));
}
}
My program's complete code is quite long (about 500 lines in total [counting empty newlines and Javadoc]), but if you need me to post it, let me know. Thank you in advance for your help.
I Discovered a tennis game
Good morning/Good day/Good evening/Good afternoon/Hello/Hi/Hei/Hola,
I/we/my friend/my sister/my brother/my cousin/my uncle/my dad/somebody saw/played/heard of/tried/found/discovered/watched/learned of/noticed/came across/uncovered/spotted a great/a marvellous/an incredible/ a fantastic/ an unbelievable/ a smashing/a funny/an amazing/ a wonderful/ a stunning/ a cool/ a groovy/ a spectacular/an excellent/ a superb/ a fabulous game/ fitness tool/ product/article/tennis simulator/sport/pastime/entertainment/tennis training equipment/tennis tool/gadget/sport gadget/sport equipment/training tool it's called tennis trainer/they say it is the tennis trainer/ the tennis trainer/tennistrainer/it's named tennis trainer/ the logo is tennis trainer , watch the video/see the clip/check it out/go to the site/try it now/buy it now/play and have fun/ fun and fitness/for all tennis lovers/the perfect sport gift.
By/bye bye/ciao/see you/regards/see ya/later