# [Solved] Penetration depth for sphere/AABB and sphere/OBB intersection correct but...

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

## Recommended Posts

Hello again, apologies for what may seem like a sudden influx of questions but this one had me curious. I have two working collision detection test for a sphere. One is for an AABB:
        private bool TestSphereAABB(CD_AxisAlignedBoundingBox aABB, ref Contact contact)
{
// Find point (p) on AABB closest to Sphere centre
Vector3 p = aABB.ClosestPtPointAABB(Centre);

// Sphere and AABB intersect if the (squared) distance from sphere centre to point (p)
// is less than the (squared) sphere radius
Vector3 v = p - Centre;

float distance_Squared = Vector3.Dot(v, v);

{
if (v != Vector3.Zero)
{
v.Normalize();
}

contact.normal = v;

return true;
}

// No intersection
return false;
}


The other (very similar) is for an OBB:
        private bool TestSphereOBB(CD_OrientedBoundingBox oBB, ref Contact contact)
{
// Find point (p) on OBB closest to Sphere centre
Vector3 p = oBB.ClosestPtPointOBB(Centre);

// Sphere and OBB intersect if the (squared) distance from sphere centre to point (p)
// is less than the (squared) sphere radius
Vector3 v = p - Centre;

// Vector3.Dot(v, v) gives the square distance to point p
float distance_Squared = Vector3.Dot(v, v);

{
if (v != Vector3.Zero)
{
v.Normalize();
}

contact.normal = v;

return true;
}

// No intersection
return false;
}


If I have the usual sliding response collision resolution of: position_Projected += contact.normal * contact.penetration; then everything works fine. However, in both cases the collision normal is in the opposite direction to what it should be. How can I achieve a correct penetration depth with the normal reversed (-v)? Also, am I making things too complicated when trying to work out the penetration depth? Thank you. EDIT: Solved it, I just need to use: Radius - (float)Math.Sqrt(distance_Squared); for all cases. Please excuse my brain. [Edited by - Spa8nky on November 1, 2009 3:51:42 PM]

1. 1
Rutin
73
2. 2
3. 3
4. 4
5. 5

• 21
• 10
• 33
• 20
• 9
• ### Forum Statistics

• Total Topics
633425
• Total Posts
3011810
• ### Who's Online (See full list)

There are no registered users currently online

×