# Thread: Collision detection using gltranslatef?

1. ## 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.

2. The coordinates of the ball are whatever you used for glTranslate, not (0,0).

3. 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 .

4. 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
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. Thanks so much, it's great to finally be able to move forward with my learning =D!

6. 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 =).

7. 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. Originally Posted by tabstop
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

9. Originally Posted by Jake.c
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.