Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 1 of 1

Thread: [Question] collision detection between two car

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2014
    Posts
    1

    [Question] collision detection between two car

    i have problem with my project. i want to when the first car (player) hit car2, window will exit. this source:

    Code :
    #include <GL/glut.h>
    #include <stdlib.h>
    #include <math.h>
     
    float speed=0;
    float xRot = 0, yRot = 0;
    float screenLeft = -10;
    float screenRight = 10;
    float screenTop = 20;
    float screenBottom = -15;
     
    int screenWidth = 400; //pixel
    int screenHeight = 650; //pixel
     
    int red=255, green=255, blue=255;
    float posisiX=0, posisiY=-12.5;
     
    void grid()
    {
    float i;
     
    glBegin(GL_LINES);
    glLineWidth(1);
    //Draw vertical row
    for(i=screenLeft;i<=screenRight;i++)
    {
    glVertex2f(i, screenTop);
    glVertex2f(i, screenBottom);
    }
     
    //draw horizontal row
    for(i=screenBottom;i<=screenTop;i++)
    {
    glVertex2f(screenLeft, i);
    glVertex2f(screenRight, i);
    }
     
    glLineWidth(3);
    glColor3f(0.0,0.0,0.0);
    glVertex2f(screenLeft,0);
    glVertex2f(screenRight,0);
    glVertex2f(0, screenBottom);
    glVertex2f(0, screenTop);
     
    glEnd();
    }
     
    void timer(int)
    {
    speed += 1;
    // refresh display
    glutPostRedisplay();
    // set timer with  10 s
    glutTimerFunc(10, timer, 0);
    }
     
     
    void square(){
     
    glBegin(GL_POLYGON);
    glColor3ub(255,255,255);
    glVertex2f(-0.5,1.1);
    glVertex2f(-0.5,0.1);
    glVertex2f(0.5, 0.1);
    glVertex2f(0.5,1.1);
    glEnd();
     
     
    glBegin(GL_POLYGON);
    glColor3ub(0,0,0);
    glVertex2f(-0.4,1);
    glVertex2f(-0.4,0.2);
    glVertex2f(0.4, 0.2);
    glVertex2f(0.4,1);
    glEnd();
     
    }
     
    void square1(){
     
    glBegin(GL_POLYGON);
    glColor3ub(255,255,255);
    glVertex2f(-0.5,1.1);
    glVertex2f(-0.5,0.1);
    glVertex2f(0.5, 0.1);
    glVertex2f(0.5,1.1);
    glEnd();
     
     
    glBegin(GL_POLYGON);
    glColor3ub(255,0,0);
    glVertex2f(-0.4,1);
    glVertex2f(-0.4,0.2);
    glVertex2f(0.4, 0.2);
    glVertex2f(0.4,1);
    glEnd();
     
    }
     
    void car3 (){
    square1();
    glPushMatrix();
    glTranslatef(0,1.1, 0);
    square1();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(0,-1.1, 0);
    square1();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(-1.1,0, 0);
    square1();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(1.1,0, 0);
    square1();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(-1.1,-2.2, 0);
    square1();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(1.1,-2.2, 0);
    square1();
    glPopMatrix();
    }
     
    void car (){
    square();
    glPushMatrix();
    glTranslatef(0,1.1, 0);
    square();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(0,-1.1, 0);
    square();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(-1.1,0, 0);
    square();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(1.1,0, 0);
    square();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(-1.1,-2.2, 0);
    square();
    glPopMatrix();
    glPushMatrix();
    glTranslatef(1.1,-2.2, 0);
    square();
    glPopMatrix();
    }
     
    void car2(float x){
    glPushMatrix();
    if (x>0) {
    glTranslatef(x-8 ,5, 0);
    car(); }
     
    glPopMatrix();
    }
     
    void baris(int n, float n1, int n2, int n3, int n4, int n5){
    glPushMatrix();
    glTranslated(0,n-8.5,0);
    car2(n1*0.000001);
    car2(n2*4);
    car2(n3*8);
    car2(n4*12);
    car2(n5*16);
    glPopMatrix();
    }
     
    void sirkuit(){
    baris(1,0,0,0,0,0);
    baris(6,0,0,0,0,0);
    baris(11,0,0,0,0,0);
    baris(16,0,0,0,0,0);
    baris(21,0,0,0,0,0);
    baris(26,0,0,0,1,0);
    baris(31,0,0,0,0,0);
    baris(36,0,1,1,0,1);
    baris(41,0,0,0,0,0);
    baris(46,0,0,0,0,0);
    baris(51,0,0,1,1,1);
    baris(56,0,1,1,0,0);
    baris(61,0,0,0,0,0);
    baris(66,1,0,1,0,1);
    baris(71,0,0,0,0,0);
    baris(76,0,0,0,0,0);
    baris(81,0,1,1,1,1);
    baris(86,0,0,0,0,0);
    baris(91,0,0,0,0,0);
    baris(96,1,1,0,1,1);
    baris(101,0,0,0,0,0);
    baris(106,0,0,1,0,0);
    baris(111,0,0,0,0,0);
    baris(116,0,1,1,1,1);
    baris(121,0,0,0,0,0);
    baris(126,1,0,0,0,0);
    baris(131,0,0,0,0,0);
    baris(136,0,1,0,1,1);
    baris(141,0,0,0,0,0);
    baris(146,0,0,0,0,0);
    baris(151,1,0,1,0,1);
    baris(156,0,0,0,0,0);
    baris(161,0,0,0,0,0);
    baris(165,1,0,1,1,1);
    baris(166,0,0,0,0,0);
    baris(171,0,0,0,0,0);
    baris(176,1,1,1,0,1);
    baris(181,0,0,0,0,0);
    baris(186,0,0,0,0,0);
    baris(191,0,1,1,1,0);
    baris(196,0,0,0,0,0);
    baris(201,0,0,0,0,0);
    baris(206,0,0,1,1,1);
    baris(211,0,0,0,0,0);
    baris(216,0,0,0,0,0);
    baris(221,1,0,1,1,1);
    baris(226,0,0,0,0,0);
    baris(231,0,0,0,0,0);
    baris(236,0,1,1,0,1);
    baris(241,0,0,0,0,0);
    baris(246,0,0,0,0,0);
    baris(251,1,1,0,1,0);
    baris(256,0,0,0,0,0);
    baris(261,0,0,0,0,0);
    baris(266,1,0,1,0,1);
    baris(271,0,0,0,0,0);
    baris(276,0,0,0,0,0);
    baris(281,0,1,0,1,1);
    baris(286,0,0,0,0,0);
    baris(291,0,0,0,0,0);
    baris(296,1,0,1,1,0);
    baris(301,0,0,0,0,0);
    baris(306,0,0,0,0,0);
    baris(311,0,1,1,0,1);
    baris(316,0,0,0,0,0);
    baris(321,0,0,0,0,0);
    baris(326,0,1,1,1,0);
    baris(331,0,0,0,0,0);
    baris(336,0,0,0,0,0);
    baris(341,1,1,1,0,0);
    baris(346,0,0,0,0,0);
    baris(351,0,0,0,0,0);
    baris(356,1,1,0,1,1);
    baris(361,0,0,0,0,0);
    baris(366,0,0,0,0,0);
    baris(371,0,1,1,1,1);
    baris(376,0,0,0,0,0);
    baris(381,0,0,0,0,0);
    baris(386,1,1,1,0,1);
    baris(391,0,0,0,0,0);
    baris(396,0,0,0,0,0);
    baris(401,1,0,1,1,1);
    baris(406,0,0,0,0,0);
    baris(411,0,0,0,0,0);
    baris(416,1,1,0,1,1);
    baris(421,0,0,0,0,0);
    baris(426,0,0,0,0,0);
    baris(431,0,1,1,1,0);
    baris(436,0,0,0,0,0);
    baris(441,0,0,0,0,0);
    baris(446,1,1,0,1,1);
    baris(451,0,0,0,0,0);
    baris(456,0,0,0,0,0);
    baris(461,1,0,1,1,0);
    baris(466,0,0,0,0,0);
    baris(471,0,0,0,0,0);
    baris(476,1,1,1,0,1);
    baris(481,0,0,0,0,0);
    baris(486,0,0,0,1,0);
    baris(491,0,1,0,0,1);
    baris(496,0,0,0,0,0);
    baris(501,0,0,0,0,0);
    baris(506,0,1,1,0,1);
    baris(511,0,0,0,0,0);
    baris(516,0,0,0,0,0);
    baris(521,0,1,1,1,0);
    baris(526,0,0,0,0,0);
    baris(531,0,0,0,0,0);
    baris(536,1,1,1,0,0);
    baris(541,0,0,0,0,0);
    baris(546,0,0,0,0,0);
    baris(551,1,1,0,1,1);
    baris(556,0,0,0,0,0);
    baris(561,0,0,0,0,0);
    baris(566,0,1,1,1,1);
    baris(571,0,0,0,0,0);
    baris(576,0,0,0,0,0);
    baris(581,1,1,1,0,1);
    baris(586,0,0,0,0,0);
    baris(591,0,0,0,0,0);
    baris(596,1,0,1,1,1);
    baris(601,0,0,0,0,0);
    baris(606,0,0,0,0,0);
    baris(611,1,1,0,1,1);
    baris(616,0,0,0,0,0);
    baris(621,0,0,0,0,0);
    baris(626,0,1,1,1,0);
    baris(631,0,0,0,0,0);
    baris(636,0,0,0,0,0);
    baris(641,1,1,0,1,1);
    baris(646,0,0,0,0,0);
    baris(651,0,0,0,0,0);
    baris(656,1,0,1,1,0);
    baris(661,0,0,0,0,0);
    baris(666,0,0,0,0,0);
    baris(671,1,1,1,0,1);
    baris(676,0,0,0,0,0);
    baris(681,0,0,0,1,0);
    baris(686,0,1,0,0,1);
    baris(691,0,0,0,0,0);
    baris(696,0,0,0,0,0);
    baris(701,1,0,1,1,1);
    baris(706,0,0,0,0,0);
    baris(711,0,0,0,0,0);
    baris(716,1,1,0,1,1);
    baris(721,0,0,0,0,0);
    baris(726,0,0,0,0,0);
    baris(731,0,1,1,1,0);
    baris(736,0,0,0,0,0);
    baris(741,0,0,0,0,0);
    baris(746,1,1,0,1,1);
    baris(751,0,0,0,0,0);
    baris(756,0,0,0,0,0);
    baris(761,1,0,1,1,0);
    baris(766,0,0,0,0,0);
    baris(771,0,0,0,0,0);
    baris(776,1,1,1,0,1);
    baris(781,0,0,0,0,0);
    baris(786,0,0,0,1,0);
    baris(791,0,1,0,0,1);
    baris(796,0,0,0,0,0);
    baris(801,0,0,0,0,0);
    baris(806,0,1,1,0,1);
    baris(811,0,0,0,0,0);
    baris(816,0,0,0,0,0);
    baris(821,0,1,1,1,0);
    baris(826,0,0,0,0,0);
    baris(831,0,0,0,0,0);
    baris(836,1,1,1,0,0);
    baris(841,0,0,0,0,0);
    baris(846,0,0,0,0,0);
    baris(851,1,1,0,1,1);
    baris(856,0,0,0,0,0);
    baris(861,0,0,0,0,0);
    baris(866,0,1,1,1,1);
    baris(871,0,0,0,0,0);
    baris(876,0,0,0,0,0);
    baris(881,1,1,1,0,1);
    baris(886,0,0,0,0,0);
    baris(891,0,0,0,0,0);
    baris(896,1,0,1,1,1);
    baris(901,0,0,0,0,0);
    baris(906,0,0,0,0,0);
    baris(911,1,1,0,1,1);
    baris(916,0,0,0,0,0);
    baris(921,0,0,0,0,0);
    baris(926,0,1,1,1,0);
    baris(931,0,0,0,0,0);
    baris(936,0,0,0,0,0);
    baris(941,1,1,0,1,1);
    baris(946,0,0,0,0,0);
    baris(951,0,0,0,0,0);
    baris(956,1,0,1,1,0);
    baris(961,0,0,0,0,0);
    baris(966,0,0,0,0,0);
    baris(971,1,1,1,0,1);
    baris(976,0,0,0,0,0);
    baris(981,0,0,0,1,0);
    baris(986,0,1,0,0,1);
    baris(991,0,0,0,0,0);
    baris(996,0,0,0,0,0);
    baris(1001,1,1,0,1,1);
    }
     
     
    void processNormalKeys(unsigned char key, int x, int y) {
     
     
    }
     
    void processSpecialKeys(int key, int x, int y) {
    switch(key) {
     
     
    case GLUT_KEY_UP:
    yRot-=0.9;
    break;
    /*
    case GLUT_KEY_DOWN: if (posisiY<=screenBottom) {posisiY=screenBottom+7.5;}
    posisiY-=5;
    */
    break;
    case GLUT_KEY_RIGHT:
    posisiX+=4; if (posisiX>=screenRight) {posisiX=screenRight-2;}
    break;
    case GLUT_KEY_LEFT:
    posisiX-=4; if (posisiX<=screenLeft) {posisiX=screenLeft+2;}
    break;
     
    }
    }
     
     
    void init() {
    glClearColor(0.5, 0.5, 0.5, 1.0);
     
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(screenLeft, screenRight, screenBottom, screenTop);
    glMatrixMode(GL_MODELVIEW);
    }
     
     
    void display()
    {
    glClear( GL_COLOR_BUFFER_BIT);
     
    glColor3ub(25,25,25);
    //grid();
     
    glPushMatrix();
    glTranslated(0,yRot,0);
    sirkuit();
    glPopMatrix();
     
    glColor3ub(red, green, blue);
    glPushMatrix();
    glTranslatef(posisiX, posisiY, 0);
    car3();
    glPopMatrix();
     
    yRot-=0.1;
     
    glutSwapBuffers();
    glFlush();
    }
     
    int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitWindowSize(screenWidth, screenHeight);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutInitWindowPosition(500, 50);
    glutCreateWindow("MisiRace");
     
    glutDisplayFunc(display);
    //glutIdleFunc(display);
    timer(10);
     
    glutKeyboardFunc(processNormalKeys);
    glutSpecialFunc(processSpecialKeys);
    init();
     
    glutMainLoop();
     
    return 0;
    }
    Last edited by rachmatarrmdhn; 12-12-2014 at 11:49 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •