much.love wrote:I coded collision myself and it works just fine for the most part. Especially collision with the pong-paddles works just fine, but it doesn't really look nice if part of the ball is in the paddle before flying back and using my methode, the ball gets reflected, even if it aleady passed the paddle, but didn't quite reach the end of the scrren just jet.
Your collision checks whether the two rectangles are overlapping just fine, but this probably isn't what you want.
One way to achieve what you want might be to give the ball a boolean variable which determines if it should check for collisions or not. For example, if the left side of the ball moves past the right side of the left paddle and they haven't collided, the paddle has already been beaten and the variable can be set to false to let the ball move off the screen.
much.love wrote:Collision with the top and the bottom of the screen sometimes leads to the ball bein "caught" and sort of being stuck.
This is caused by the ball switching y directions when it's off the screen, moving for the next frame and still being off the screen, therefore switching directions again!
One possible way to remedy this is by changing this:
Code: Select all
if ball.y <= 0 or ball.y >= (600-ball.height) then
ball.diry = ball.diry*(-1)
end
To this:
Code: Select all
if ball.y <= 0 then
ball.diry = math.abs(ball.diry)
end
if ball.y >= (600-ball.height) then
ball.diry = -math.abs(ball.diry)
end
math.abs() will return the absolute value of the number (for example, math.abs(123) and math.abs(-123) will both return 123). If the ball moves off the up of the screen, math.abs(ball.diry) will definitely return a positive number, and if it moves off the bottom, -math.abs(ball.diry) will definitely return a negative number.
I hope this helps!