I am making a pong-like game for a class.
I am having trouble with the ball's collision detection.

The game is a 1 player version, where the ball and paddle are both contained in a box. The player uses a gun which is built into the paddle, in order to shoot a hole through the back wall so that the ball may escape (earning them a point)
I have also given the paddle 2-D movement, instead of just along the x-axis.

The ball seems to be bouncing off the side walls fine, but when it hits the top wall, it seems to "reset" the ball, and it just reappears near the paddle and starts moving again.
I also notice that when the ball is reflected off a wall toward the paddle, it seems to be attracted to the paddle like a magnet.

I tried to post my code, but it was giving me some error about "you do not have permission to create tags" or something like that.

I am wondering if there is a way to detect collision, based off of getpixel(), but for more than one pixel at a time. For example, since the ball is circular, instead of just testing one pixel in the direction that the ball is going, is it possible to getpixel() for a specific area of pixels instead of just one single pixel at a time? Does my question make sense?

Please help!
Thank you!!

Add a portion of your code in your post -- the part that's giving you trouble -- with CODE Tags (don't ask, they're explained everywhere).

And a complete explanation of the problem code helps.

Show us how you are detecting your collision. And a picture of the
situation would be nice as well. Depending on the situation, you might be
able to get away with 1d collision.

I tried attaching my code to my original post earlier, but it was giving me problems and wouldn't let me.
Now it seems to be working...?

In my game, the ball and paddle are enclosed in a BROWN colored box.
I am using getpixel() to detect when the ball hits the BROWN box.
The ball is WHITE in color.

The paddle is 60 wide and 10 tall.
The ball has a radius of 10.

dir == 1 indicates ball is moving up and to the left
dir == 2 indicates ball is moving down and to the left
dir == 3 indicates ball is moving up and to the right
dir == 4 indicates ball is moving down and to the right

The paddle_x and paddle_y are the paddle's coordinates, and ball_x and ball_y are the ball's.
It should be pretty easy to understand, but I can't figure out where my problem is coming from.

If it helps to know, I am using Dev-C++ and the Allegro library for this game. (I am new to both)

If it would help, I could post my entire code so far. It's only about 280 lines I think.

void moveBall() {
     ball_tempX = ball_x;
     ball_tempY = ball_y;

     if (dir == 1) {
        if ((getpixel(screen, ball_x-11, ball_y-11)==BROWN) || (getpixel(screen, ball_x, ball_y-11)==BROWN)) {
            dir = rand()% 2 + 3;
        }
        else {
             --ball_x;
             --ball_y;
        }    
     }
     else if (dir == 2) {
         if ((getpixel(screen, ball_x-11, ball_y+11)==BROWN) || ((ball_y = paddle_y - 10) && (ball_x >= paddle_x - 10) && (ball_x <= paddle_x + 70))) {
            dir = rand()%2 + 1;
         }
         else {    
              --ball_x;
              ++ball_y;
         }
     }
     else if (dir == 3) {
         if ((getpixel(screen, ball_x+11, ball_y-11)==BROWN) || (getpixel(screen, ball_x, ball_y-11)==BROWN)) {
              dir = rand()% 2 + 3;
         }
         else {    
              ++ball_x;
              --ball_y;
         }
     }
     else if (dir == 4) {
          if ((getpixel(screen, ball_x+11, ball_y+11)==BROWN) || ((ball_y = paddle_y - 10) && (ball_x >= paddle_x - 10) && (ball_x <= paddle_x + 70))) {
               dir = rand()%2 + 1;
          }
          else {    
                   ++ball_x;
                   ++ball_y;
          }
     } 
     else { 
          if (dir == 1 || dir == 3) {
             ++dir;
          }
          else if (dir == 2 || dir == 4) {
             --dir;
          }
     }
         
    acquire_screen();
    circlefill (buffer, ball_tempX, ball_tempY, 10, BLACK);
    circlefill (buffer, ball_x, ball_y, 10, WHITE);
    draw_sprite(screen, buffer, 0, 0);
    release_screen();
    rest(5);
}

Well......... I accidentally erased the entire program I had so far, and had to start over. As you can imagine, I was quite annoyed at first, but going through it for my second time, I did something right where I previously did it wrong. What it is, I'm not sure... but it's now working. :)

I'm sure I'll run into another problem or two...so stay tuned ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.