where the ball in my game starts to reverses in movement
where the ball in my game starts updating in movement (well, close enough)
I’m utilizing the raylib library to create the game. It’s a simple game of ping pong where the ball should only bounce off of the window barriers on the y-axis and any rectangles on the x-axis. Instead, The ball behaving out of what I intended it to because it keeps bouncing back and forth with the bottom of the screen, and the middle of the screen(as shown in images). I know the code might not be up to high standards but that’s just because I’m relatively new to creating games. I would like to know why it’s behaving like this so I can prevent similar cases like this from happening again. I’m not so much so looking for a replacement for my code, just an explanation as to why this is happening.
int collisionDetection(float ballX, float ballY, int ballRadius, float rectangleX, float rectangleY, float rectangleWidth, float rectangleHeight, int screenWidth, int screenHeight)
{
//collision with left rectangle
if(ballX <= rectangleX + rectangleWidth)
{
if((ballY + ballRadius <= rectangleHeight + rectangleY) && (ballY + ballRadius >= rectangleY))
{
//collision found with left rectangle. Use update().
return 1;
}
}
if(ballY < 0)
{
//collision found with top barrier(out of bounds). Use update().
return 2;
}
//collision with right rectangle
if(ballX + ballRadius >= rectangleX)
{
if((ballY + ballRadius <= rectangleHeight + rectangleY) && (ballY + ballRadius >= rectangleY))
{
//collision found with right rectangle. Use reverse().
return 3;
}
}
if(ballY + ballRadius > screenHeight)
{
//collision found with bottom barrier(out of bounds). Use reverse().
return 4;
}
//no collision detected
return 0;
}```
Overflow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.