Embrace_It
New Member
- Reaction score
- 9
Atm, I'm making a Pong Clone. While building the game up from strach, I'm reading articles about collision detection. The following is the collision detection from Aaron's Game Programming Tutorials:
Now assume that we move the ball, then check for collisions, and that the ball has a speed of 10 pixels/second towards the right. Also, imagine that the ball is currently 9 pixels away from a paddle (in the x-direction). The next time we move the ball, this function registers a collision, since the ball moves "into" the paddle. Upon returning from the function and seeing that it returned true, we inverse the xspeed to make it go in the opposite direction, so the next time we move the ball, it is moving towards the other end of the screen.
What I don't understand is that you would see the ball bounce off 9 pixels too early, right? Correct me if I'm wrong, but if this is the case, what would/could be a solution? Maybe set the ball to move the 9 pixels instead of 10? Then afterwards move it normally with the right speed?
Code:
// Check to see if the ball is going to hit a paddle
bool CheckBallCollisions(Entity& paddle)
{
// Temporary values to keep things tidy
int ball_x = g_Ball.screen_location.x;
int ball_y = g_Ball.screen_location.y;
int ball_width = g_Ball.screen_location.w;
int ball_height = g_Ball.screen_location.h;
int ball_speed = g_Ball.y_speed;
int paddle_x = paddle.screen_location.x;
int paddle_y = paddle.screen_location.y;
int paddle_width = paddle.screen_location.w;
int paddle_height = paddle.screen_location.h;
// Get which paddle we're checking against
if ( paddle.screen_location.y == PLAYER_Y)
{
// Check to see if ball is in Y range of the player's paddle.
// We check its speed to see if it's even moving towards the player's paddle.
if ( (ball_speed > 0) && (ball_y + ball_height >= paddle_y) &&
(ball_y + ball_height <= paddle_y + paddle_height) ) // side hit
{
// If ball is in the X range of the paddle, return true.
if ( (ball_x <= paddle_x + paddle_width) && (ball_x + ball_width
>= paddle_x) )
{
return true;
}
}
}
else
{
// Check to see if ball is in Y range of the computer's paddle.
// We check its speed to see if it's even moving towards the computer's paddle.
if ( (ball_speed < 0) && (ball_y >= paddle_y) && (ball_y <= paddle_y +
paddle_height) )
{
// If ball is in the X range of the paddle, return true.
if ( (ball_x <= paddle_x + paddle_width) && (ball_x + ball_width >=
paddle_x) )
{
return true;
}
}
}
return false;
}
Now assume that we move the ball, then check for collisions, and that the ball has a speed of 10 pixels/second towards the right. Also, imagine that the ball is currently 9 pixels away from a paddle (in the x-direction). The next time we move the ball, this function registers a collision, since the ball moves "into" the paddle. Upon returning from the function and seeing that it returned true, we inverse the xspeed to make it go in the opposite direction, so the next time we move the ball, it is moving towards the other end of the screen.
What I don't understand is that you would see the ball bounce off 9 pixels too early, right? Correct me if I'm wrong, but if this is the case, what would/could be a solution? Maybe set the ball to move the 9 pixels instead of 10? Then afterwards move it normally with the right speed?