C Board  

Go Back   C Board > General Programming Boards > Game Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-17-2009, 03:50 AM   #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 =).
Jake.c is offline   Reply With Quote
Old 10-17-2009, 01:41 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
The coordinates of the ball are whatever you used for glTranslate, not (0,0).
tabstop is offline   Reply With Quote
Old 10-17-2009, 04:08 PM   #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 04:14 PM.
Jake.c is offline   Reply With Quote
Old 10-17-2009, 04:24 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.)
tabstop is offline   Reply With Quote
Old 10-17-2009, 04:29 PM   #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 07:51 PM.
Jake.c is offline   Reply With Quote
Old 10-18-2009, 05:10 AM   #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 05:16 AM.
Jake.c is offline   Reply With Quote
Old 10-18-2009, 09:39 AM   #7
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Old 10-18-2009, 01:02 PM   #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 01:11 PM.
Jake.c is offline   Reply With Quote
Old 10-18-2009, 01:30 PM   #9
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
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.
tabstop is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Collision Detection Problems Dark_Phoenix Game Programming 1 12-17-2006 03:25 PM
Collision Detection Grantyt3 C++ Programming 3 09-30-2005 03:21 PM
bounding box collision detection DavidP Game Programming 7 07-07-2002 11:43 PM
collision detection DavidP Game Programming 2 05-11-2002 01:31 PM
boolean algebra / memory manipulation / collision detection DavidP Game Programming 4 05-03-2002 09:40 PM


All times are GMT -6. The time now is 12:56 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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