Search code examples
c++openglcollision-detectionglut

how can I add collision to my object and around the screen in my program?


I am trying to create a car game with along with obstacles. I want to add collision so every time my object hits one of the obstacles, obstacle changes color and we know there has been a collision. I also want there to be a collision around my screen so that my object can not leave the view port?

--void 'draw car' is my movable object

#include "include\freeglut.h"   // OpenGL toolkit - in the local shared folder
#include <cmath>
#include <iostream>

//set up some constants
#define X_CENTRE 0.0      /* centre point of square */
#define Y_CENTRE 0.0
#define LENGTH   5.0      /* lengths of sides of square */


//forward declaration - best in the header
void drawStar(GLfloat radius, GLfloat x, GLfloat y);
void drawSquare(GLfloat length, GLfloat x, GLfloat y);
void drawRect(GLfloat lengthX, GLfloat lengthY, GLfloat x, GLfloat y);
void drawCar(GLfloat length, GLfloat x, GLfloat y);


GLfloat red = 1.0, green = 1.0, blue = 1.0;
GLint   xmove = -12.0, ymove = -12.5, zmove = 0.0;



/* reshape callback function
   executed when window is moved or resized. This function should be used in Tutorial 1 */
void reshape(int width, int height)
{
    GLfloat aspect = (GLfloat)width / (GLfloat)height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);  // To operate on the Projection matrix
    glLoadIdentity();

  //  glOrtho(-10.0, 10.0, -10.0, 10.0, -1.0, 1.0);   //sets the x,y,z plane from -1 to 1
    if (width <= height) //if aspect is less or equal to 1
        glOrtho(-10.0, 10.0, -10.0/aspect, 10.0/aspect, -1.0, 1.0);
    else // aspect is greater than 1
        glOrtho(-10.0 * aspect, 10.0* aspect, -10.0 , 10.0, -1.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
}


/* display callback function
   called whenever contents of window need to be re-displayed */
   //this is the all important drawing method - all drawing code goes in here
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);     /* clear window */
    /*glColor3f(1.0, 1.0, 1.0);        /* white drawing objects */

    glColor3f(0.0, 0.0, 1.0);          /* blue drawing objects */
    /* define object to be drawn as a square polygon */



    glColor3f(0.3, -3.0, 4.0);
    drawSquare(2, -3, 8);

    glColor3f(0.3, -1.0, 2.0);
    drawSquare(2, -3, 8);

    glColor3f(1.0, 0.0, 0.0);
    drawSquare(2, -5, -6);

    glColor3f(0.0, 1.0, 0.0);
    drawSquare(2, 0.0, 0.0);

    glColor3f(2.0, 3.0, 1.1);
    drawSquare(2, 6.0, 0.0);

    glColor3f(0.3, -1.0, 2.0);
    drawSquare(2, -6, 5);

    glColor3f(0.7, -1.0, 2.0);
    drawSquare(2, 5, -5);

    glColor3f(0.0, 0.0, 1.0);
    drawSquare(2, 5, 8);

    glColor3f(0.75, 0.5, 0.25);
    drawCar(1,0,0);



    glFlush();     /* execute drawing commands in buffer */
}

void drawCar(GLfloat length, GLfloat x, GLfloat y) {
    glPushMatrix();
    glTranslatef(xmove, ymove, zmove);
    //glBegin(GL_TRIANGLES);
    ////specify the vertices (points in 3D space) of the shape - note that these are 2D points
    //glVertex2f(X_CENTRE - LENGTH / 3, Y_CENTRE - LENGTH / 3);
    //glVertex2f(X_CENTRE - LENGTH / 3, Y_CENTRE + LENGTH / 3);
    //glVertex2f(X_CENTRE + LENGTH / 3, Y_CENTRE + LENGTH / 3);
    //glVertex2f(X_CENTRE + LENGTH / 3, Y_CENTRE - LENGTH / 3);
    //glEnd();

   glRectf(3, 4, 6, 2);
    glPopMatrix();
    glFlush();
   
}




void keyInput(unsigned char key, int x, int y)
{
    switch (key)
    {
    case 'w':
        ymove++;
        if (ymove >= 10) ymove = 3.0;

        break;
    case 's':
        ymove--;
        //  if (ymove <= -5) ymove = 0.0;
        break;
    case 'd':
        xmove++;
        //  if (xmove >= 5) xmove = 0.0;
        break;
    case 'a':
        xmove--;
        //  if (xmove <= -5) xmove = 0.0;
        break;
    }
        
    glutPostRedisplay();
}









void drawSquare(GLfloat length, GLfloat x, GLfloat y)
{
    //x1,y1 is the top left-hand corner coordinate
    GLfloat x1, y1, x2, y2, x3, y3, x4, y4;

    //glColor3f(0.1, 1.0, 0.2);
    x1 = x - length / 2;
    y1 = y + length / 2;
    x2 = x + length / 2;
    y2 = y + length / 2;
    x3 = x + length / 2;
    y3 = y - length / 2;
    x4 = x - length / 2;
    y4 = y - length / 2;

    glBegin(GL_POLYGON);
    glVertex2f(x1, y1);
    glVertex2f(x2, y2);
    glVertex2f(x3, y3);
    glVertex2f(x4, y4);
    glEnd();

    glFlush();
}

void drawRect(GLfloat lengthX, GLfloat lengthY, GLfloat x, GLfloat y)
{
    //x1,y1 is the top left-hand corner coordinate
    GLfloat x1, y1, x2, y2, x3, y3, x4, y4;

    //This example is for a rectangle 
    x1 = x - lengthX / 2;
    y1 = y + lengthY / 2;
    x2 = x + lengthX / 2;
    y2 = y + lengthY / 2;
    x3 = x + lengthX / 2;
    y3 = y - lengthY / 2;
    x4 = x - lengthX / 2;
    y4 = y - lengthY / 2;

    glBegin(GL_POLYGON);
    glVertex2f(x1, y1);
    glVertex2f(x2, y2);
    glVertex2f(x3, y3);
    glVertex2f(x4, y4);
    glEnd();

    glFlush();
}

//Draws a 5 pointed star using lines
void drawStar(GLfloat radius, GLfloat x, GLfloat y)
{
    //x1,y1 is the top coordinate
    //glColor3f(0.0, 1.0, 1.0);

    GLfloat x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;

    x1 = x;
    y1 = y + radius;
    x2 = x + 0.90 * radius;
    y2 = y + 0.40 * radius;
    x3 = x + 0.65 * radius;
    y3 = y - 0.55 * radius;
    x4 = x - 0.65 * radius;
    y4 = y - 0.55 * radius;
    x5 = x - 0.90 * radius;
    y5 = y + 0.40 * radius;

    glLineWidth(1.0);
    glBegin(GL_LINES);
    glVertex2f(x1, y1);
    glVertex2f(x3, y3);
    glVertex2f(x1, y1);
    glVertex2f(x4, y4);
    glVertex2f(x2, y2);
    glVertex2f(x4, y4);
    glVertex2f(x2, y2);
    glVertex2f(x5, y5);
    glVertex2f(x3, y3);
    glVertex2f(x5, y5);
    glEnd();


    glFlush();
}


/* graphics initialisation */
void init(void)
{
      
    glClearColor(0.0, 0.0, 0.0, 0.0);  //Setting up the background color 
}


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_RGBA);
    /* window width = 400 pixels, height = 400 pixels */
    /* window width = 640 pixels, height = 480 pixels for a 4:3 ascpect ratio */
    /* window width = 1024 pixels, height = 576 pixels for a 16:9 ascpect ratio */
    glutInitWindowSize(750, 750);
    /* window upper left corner at (100, 100) */
    glutInitWindowPosition(100, 100);
    /* creates an OpenGL window with command argument in its title bar */
    glutCreateWindow("Example 1");

    glutKeyboardFunc(keyInput);

    init();

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

Solution

  • Keep track of your window size (750, 750), and your orthographic projection multiplier (10.0), then you should be able to tell when a certain world space coordinate is at the edge of the screen from these two.