Jump to content
  • Advertisement
Sign in to follow this  
ukdeveloper

Pong - ball won't bounce off paddles

This topic is 4895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, my ongoing Pong clone is nearly complete, but there's still one huge problem. I can't figure out how to get the ball to bounce off the paddles. It bounces fine off the walls, but I'm basing my function on Aaron Cox's function for paddle collisions, and can't make it work for my own project. Here's the function I'm using:
bool PaddleHit(GameElement &paddle) // paddle is passed by reference - 2 paddles exist within the game - element_paddle_player and element_paddle_computer
{
     
     int temp_ball_loc_x = element_ball.location_on_screen.x;
     int temp_ball_loc_y = element_ball.location_on_screen.y;
     int temp_ball_width = element_ball.location_on_screen.w;
     int temp_ball_height = element_ball.location_on_screen.h;
     int temp_ball_speed = element_ball.ball_speed_x;
     
     int temp_paddle_loc_x = paddle.location_on_screen.x;
     int temp_paddle_loc_y = paddle.location_on_screen.y;
     int temp_paddle_width = paddle.location_on_screen.w;
     int temp_paddle_height = paddle.location_on_screen.h;
     
     
    if ( temp_paddle_loc_x == (BOUNDARY_LINE_X)+50) // BOUNDARY_LINE_X+50 is the x co-ordinate of the player's paddle
    {
        
         if( ( temp_ball_speed < 0 ) && ( temp_ball_loc_x - temp_ball_width <= temp_paddle_loc_x ) &&
         
         (temp_ball_height + temp_ball_loc_y <= temp_paddle_loc_y + temp_paddle_height) )
         
         {
                           
                           if ( ( temp_ball_loc_y <= temp_paddle_loc_y + temp_paddle_height) && (temp_ball_loc_x + temp_ball_width <= temp_paddle_loc_y ) )
                           {
                                
                                return true;
                           }
         }
    }
    
    
    return false;
         
   
}


And here's where the true/false return ends up:

void BallHandler() // Function to handle ball movement
{
     
     Ball_Move();
     
     if(PaddleHit(element_paddle_player)) // element_paddle_player is passed by reference into the bool function
     {
          finish(); // Quits the game immediately - called if true is returned (just for debugging, this'll be removed and replaced with a proper function to start the next round)
                                   
     }
     
     if(PaddleHit(element_paddle_computer))
     {
     
// Not coded yet - trying to at least get the player's paddle collisions working first.                                
     }

     
}



Please note also that in my Pong clone, the paddles are vertical and at the left and right hand sides of the screen, unlike Aaron's original with the paddles horizontal and at the top and bottom. I think I've tried just about everything, it just plain ain't working like it should. Once, I randomly altered the bool function and it always returned true, but that wasn't right either. Can anybody help me? I'm completely lost and can't follow the logic for this function. I've been at it for days now, still no joy. Please help, ukdeveloper.

Share this post


Link to post
Share on other sites
Advertisement

if ( (ball_speed > 0) && (ball_y + ball_height >= paddle_y) &&
(ball_y + ball_height <= paddle_y + paddle_height) ) // side hit


Just trying to compare your code with Aaron's (above, yours below)
It seems like you didn't change the second line to correspond to the game being oriented differently. Did you try changing it to be width, and x?


if( ( ball_speed < 0 ) && ( ball_loc_x - ball_width <= paddle_loc_x ) &&
(ball_height + ball_loc_y <= paddle_loc_y + paddle_height) )


Maybe try it with:

if( ( temp_ball_speed < 0 ) && ( temp_ball_loc_x - temp_ball_width <= temp_paddle_loc_x ) &&
(temp_ball_width + temp_ball_loc_x <= temp_paddle_loc_x + temp_paddle_width)


The second if statement then checks the correct y positions.

Share this post


Link to post
Share on other sites
You changed the second if statement to this?

if ( ( temp_ball_loc_y <= temp_paddle_loc_y + temp_paddle_height) && (temp_ball_loc_y + temp_ball_height >= temp_paddle_loc_y ) )
{
return true;
}

Also, is the player on the left or the right? (I have to conceptualize this) (nevermind, I am guessing he is on the left)

-edit- For the first if statement, I think the first <= should probably be >=, hmm maybe a + instead of a - as well

if( ( temp_ball_speed < 0 ) && ( temp_ball_loc_x + temp_ball_width >= temp_paddle_loc_x ) &&
(temp_ball_loc_x <= temp_paddle_loc_x + temp_paddle_width)

Share this post


Link to post
Share on other sites
Tried everything you suggested, still doesn't work. The human player is on the left, yes. This is so frustrating, it's just not working at all, the ball just goes straight through the paddle.

Thanks for your help, though.

Share this post


Link to post
Share on other sites
Sorry I couldn't help. I am better at debugging when I can run different possible solutions.

It should work just like Aaron's code, except changing the x's for y's and heights for widths. Keeping the + signs and <=, >= signs the same.

Share this post


Link to post
Share on other sites
Actually, I'll just give you the whole program as it currently stands.

Please have a look, you might be able to fix the problem if you have the whole thing. Clicky.

Hope that helps.

Share this post


Link to post
Share on other sites
I'm having trouble understanding some of your logic.

Firstly, why do you copy all the properties of the paddle into stack variables in PaddleHit? That defeats the purpose of passing by reference.

Secondly, why do you check if the paddle is at x-coordinate BOUNDARY_LINE_X + 50? If this is a way for differentiating between the player and computer paddles, then you aren't checking for ball to cpu paddle collisions.

Your code is hard for me to follow, so I just rewrote the function:


const bool PaddleHit( const GameElement& paddle )
{

if ( ( element_ball.location_on_screen.y <= paddle.location_on_screen.y + paddle.location_on_screen.h ) &&
( element_ball.location_on_screen.y + element_ball.location_on_screen.h >= paddle.location_on_screen.y ) ) {

if ( element_ball.ball_speed_x < 0 ) {

if ( element_ball.location_on_screen.x <= paddle.location_on_screen.x )
return true;
}
else if ( element_ball.ball_speed_x > 0 ) {

if ( element_ball.location_on_screen.x >= paddle.location_on_screen.x )
return true;
}
}

return false;
}







I hope this helps, though it might not as I can't understand completely the original logic of your code.

Edit: modified code slightly.

Share this post


Link to post
Share on other sites
Quote:
Original post by nilkn
Secondly, why do you check if the paddle is at x-coordinate BOUNDARY_LINE_X + 50?


That's to see what paddle the ball hit, player or CPU.

Sorry to waste everyone's time, this isn't working at all.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!