title: bounded box collision detection in opengl
body: <p>Here is the overlay of the program. I'm working in visual studio 10 , c++ with opengl/glut. I am trying to create a maze(which i generate from an input file). Then i import two models from  3dstudio max with a obj loader(glm). And one of the models(mo) will be moving around the maze. So i added a bounding box around him and also around the walls of the labyrinth . I also drawn the box around mo and it seems to move/rotate along with the character. But for some reason (maybe i'm not doing it in the right place) collision test never detects anything. I will let the code do the talking , and if you have any questions i'll be glad to straighten them out.Prepare for code now.</p>
  
  <pre><code>//bounding boxes
  struct BoundingBox
  {
      Vector3D max;
      Vector3D min;
  };
  BoundingBox *box_mo;
  BoundingBox *static_box[400];
  
  void initbox(BoundingBox *b){
      b-&gt;min.x=100000;
      b-&gt;min.y=100000;
      b-&gt;min.z=100000;
      b-&gt;max.x=-100000;
      b-&gt;max.y=-100000;
      b-&gt;max.z=-100000;
  }
  
  BoundingBox *CreateCollisionBox(GLMmodel *model,GLMgroup *object){
  /* GLM doesn't store each vertex together with the object that owns it. It doesn't have that notion. In GLM object don't have vertex, they have triangles. And each triangle is actually an index in the triangle list of the object.*/
      BoundingBox *boxx=(BoundingBox*)malloc(sizeof(BoundingBox));
      initbox(boxx);
      for(int i=0;i&lt;object-&gt;numtriangles;i++){
  // for each vertex of the triangle pmodel1-&gt;triangles[object-&gt;triangles[i]]
  // calculate min and max
          for(int j=0;j&lt;3;j++){
              GLuint index=model-&gt;triangles[object-&gt;triangles[i]].vindices[j];
              GLfloat x = model-&gt;vertices[index*3 +0];
              GLfloat y = model-&gt;vertices[index*3 +1];
              GLfloat z = model-&gt;vertices[index*3 +2];
              if(boxx-&gt;min.x&gt;x) boxx-&gt;min.x =x;
              if(boxx-&gt;min.y&gt;y) boxx-&gt;min.y =y;
              if(boxx-&gt;min.z&gt;z) boxx-&gt;min.z =z;
  
              if(boxx-&gt;max.x&lt;x) boxx-&gt;max.x =x;
              if(boxx-&gt;max.y&lt;y) boxx-&gt;max.y =y;
              if(boxx-&gt;max.z&lt;z) boxx-&gt;max.z =z;
          }
      }
      return boxx;
  }
  
  void AddCollisionBox(GLMmodel *model,GLMgroup *object){
      //box[boxindex]=CreateCollisionBox(model,object);
      box_mo=CreateCollisionBox(model,object);
      //boxindex++;
  }
  // A GLMmodel has a chained list of groups, each group representing an object. 
  // Each object has a name (the name you gave it in 3D Studio Max or Gmax).
  // Let's you have 10 walls in your scene a few other objects as well and you want to 
  // create collision boxes just for the walls and you do not want to make a collision box 
  // for one of your objects. You could name all your walls
  // like this: Wall1, Wall2, ..., Wall10. If you wanted to add collision boxes just to them 
  // you could go through all objects in the scene and if their name contains "Wall" add them.
  // with this one: strstr
  // Basicly this function does just that: if you want to add boxes for the walls you would call it like this: DefineCollisionBoxes(model,"Wall"); 
  void DefineCollisionBoxes(GLMmodel *model,char *name){
      GLMgroup *group = model-&gt;groups;
      while(group){
          if(strstr(group-&gt;name,name))
              AddCollisionBox(model,group);
          group=group-&gt;next;
      }
  }
  
  bool Collision(BoundingBox *b,GLfloat x,GLfloat y,GLfloat z){
      return x &lt;= b-&gt;max.x &amp;&amp; x&gt;= b-&gt;min.x &amp;&amp; y&lt;= b-&gt;max.y &amp;&amp; y&gt;= b-&gt;min.y &amp;&amp; z&lt;= b-&gt;max.z &amp;&amp; z &gt;= b-&gt;min.z;
  }
  
  bool CollisionTest(BoundingBox *a,BoundingBox *b){
      /*bool collision=false;
      for(int i=0;i&lt;static_boxes;i++){
          for(float x=static_box[i]-&gt;min.x, y=static_box[i]-&gt;min.y,z=static_box[i]-&gt;min.z ;x&lt;=static_box[i]-&gt;max.x &amp;&amp; y&lt;=static_box[i]-&gt;max.y &amp;&amp; z&lt;=static_box[i]-&gt;max.z;x+=0.1,y+=0.1,z+=0.1){
              if(Collision(a,x,y,z) == true)
                  collision=true;
          }
      }
      return collision;*/
      if(a-&gt;min.x &lt;= b-&gt;max.x &amp;&amp; a-&gt;max.x &gt;= b-&gt;min.x &amp;&amp; a-&gt;min.z &lt;= b-&gt;max.z &amp;&amp; a-&gt;max.z &gt;= b-&gt;min.z &amp;&amp; a-&gt;min.y &lt;= b-&gt;max.y &amp;&amp; a-&gt;max.y &gt;= b-&gt;min.y)
          return true;
      return false;
  }
  
  void drawBox(BoundingBox *b){
      glColor3f(1,1,1);
      glBegin(GL_LINE_LOOP);
      glVertex3f(b-&gt;max.x,b-&gt;max.y,b-&gt;min.z);
      glVertex3f(b-&gt;min.x,b-&gt;max.y,b-&gt;min.z);
      glVertex3f(b-&gt;min.x,b-&gt;min.y,b-&gt;min.z);
      glVertex3f(b-&gt;max.x,b-&gt;min.y,b-&gt;min.z);
      glEnd();
  
      glBegin(GL_LINE_LOOP);
      glVertex3f(b-&gt;max.x,b-&gt;min.y,b-&gt;max.z);
      glVertex3f(b-&gt;max.x,b-&gt;max.y,b-&gt;max.z);
      glVertex3f(b-&gt;min.x,b-&gt;max.y,b-&gt;max.z);
      glVertex3f(b-&gt;min.x,b-&gt;min.y,b-&gt;max.z);
      glEnd();
  
      glBegin(GL_LINE_LOOP);
      glVertex3f(b-&gt;max.x,b-&gt;max.y,b-&gt;min.z);
      glVertex3f(b-&gt;max.x,b-&gt;max.y,b-&gt;max.z);
      glVertex3f(b-&gt;min.x,b-&gt;max.y,b-&gt;max.z);
      glVertex3f(b-&gt;min.x,b-&gt;max.y,b-&gt;min.z);
      glEnd();
  
      glBegin(GL_LINE_LOOP);
      glVertex3f(b-&gt;max.x,b-&gt;min.y,b-&gt;max.z);
      glVertex3f(b-&gt;min.x,b-&gt;min.y,b-&gt;max.z);
      glVertex3f(b-&gt;min.x,b-&gt;min.y,b-&gt;min.z);
      glVertex3f(b-&gt;max.x,b-&gt;min.y,b-&gt;min.z);
      glEnd();
  }
  
  
  //display function
  void display(){
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      //setup view
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      camera.render
      //read from file, create walls and characters 
      //place model mo in front of the third person camera always
      Vector3D positionn;
      positionn = camera.position + camera.forward*5;
  
      glPushMatrix();
      glColor3f(0.88f,0.75f,0.49f);
      //here i move my character and calculate bounding box at each frame for him
      glTranslatef(positionn.x,-0.42f,positionn.z);
      glScalef(0.7f,0.7f,0.7f);
      glRotatef(angle,0,1,0);
      drawMo();
      DefineCollisionBoxes(pmodel1,"body"); // actual function call
      drawBox(box_mo); //draw bounding box
      glPopMatrix();
  
      //test if the character collides with any of the walls - not working
      for(int i=0;i&lt;static_boxes;i++){
          if(CollisionTest(box_mo,static_box[i]) == true){
              printf("collision");
          }
      }
  
  
      //swap buffers
      glutSwapBuffers();
  }
  </code></pre>