Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

Collision detection with multiple segments and an octree.

This topic is 3177 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've got a spline which is made up of lots of segments that have been partioned into separate nodes using an octree. The problem I'm facing is how best to deal with the collision detection of any other object in the octree against these segments. I would like to only test against the nearest segment but I am unsure where I should define the nearest. Currently I have the following for collision detection:
        static CollisionFunction[] function = new CollisionFunction[]
        {
            TestAABBAABB,          
            TestAABBOBB,            
            TestAABBPlane,        
            TestAABBRay,         
            TestAABBSegment,      
            TestAABBSphere,     
            TestAABBTriangle,    

            TestOBBOBB,       
            TestOBBPlane,           
            TestOBBRay,             
            TestOBBSegment,         
            TestOBBSphere,          
            TestOBBTriangle,        

            TestPlanePlane,    
            TestPlaneRay,
            TestPlaneSegment,
            TestPlaneSphere,
            TestPlaneTriangle,

            TestRayRay,          
            TestRaySegment,
            TestRaySphere,
            TestRayTriangle,

            TestSegmentSegment,                    
            TestSegmentSphere,
            TestSegmentTriangle,

            TestSphereSphere,    
            TestSphereTriangle,

            TestTriangleTriangle
        };

        delegate bool CollisionFunction(Collidable col_A, Collidable col_B, ref Contact contact);

        public static bool DetectCollision(IOctree_Object a, IOctree_Object b)
        {
            int i_A = (int)a.collision_Primitive.Type;
            int i_B = (int)b.collision_Primitive.Type;

            // All tests are covered in ascending numberical order
            // There is no need to test (a vs b) if we have (b vs a) implemented
            if (i_A > i_B)
            {
                // Swap A and B around 
                // Return bool is needed otherwise the original DetectCollision method will still be performed with the objects the wrong way around
                return DetectCollision(b, a);
            }

            int index = i_A * COLLISION_TYPE_COUNT + i_B - (i_A * (i_A + 1) / 2);            
            //Console.WriteLine("Index {0}", index);

            // Find the correct collision test function based on both objects collidable shapes (bounding volumes, etc)
            CollisionFunction function_Current = function[index];

            // If a correct collision test function doesn't exist, fail silently
            if (function == null)
            {
                return false;
            }

            Contact contact = new Contact(true);

            // Call the current test function for the two collision primitive types
            if (function_Current(a.collision_Primitive, b.collision_Primitive, ref contact))
            {
                // Store the two rigid bodies involved in the collision
                contact.body[0] = a;
                contact.body[1] = b;

                // If the two are intersecting/colliding, add the contact information to the list
                contact_List.Add(contact);

                return true;
            }

            return false;
        }

This generates a list of contacts which I could then iterate through and find the nearest segment before resolving the contact with the closest segment only. The problem is that I will be testing all nearby segments and wondered if it would be faster just to test distances first and then complete the remaining part of the test if the distance is closest? I am also not sure how I could tell the collision resolution method which segment is closest to each object tested in a quick and efficient manner? The current implementation of the collision resolution process is very simple:
        public static void ResolveAllCollisions()
        {
            foreach (Contact contact in Collision_Detection.contact_List)
            {
                IOctree_Object body_0 = contact.body[0];
                IOctree_Object body_1 = contact.body[1];

                if (body_0.isMovable)
                {
                    body_0.collision_Primitive.Centre += contact.normal * contact.penetration;
                }

                if (body_1.isMovable)
                {
                    // This object should move in the opposite direction
                    body_1.collision_Primitive.Centre += -contact.normal * contact.penetration;
                }
            }

            // Clear the contact list now that all the contacts have been resolved
            Collision_Detection.contact_List.Clear();
        }

How would you guys tackle this problem? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!