Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm looking for an algorithm that will determine if a sphere is in any way touching an AABB or contained within it. Profile results indicate that this specific use is taking up a large percentage of my time, so it would be nice if it was as efficient as possible.

Edit: ATM, I've got this:

        bool IntersectsAABBWithSphere(Math::AABB rhs, Physics::Sphere sphere) const {
            // http://www.gamedev.net/topic/335465-is-this-the-simplest-sphere-aabb-collision-test/
            auto aabbcenter = (rhs.BottomLeftClosest + rhs.TopRightFurthest) / 2.0f;
            auto SepAxis = sphere.origin - aabbcenter;
            auto Dist = Math::Length(SepAxis);
            SepAxis = Math::Normalize(SepAxis);
            if( SepAxis.x >= SepAxis.y && SepAxis.x >= SepAxis.z )
                SepAxis /= SepAxis.x;
            else if( SepAxis.y >= SepAxis.x && SepAxis.y >= SepAxis.z )
                SepAxis /= SepAxis.y;
            else
                SepAxis /= SepAxis.z;

            auto extents = (rhs.TopRightFurthest - rhs.BottomLeftClosest);
            SepAxis.x *= extents.x / 2.0f;
            SepAxis.y *= extents.y / 2.0f;
            SepAxis.z *= extents.z / 2.0f;

            return Dist <= (sphere.radius + Math::Length(SepAxis));
        }

My AABB's are currently represented as min/max, and my spheres as origin/magnitude.

share|improve this question
    
Perhaps you could share what you're currently doing? –  Tetrad May 6 '12 at 0:41
    
@Tetrad: Dang, my mistake. –  DeadMG May 6 '12 at 0:48
add comment

1 Answer

Found an SSE implementation in the paper and source code given here. Now that function has gone from 45% of my run-time, to so little I can't even find it in the list. win!

share|improve this answer
    
How are you profiling. I'm curious because this hasn't come up in my thinking yet and I've never felt a need to do it for the simple programs I've written before but I think this would be useful for my game in the future for optimizing it. Is it simply a matter of tracking the amount of time in the given function areas or is there some other third party library or something you use? –  Azaral May 6 '12 at 1:58
    
@Azaral: I use AMD Code Analyst. VS2010 Ultimate has a profiler built in, but for some reason mine doesn't work. It's a relatively simple CPU sampler- the program checks what function the CPU is in, say, 10000 times a second. You leave it running for a minute and you have a statistical sample of what functions the CPU is in most of the time. –  DeadMG May 6 '12 at 4:20
    
Link is dead (500). This is why you copy the code in the answer - license permitting - or at least the name of said paper... sigh. –  Thomas Nov 1 '13 at 7:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.