Reputation: 13
This is my code for the ball collision in arkanoid:
Rectangle intersection = Rectangle.Intersect(block.Rect2D, ball.BallRec);
if (intersection.Width > intersection.Height)
{
ball.yVel = -ball.yVel;
}
else if (intersection.Width < intersection.Height)
{
ball.xVel = -ball.xVel;
}
else
{
ball.xVel = -ball.xVel;
ball.yVel = -ball.yVel;
}
Unfortunately the ball sometimes "melts" into the blocks and bounces weirdly, especially when its at higher speed. How can I fix that?
Upvotes: 0
Views: 1094
Reputation: 9118
When collision is detected it is not sufficient to just change the direction of the ball, you need to change the position too. If the ball moved 20 pixels, and is now 5 pixels into the block, then you need to move the ball 5 pixels away from the block.
You will also need to check if the block you are detecting collisions for was between the ball's old location and the new one.
Upvotes: 1