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 4 of 4

Thread: Creating and using AABB for collision detection 3d

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2013
    Posts
    2

    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?
    Last edited by zemprof; 04-01-2013 at 12:48 AM.

  2. #2
    Junior Member Regular Contributor
    Join Date
    Jan 2011
    Location
    Paris, France
    Posts
    248
    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 http://stackoverflow.com/questions/4...ersection-test using an "intersection cube sphere" request at www.google.fr
    Code :
    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)
    Last edited by The Little Body; 04-12-2013 at 03:19 PM.
    @+
    Yannoo

  3. #3
    Junior Member Regular Contributor
    Join Date
    Jan 2011
    Location
    Paris, France
    Posts
    248
    Please, can you test to be a little more precise, I don't understand your language
    Last edited by Dark Photon; 04-13-2013 at 04:25 PM.
    @+
    Yannoo

  4. #4
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,126
    Quote Originally Posted by The Little Body View Post
    Please, can you test to be a little more precise, I don't understand your language
    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.
    Last edited by Dark Photon; 04-13-2013 at 04:47 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
  •