Sign in to follow this  
Spa8nky

Why does one penetration depth work and the other doesn't in this case?

Recommended Posts

Hello again, Here is an intersection test for a bounding sphere against a tube:
        private bool TestSphereTube(CD_Segment segment, float radius, ref Contact contact)
        {
            // Find the distance between the object and the nearest point on the segment
            float distance_Squared = segment.SquareDistanceToPoint(Centre);
            distance_Squared += Radius;

            // If the object is outside the radius (> instead of >= means I don't have to use an Epsilon)
            if (distance_Squared > (radius * radius))
            {
                // Compute the closest point on the segement to the projected position
                Vector3 point = segment.ClosestPtPointSegment(Centre);

                // Calculate the normal based on the closest point on segment to object's position
                Vector3 normal = point - Centre;
                normal.Normalize();

                contact.normal = normal;
                contact.penetration = (float)Math.Sqrt(distance_Squared) - radius;

                return true;
            }

            return false;
        }

For the collision response I have the following: position_Projected += contact.normal * contact.penetration; What I don't understand is that if I change the penetration to the following (avoiding the use of a square root): contact.penetration = distance_Squared - (radius * radius); then the collision response jerks sporadically! What is going on here? Thank you.

Share this post


Link to post
Share on other sites
Because the difference between 2 numbers is not the same as the difference between the squares of those numbers.

5 - 2 = 3
(5*5) - (2*2) = 25 - 4 = 21

The square root turns the two numbers from a quadratic scale back into a linear scale. You need it. The square root does take lot of time compared to say 1 addition, but just making less tests is by far the better way to optimize. Unless your doing Crytek 3, you probably don't need to remove square roots - just pick a better culling system. Not checking something is infinitely faster than a fast check :)


Note: I'm assuming your trying this because the profiler told you your code was slow? You wouldn't be trying to prematurely optimize would you? :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this