Collision detection using gltranslatef?

This is a discussion on Collision detection using gltranslatef? within the Game Programming forums, part of the General Programming Boards category; Hi, I'm slowly working towards using my opengl knowledge to create a pong clone. If I've been reading right, I ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    47

    Collision detection using gltranslatef?

    Hi,

    I'm slowly working towards using my opengl knowledge to create a pong clone. If I've been reading right, I should move objects (ball in particular) around using gltranslatef.

    The problems comes when I'm trying to figure out collision detection. If the coordinates for the ball are set at 0,0 and I use gltranslatef to translate around and eventually collide with the edge of the window, I can't work out if any of the vertices meet the edge because (as i've used gltranslatef) the coordinates for the ball still equal 0,0.

    Basically:

    Set ball coordinates at 0,0 -> Translate in the positive y direction -> I don't know how to detect a collision if the ball appears at the top, but the coordinates are still set at 0,0 because I've been using gltranslatef...

    So I haven't been able to find a place that explains how to do collision detection when using gltranslatef (which is how I've been told to move things) instead of just specifying new coordinates every time.

    Thanks in advance =).

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The coordinates of the ball are whatever you used for glTranslate, not (0,0).

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Here's my program. The floats x and y draw the ball at 0,0 as that is their constant values. gltranslatef then translates up 0.1 in the positive y direction every time the animation loop comes around. This results in the ball slowly moving up the window until it dissapears. Printf then prints the values of x and y every time to show that they are always 0.

    My problem is that if my original coordinates are 0,0 then I can't use those to detect collisions because I use gltranslatef for movement instead of just writing new coordinates every time.

    So gltranslatef only tells me that it has move 0.1 in the positive y direction.

    I don't understand how to detect collisions when there is no way (that I know of) of knowing the coordinates of the ball in the window, thus not being able to know if it is colliding with the top or a certain point.

    This is certainly not pong, but it does simulate how I've understood a ball should move when animated.
    My code:

    Code:
    #include <glut.h>
    #include <stdio.h>
    
    float x,y;
    
    void colors(void)
    {
        glClearColor(0.0,0.0,0.0,0.0);
        glColor3f(1.0,1.0,1.0);
        glPointSize(5);
    }
    
    void display(void)
    {
        glClear(GL_COLOR_BUFFER_BIT);
    
        glTranslatef(0.0,0.1,0.0);
        glBegin(GL_POINTS);
        glVertex2f(0.0,0.0);
        glEnd();
    
        glutSwapBuffers();
    }
    
    void update(void)
    {
        glutPostRedisplay();
        printf("%f\n\n%f\n\n", x , y);
        glutTimerFunc(500,update,0);
    }
    
    
    int main(int argc, char **argv)
    {
        x = 0.0;
        y = 0.0;
        glutInit(&argc,argv);
        glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
        glutInitWindowSize(100,100);
        glutCreateWindow("Moving Ball");
        glutDisplayFunc(display);
        colors();
        glutTimerFunc(500,update,0);
        glutMainLoop();
    }
    Thanks for this - this forum is too kind .
    Last edited by Jake.c; 10-17-2009 at 05:14 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So your drawing mechanism is not going to work when you want to draw anything else, so you should probably fix your drawing mechanism.

    There's nothing wrong about separating world coordinates from object coordinates; in fact that's a good thing. BUT: when you have more than one object you will need to do some resetting. So you will have code that looks something like this -- note I'm just giving names and not actual code:
    Code:
    glPushMatrix
    determine current coordinates of ball
    glTranslate(ball coordinates)
    draw ball
    glPopMatrix
    glPushMatrix
    determine current coordinates of paddle
    glTranslate(paddle coordinates)
    draw paddle
    glPopMatrix
    As it stands now, you're moving the world around the ball instead of the other way around. (Also you may need to draw a second paddle, etc.)

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Thanks so much, it's great to finally be able to move forward with my learning =D!
    Last edited by Jake.c; 10-17-2009 at 08:51 PM.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Hmmm... now I'm having a problem with my collision detection itself. My program makes a ball move up and down - when the ball reaches 0.9 or -0.9 on the y axis it reverses and goes the other way. The ball is a GL_POINTS polygon by the way, so it should only have one coordinate.

    Code:
    float move; // Position of the ball
    float direction; // The direction the ball is moving. 0 = Negative y / 1 = Positive y
    
    void collisions(void)
    {
        if(direction == 1) // 1 = Moving Up
        {
            if(move < 0.90) // If it hasn't reached the boundary (0.9) it continues to move
            {
                move = move + 0.01;
            }
    
            else if(move >= 0.90) // If it meets the boundary (0.9) it changes direction and moves down
            {
                direction = 0;
                move = move - 0.01;
            }
        }
    
        else if(direction == 0) // 0 = Moving Down
        {
            if(move > -0.90) // If it hasn't reached the boundary (-0.9) it continues to move
            {
                move = move - 0.01;
            }
    
            else if(move <= -0.90) // If it meets the boundary (-0.9) it changes direction and moves down
            {
                direction = 1;
                move = move + 0.01;
            }
        }
    }
    Every time the animation loop occurs, I print the value of move to the console. My problem is that for some reason, the ball doesn't move in the opposite direction until it reaches 0.91 or -0.91. It's a very minor problem, but I would like to know what's causing it. It should stop at (-)0.9 but continues 0.01 (my increment) ahead of that.

    Any help on the matter would be greatly appreciated =).
    Last edited by Jake.c; 10-18-2009 at 06:16 AM.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Ah, the decimal system. Your belief that the value inside the machine is actually 0.01, though charming, is incorrect. By the time you get to the top of the screen, move is something like 0.89999998, which is less then 0.9 and up you go.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    47
    Quote Originally Posted by tabstop View Post
    Ah, the decimal system. Your belief that the value inside the machine is actually 0.01, though charming, is incorrect. By the time you get to the top of the screen, move is something like 0.89999998, which is less then 0.9 and up you go.
    Oh, that's... odd. I always thought computers would be completely accurate. So I guess the solution would just been to use a slightly lower number if I abosultely need it to be accurate?

    But surely if I'm saying to the computer: if your value that should be but is not 0.9 is also equal to my same value that should be but isn't 0.9. Basically, if the computer believes 0.9 to be slightly lower, then shouldn't my comparison value of 0.9 be seen by the computer as slightly lower and thus be seen as equal in the end?

    Thanks
    Last edited by Jake.c; 10-18-2009 at 02:11 PM.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Jake.c View Post
    Oh, that's... odd. I always thought computers would be completely accurate. So I guess the solution would just been to use a slightly lower number if I abosultely need it to be accurate?

    But surely if I'm saying to the computer: if your value that should be but is not 0.9 is also equal to my same value that should be but isn't 0.9. Basically, if the computer believes 0.9 to be slightly lower, then shouldn't my comparison value of 0.9 be seen by the computer as slightly lower and thus be seen as equal in the end?

    Thanks
    The computer is entirely accurate to the limits of the machine. 0.01 is an unending binary number, and you don't have an unending amount of memory. Look up IEEE-754.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Collision Detection Problems
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 12-17-2006, 03:25 PM
  2. Collision Detection
    By Grantyt3 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2005, 04:21 PM
  3. bounding box collision detection
    By DavidP in forum Game Programming
    Replies: 7
    Last Post: 07-08-2002, 12:43 AM
  4. collision detection
    By DavidP in forum Game Programming
    Replies: 2
    Last Post: 05-11-2002, 02:31 PM
  5. Replies: 4
    Last Post: 05-03-2002, 10:40 PM

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