And another pong problem!
I had a slight collision problem with my pong game. If the ball happens to collide with the "side" of the paddle, the ball will "stick" to the paddle and slowly move to the side of the paddle, and then fall off. I have a feeling this is happening because on the side, the ball can collide with the paddle for multiple loops of the game, because there is more than a single point it is touching, but I can't think of a way to fix this. Here is the code dealing with the collision:
Code:
if(ball.posY >= windowHeight-85 && ball.posX >= paddle.posX && ball.posX <= paddle.posX + paddle.width)
{
ball.speedY = -ball.speedY;
score++;
if(score % 5 == 0)
{
// speed the ball up
if(ball.speedX > 0)
ball.speedX++;
else
ball.speedX--;
if(ball.speedY > 0)
ball.speedY++;
else
ball.speedY--;
}
}
Re: And another pong problem!
One possible solution: don't try to solve all collisions in one block. Choose the conditions which should make the speedX positive and make speedX definitely positive in that block using Math.abs. Same for speedX negative, speedY positive and speedY negative. This way you won't have your speed? flip-flopping directions.