Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

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

This topic is 3174 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

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
Advertisement
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
Ooops. You are right.

I was trying to optimize too early and forgetting to do the simple math as a result.

Share this post


Link to post
Share on other sites
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!