![]() |
| | #1 |
| Registered User Join Date: Sep 2008
Posts: 47
| Collision detection using gltranslatef? 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 | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| The coordinates of the ball are whatever you used for glTranslate, not (0,0). |
| tabstop is offline | |
| | #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();
}
. Last edited by Jake.c; 10-17-2009 at 04:14 PM. |
| Jake.c is offline | |
| | #4 |
| and the Hat of Guessing 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 |
| tabstop is offline | |
| | #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 | |
| | #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;
}
}
}
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 | |
| | #7 |
| and the Hat of Guessing 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 | |
| | #8 | |
| Registered User Join Date: Sep 2008
Posts: 47
| Quote:
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 | |
| | #9 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
| |
| tabstop is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |