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