Sphere to Plane Collision - Getting opposite direction

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

Recommended Posts

// Structs
struct Sphere
{
Vector3 center;
};
struct Plane
{
Vector3 normal;
float offset;
};

// Collision Check
bool SphereToPlane( Vector3 &PointToSphere,Sphere &sphere,Plane &plane )
{
// Calculates the distance from the sphere center to the plane
float dist = DotProduct(plane.normal,sphere.center) - plane.offset;

// Save out the vector between the sphere and plane
PointToSphere = plane.normal*dist;

// Check to see if the sphere is straddling the plane
if(dist < -sphere.radius || dist > sphere.radius)
return false;

return true;
}
// Reacting to the collision
bool SphereToPlaneReaction( Sphere &sphere,Plane &plane )
{
Vector3 toPoint;

// Check if there was a collision
if( !SphereToPlane(toPoint,sphere,plane) )
return false;

// See how far they penetrated each other
float fLength = toPoint.length();
float penetrationDist = sphere.radius - fLength;

toPoint /= fLength; // Normalize

// Set it's position back so it's not colliding anymore
sphere.center += toPoint * penetrationDist;

return true;
}



Ok when i collide against a plane the direction that it moves the sphere back to is basically the reflection of what it is actually supposed to be.

if the plane is flat on the ground it works perfectly but as soon as it has rotation, the it gets put in the opposite direction that it's supposed to.

I tried reflecting the final position across the normal but that didnt work. any ideas?

EDIT: I fixed it, I was calculating the normal wrong for the plane

Edited by Muzzy A