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.

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);

// If the object is outside the radius (> instead of >= means I don't have to use an Epsilon)
{
// 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;

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 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 on other sites
Ooops. You are right.

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

1. 1
Rutin
24
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 9
• 46
• 41
• 23
• 13
• Forum Statistics

• Total Topics
631749
• Total Posts
3002046
×