I am trying to get a sphere and a plane to collide properly. With the below code, they do collide, but only for a little while. I am at a loss when it comes to planes, so there must be something I have overlooked.
What I am trying to discern with the collision (which takes place during the narrowphase) is the depth, the normal and the point of contact. These are used later when resolving impulse etc.
The problem is that distance becomes < 0 after only a few iterations (the sphere is rolling, and then just goes under the plane).
auto distance = glm::dot(sphere->GetPosition() - glm::vec3(planeCollider->GetDistance()), planeCollider->GetNormal());
auto normal = planeCollider->GetNormal() * distance;
if (distance < 0.0f)
{
return false;
}
if (distance == 0.0f)
{
penetrationDepth = sphereCollider->GetRadius();
contactNormal = glm::vec3(0.0f, 1.0f, 0.0f);
contactPoint = bodyA->GetPosition();
}
else
{
penetrationDepth = sphereCollider->GetRadius() - distance;
contactNormal = normal;
contactPoint = normal * (distance - sphereCollider->GetRadius());
}