Dear fellow programmer,
i started OpenGL a couple of month ago. And I got really stuck on detecting if one object overlaps another. I’ve looked through some books and couldn’t find anything of help. Could someone please help me with this one.
Thankful beforehand.
I include the code of my programme. The idea is the Ihave to detect when the blue square overlaps with the white box -> if it does then the programme should exit.
(i’m using VC++ 6 on Windows and Mesa 3.0 on Unix)
Here goes the programme:
/*
*
*/
#include <GL/glut.h>
#define CLOSED_X_CENTRE 20.0 /* centre point of CLOSED BOX /
#define CLOSED_Y_CENTRE 30.0
#define OP_LENGTH 10.0 / lengths of sides of CLOSED BOX */
#define OPEN_X_CENTRE 80.0 /* centre point of OPEN BOX /
#define OPEN_Y_CENTRE 30.0
#define CL_LENGTH 5.0 / lengths of sides of OPEN BOX */
GLfloat angle = 45.0; /* angle of rotation (in degrees) /
GLfloat move_ver = 0.0; / units to be translated vertically*/
GLfloat move_hor = 0.0; /* units to be translated horizontally*/
GLfloat scale_x = 1.0;
GLfloat scale_y = 1.0;
/* reshape callback function
executed when window is moved or resized /
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
/ use orthographic (parallel) projection
use xmin = -1, xmax = 1
ymin = -1, ymax = 1
znear = -1, zfar = 1 - not relevant here (2D) */
glOrtho(0.0, 100.0, 0.0, 100.0, -1.0, 1.0);
glMatrixMode( GL_MODELVIEW );
}
/* display callback function
called whenever contents of window need to be re-displayed /
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT); / clear window /
glColor3f(0.0, 0.0, 1.0); / blue objects */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(CLOSED_X_CENTRE + move_hor, CLOSED_Y_CENTRE + move_ver, 0.0);
glScalef( scale_x, scale_y, 1.0);
glTranslatef(-CLOSED_X_CENTRE - move_hor, -CLOSED_Y_CENTRE - move_ver, 0.0);
glTranslatef(CLOSED_X_CENTRE + move_hor, CLOSED_Y_CENTRE + move_ver, 0.0);
glRotatef( angle, 0.0, 0.0, 1.0 ); /* modelling transformation */
glTranslatef(-CLOSED_X_CENTRE, -CLOSED_Y_CENTRE, 0.0);
glBegin(GL_POLYGON);
glVertex2f( CLOSED_X_CENTRE - OP_LENGTH, CLOSED_Y_CENTRE - OP_LENGTH );
glVertex2f( CLOSED_X_CENTRE - OP_LENGTH, CLOSED_Y_CENTRE + OP_LENGTH );
glVertex2f( CLOSED_X_CENTRE + OP_LENGTH, CLOSED_Y_CENTRE + OP_LENGTH );
glVertex2f( CLOSED_X_CENTRE + OP_LENGTH, CLOSED_Y_CENTRE - OP_LENGTH );
glEnd();
glLoadIdentity();
glColor3f(1.0, 1.0, 1.0); /* white object*/
glBegin(GL_LINE_STRIP);
glVertex2f( OPEN_X_CENTRE - CL_LENGTH, OPEN_Y_CENTRE + CL_LENGTH );
glVertex2f( OPEN_X_CENTRE - CL_LENGTH, OPEN_Y_CENTRE - CL_LENGTH );
glVertex2f( OPEN_X_CENTRE + CL_LENGTH, OPEN_Y_CENTRE - CL_LENGTH );
glVertex2f( OPEN_X_CENTRE + CL_LENGTH, OPEN_Y_CENTRE + CL_LENGTH );
glEnd();
glFlush(); /* execute drawing commands in buffer */
}
/* graphics initialisation /
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0); / window will be cleared to black */
}
/* mouse callback function
called when mouse button clicked */
void mouse(int button, int state, int x, int y)
{
switch ( button )
{
case GLUT_LEFT_BUTTON: if (state == GLUT_DOWN )
{
angle = 45.0;
move_ver = 0.0;
move_hor = 0.0;
scale_x = 1.0;
scale_y = 1.0;
glutPostRedisplay();
}
break;
case GLUT_RIGHT_BUTTON: if (state == GLUT_DOWN )
{
exit(0);
}
break;
default: break;
}
}
void keyboard (unsigned char key, int x, int y)
{
switch ( key )
{
case ‘t’:
case ‘T’:
angle += 5.0;
glutPostRedisplay();
break;
case ‘u’:
case ‘U’:
case ‘8’:
case GLUT_KEY_UP:
move_ver += 2.0;
glutPostRedisplay();
break;
case ‘d’:
case ‘D’:
case ‘2’:
move_ver -= 2.0;
glutPostRedisplay();
break;
case ‘l’:
case ‘L’:
case ‘4’:
move_hor -= 2.0;
glutPostRedisplay();
break;
case ‘r’:
case ‘R’:
case ‘6’:
move_hor += 2.0;
glutPostRedisplay();
break;
case ‘b’:
case ‘B’:
case ‘+’:
scale_x += 0.1;
scale_y += 0.1;
glutPostRedisplay();
break;
case ‘s’:
case ‘S’:
case ‘-’:
scale_x -= 0.1;
scale_y -= 0.1;
glutPostRedisplay();
break;
}
}
int main(int argc, char** argv)
{
/* window management code … /
/ initialises GLUT and processes any command line arguments /
glutInit(&argc, argv);
/ use single-buffered window and RGBA colour model /
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
/ window width = 400 pixels, height = 400 pixels /
glutInitWindowSize (400, 400);
/ window upper left corner at (100, 100) /
glutInitWindowPosition (100, 100);
/ creates an OpenGL window with command argument in its title bar */
glutCreateWindow (argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
/* register mouse callback function */
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
Thanks again!