title: Implementing bounding sphere collision in OpenGL
body: <p>I understand the basic principles of bounding sphere collision however implementing it is confusing me a little.</p>
  
  <p>if I have a two cubes defined within arrays: cube1[] and cube2[] with each array consisting of GLfloats that make up each triangle. How then can I first calculate the center point of each cube? and how would I get the radius of the sphere around this?</p>
  
  <p>What mathematics are needed to calculate this?</p>
  
  <p>EDIT: To give more clarification on my question. Assume I have a cube defined using the following array:</p>
  
  <pre><code>GLfloat cube[] = {
       2.0f,  3.0f, -4.0f, // triangle 1, top right
       3.0f,  3.0f, -4.0f,
       2.0f,  2.0f, -4.0f, // bottom right
  
       3.0f,  3.0f, -4.0f, // triangle 2, back face top left
       3.0f,  2.0f, -4.0f, // bottom left
       2.0f,  2.0f, -4.0f,
  
       2.0f,  3.0f, -3.0f, // triangle 1, front face top left
       2.0f,  2.0f, -3.0f, // bottom left
       3.0f,  3.0f, -3.0f, // Bottom right
  
       3.0f,  3.0f, -3.0f, // triangle 2, front face
       2.0f,  2.0f, -3.0f,
       3.0f,  2.0f, -3.0f, // Bottom right
  
       2.0f,  3.0f, -3.0f, // triangle 1, top face
       3.0f,  3.0f, -3.0f,
       2.0f,  3.0f, -4.0f,
       3.0f,  3.0f, -4.0f, // triangle 2, top face
       2.0f,  3.0f, -4.0f,
       3.0f,  3.0f, -3.0f,
  
       2.0f,  2.0f, -3.0f, // triangle 1, bottom face
       2.0f,  2.0f, -4.0f,
       3.0f,  2.0f, -3.0f,
       3.0f,  2.0f, -4.0f, // triangle 2, bottom face
       3.0f,  2.0f, -3.0f, // Bottom Right.
       2.0f,  2.0f, -4.0f,
  
       2.0f,  2.0f, -4.0f, // triangle 1, left face
       2.0f,  2.0f, -3.0f,
       2.0f,  3.0f, -4.0f,
       2.0f,  3.0f, -4.0f, // triangle 2, left face
       2.0f,  2.0f, -3.0f,
       2.0f,  3.0f, -3.0f,
  
       3.0f,  2.0f, -4.0f, // triangle 1, right face
       3.0f,  3.0f, -4.0f,
       3.0f,  2.0f, -3.0f,
       3.0f,  3.0f, -4.0f, // triangle 2, right face
       3.0f,  3.0f, -3.0f,
       3.0f,  2.0f, -3.0f,
  
  };
  </code></pre>
  
  <p>Given this cube, I need to get the center point and keep track of it every time the cube translates. I believe I have done so but assistance on whether this is correct is also appreciated:</p>
  
  <pre><code>// Calculate initial center of the shape
   glm::vec3 corner1 = glm::vec3(2.0f,  3.0f, -4.0f);
   glm::vec3 corner2 = glm::vec3(2.0f,  2.0f, -4.0f);
   glm::vec3 corner3 = glm::vec3(3.0f,  3.0f, -4.0f);
   glm::vec3 corner4 = glm::vec3(3.0f,  2.0f, -4.0f);
   glm::vec3 corner5 = glm::vec3(2.0f,  3.0f, -3.0f);
   glm::vec3 corner6 = glm::vec3(2.0f,  2.0f, -3.0f);
   glm::vec3 corner7 = glm::vec3(3.0f,  3.0f, -3.0f);
   glm::vec3 corner8 = glm::vec3(3.0f,  2.0f, -3.0f);
  
  GLfloat x = (corner1.x + corner2.x + corner3.x + corner4.x + corner5.x + corner6.x+ corner7.x + corner8.x)/8;
  GLfloat y = (corner1.y + corner2.y + corner3.y + corner4.y + corner5.y + corner6.y+ corner7.y + corner8.y)/8;
  GLfloat z = (corner1.z + corner2.z + corner3.z + corner4.z + corner5.z + corner6.z+ corner7.z + corner8.z)/8;
  center = glm::vec4(x, y, z, 1.0f);
  </code></pre>
  
  <p>Translation is kept in check with the following function:</p>
  
  <pre><code>void Cube::Translate(double x, double y, double z)
  {
  // Translation matrix for cube.
  glm::mat4 cubeTransMatrix = glm::mat4();
  cubeTransMatrix = glm::translate(cubeTransMatrix, glm::vec3(x, y, z));
  //center = cubeTransMatrix * center;
  //Move the cube
  for(int i = 0; i &lt; sizeof(cube) / sizeof(GLfloat); i+=3){
          glm::vec4 vector = glm::vec4(cube[i], cube[i+1], cube[i+2], 1.0f);
          glm::vec4 translate = cubeTransMatrix*vector;
          glm::vec4 translateCenter = cubeTransMatrix*center;
          center.x = translateCenter[0];
          center.y = translateCenter[1];
          center.z = translateCenter[2];
          cube[i] = translate[0];
          cube[i+1] = translate[1];
          cube[i+2] = translate[2];
  
      }
  }
  </code></pre>