Creating and using AABB for collision detection 3d

Suppose I have a square that I have created in the following way:
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);

glColor3f(1.0f,0.5f,0.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);

glColor3f(1.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);

glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);

glColor3f(1.0f,0.0f,1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glEnd();

Now suppose I want to use an AABB structure to detect when a sphere collides with the sides of this cube (not the top nor the bottom of the cube) and thus prevent it from going through the cube? Any ideas on how to go about doing this? I have gone through a lot of websites and none seem to fully help me understand properly

Also would I be able to use the AABB structure if I had implemented the cube using glutSolidCube?

Also is there an easier of detecting collision without having to use AABB? Maybe like testing for intersection or something like that?

Test if the minimum distance between the center of the sphere and the 6 faces of the cube is smaller than the radius of the sphere ?

Note that I have speedly find this at c++ - Cube sphere intersection test? - Stack Overflow using an “intersection cube sphere” request at www.google.fr


Assuming an axis-aligned cube and letting C1 and C2 be opposing  corners, S the center of the sphere, and R the radius of the sphere, and  that both objects are solid:

  
inline float squared(float v) 
{ 
    return v * v; 
} 

bool doesCubeIntersectSphere(vec3 C1, vec3 C2, vec3 S, float R) 
{     
    float dist_squared = R * R;     

    /* assume C1 and C2 are element-wise sorted, if not, do that now */     

    if (S.X < C1.X) dist_squared -= squared(S.X - C1.X);     
    else if (S.X > C2.X) dist_squared -= squared(S.X - C2.X);     

    if (S.Y < C1.Y) dist_squared -= squared(S.Y - C1.Y);     
    else if (S.Y > C2.Y) dist_squared -= squared(S.Y - C2.Y);     

    if (S.Z < C1.Z) dist_squared -= squared(S.Z - C1.Z);     
    else if (S.Z > C2.Z) dist_squared -= squared(S.Z - C2.Z);     

    return dist_squared > 0;
} 

(I haven’t tested if this work or not but this seem in first view a good method)

Please, can you test to be a little more precise, I don’t understand your language :slight_smile:

It was Vietnamese spam for an A/C service company. I just spamblocked the guy. If you see any more such nonsense and want to see what it says, go to http://translate.google.com, select “translate from Vietnamese”, and copy/paste the text. All the recent spam has been Vietnamese lately.

Now as to the question at-hand…

                               zemprof, does your AABB align with the sides of the cube?  And you said you don't care about intersection with the top/bottom of the cube -- just the sides?  If so, sounds to me like you've just reduced the problem to 2D.  Just decide which plane you want to do the test in, and you've got a 2D circle-box test rather than a 3D sphere-cube test.  I don't know how many of these tests you have, but to make them even cheaper, do a cheap 2D circle-circle test (or if you must use 3D a 3D sphere-sphere test) to try and protect against needing to do 2D circle-box (or 3D sphere-cube) tests in most cases.