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 (distance_Squared <= Radius * Radius)
{
if (v != Vector3.Zero)
{
v.Normalize();
}
contact.normal = v;
contact.penetration = aABB.DistPointAABB(Centre) - Radius;
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 (distance_Squared <= Radius * Radius)
{
if (v != Vector3.Zero)
{
v.Normalize();
}
contact.normal = v;
contact.penetration = (float)Math.Sqrt(oBB.SqDistancePoint(Centre)) - Radius;
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]