Collision Detection Problem

This is a discussion on Collision Detection Problem within the Game Programming forums, part of the General Programming Boards category; I am trying to program a pong game, and I am using openGL and Glut for the graphics. In my ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Collision Detection Problem

    I am trying to program a pong game, and I am using openGL and Glut for the graphics.

    In my main loop I draw the sprites and then test to see if they have collided, if so then I alter the velocites, either way I add to the coords of the ball and then redraw (to move the ball). Now, when I comment out the collide condition the ball moves across the screen fine, except it goes through the paddle. When I uncomment out the collide test, however, the ball just stays in the center, yet I can still move the paddles so the redraw portion is being exectuted properly. It seems as though the code after the test is not being executed. I am also lead to believe there is a problem with my collide function. Everything in the game is rectangluar even the ball (for now) and are initialize in a struct fyi. Here are my functions:

    Code:
    bool Collide(Rect *object1, Rect *object2) {
      
        float left1, left2;
        float right1, right2;
        float top1, top2;
        float bottom1, bottom2;
    
        left1 = object1->x1 + object1->col_x_offset;
        left2 = object2->x1 + object2->col_x_offset;
        right1 =object1->x2 + object1->col_width;
        right2 =object2->x2 + object2->col_width;
        top1 = object1->y1+ object1->col_y_offset;
        top2 = object2->y1 + object1->col_y_offset;
        bottom1 = object2->y1 + object1->col_height;
        bottom2 = object2->y2 + object2->col_height;
    
        if (bottom1 < top2) return true;
        if (top1 > bottom2) return true;
      
        if (right1 < left2) return true;
        if (left1 > right2) return true;
    
        return false;
    
    }
    Code:
    void display(void)
    { 
         
    
            
      glClear(GL_COLOR_BUFFER_BIT);
      glClearColor(0.0,0.0,0.0,1.0);   
         
        glPushMatrix(); 
          glBegin (GL_LINES);
            glVertex2f(-3.0, 1.3);
            glVertex2f(3.0, 1.3);
            glVertex2f(-3.0,-1.3);
            glVertex2f(3.0,-1.3);
          glEnd ();
        glPopMatrix();
        
       glPushMatrix();  
        glBegin(GL_QUADS);
            glVertex3f(Paddle1.x1,Paddle1.y1,0.0);
            glVertex3f(Paddle1.x1,Paddle1.y2,0.0);
            glVertex3f(Paddle1.x2,Paddle1.y2,0.0);
            glVertex3f(Paddle1.x2,Paddle1.y1,0.0);
         glEnd();
        glPopMatrix();
                 
       glPushMatrix();           
        glBegin(GL_QUADS);
            glVertex3f(Paddle2.x1,Paddle2.y1,0.0);
            glVertex3f(Paddle2.x1,Paddle2.y2,0.0);
            glVertex3f(Paddle2.x2,Paddle2.y2,0.0);
            glVertex3f(Paddle2.x2,Paddle2.y1,0.0);
         glEnd();
        glPopMatrix();         
                 
        glPushMatrix(); 
         glBegin(GL_QUADS);
            glVertex3f(ball.x1,ball.y1,0.0);
            glVertex3f(ball.x1,ball.y2,0.0);
            glVertex3f(ball.x2,ball.y2,0.0);
            glVertex3f(ball.x2,ball.y1,0.0);
         glEnd();
        glPopMatrix();
       
       if (Collide(&ball,&Paddle1)){vx=-vx;vy=-vy;}
          
       ball.x1=ball.x1+vx;
       ball.x2=ball.x2+vx;
       ball.y1=ball.y1+vy;
       ball.y2=ball.y2+vy;
         
         glutSwapBuffers();
    
    }
    If anyone could provide me with any ideas that would be a huge help.

    Thanks
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    It sounds like the ball always thinks it's colliding, and it looks that way to with your code. Your conditions don't look right. You only have a collision if all those if's succeed for the given objects.

    I also find it odd that you have x1, x2, y1, and y2 which are the corners of the objects, but still add the width or offset for your checks.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    24
    Code:
        top1 = object1->y1+ object1->col_y_offset;
        top2 = object2->y1 + object1->col_y_offset;
        bottom1 = object2->y1 + object1->col_height;
        bottom2 = object2->y2 + object2->col_height;
    
        if (bottom1 < top2) return true;
    I think you mean

    Code:
    bottom1 = object1->y1 + object1->col_height;
    As is, the if statement is always true, unless the top of object2 is below its bottom (not necessarily a nonsensical statement if the collision box rotates).

    Hope that helps.

    EDIT: Also, use more parentheses to make reading your code easier. For example, in this line

    Code:
    if (Collide(&ball,&Paddle1)){vx=-vx;vy=-vy;}
    It's hard to tell if you mean "assign value of inverted (hope that's right) vx to vx", or if you meant to say "calculate value of vx minus vx, then assign the result to vx" and got the '-' and '=' the wrong way round.
    Last edited by ruthgrin; 06-08-2006 at 01:06 AM. Reason: Looked over the code again

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    24
    After yet another look through, I realized that there was at least one more error where you assign the values for the corners. Perhaps you could try assigning the corner locations inside each box (which reminds me, are you programming this in C or C++?) and update them as the box's location changes.

    Also, in the comparison statements, I don't think the tests will yield the desired results, though it took me a while to realize what skorman00 was saying. Basically, it is quite possible for the top of box 1 to be higher than the bottom of box 2 without a collision taking place (same for all the other individual conditions). In order to make sure the boxes are meeting, you have to make sure that they occupy the same space by narrowing down the area of potential intersection.

    For example, if box 1's top is within the range defined by box 2's top and box 2's bottom, then you know the boxes are in the valid y-axis range for a collision, but not necessarily on the x-axis, so you test again, this time with, say, box 1's right, box 2's left and box 2's right. If this test also returns true, then the boxes are intersecting (try working it out with your hands, possibly some other props). This calc could be worth considering:

    Code:
    if((box1.left <= box2.right) && (box1.left >= box2.left)) {
      // box1.left is within x range.
      if((box1.top >= box2.bottom) && (box1.top <= box2.top)) {
        // Collision has happened, handle it here.
      }
      else if((box1.bottom <= box2.top) && (box1.bottom >= box2.bottom)) {
        // Collision has occurred.
      }
    }
    else if((box1.right >= box2.left) && (box1.right <= box2.right)) {
      // Hopefully you can work out the rest.
    }
    I'm unable to test this with my compiler right now, but hopefully there aren't any errors worse than a missing parentheses or curly brace. If none of this makes sense please show the structure of your collision box, as I may be making incorrect assumptions about its contents.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ray tracer and collision detection
    By hdragon in forum Game Programming
    Replies: 31
    Last Post: 01-19-2006, 11:09 AM
  2. Collision Detection
    By Grantyt3 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2005, 04:21 PM
  3. matrixes for collision detection
    By DavidP in forum Game Programming
    Replies: 10
    Last Post: 11-09-2002, 10:31 PM
  4. Bounding box collision detection?
    By Crossbow in forum Game Programming
    Replies: 5
    Last Post: 07-13-2002, 06:20 PM
  5. bounding box collision detection
    By DavidP in forum Game Programming
    Replies: 7
    Last Post: 07-08-2002, 12:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21