Hey,
- I'm writing a collision library.
- I have gravity constantly applied to all objects.
- Objects colliding with the ground kind of bounce or vibrate do to gravity, and the collision with the ground resetting the position
So should I not apply gravity if the objects are not in the air? or should it not matter if I'm doing the collision properly?
// Here is what my collision is doing, just in case I have something wrong here
// Sphere structure
struct Sphere
{
Vector3 center;
float fRadius;
};
// Plane structure
struct Plane
{
Vector3 normal;
float fOffset;
}
// This function tells whether or not there is a collision
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.fOffset;
PointToSphere = sphere.center - plane.normal*dist;
if(dist < sphere.fRadius)
return true;
return false;
}
// This function reacts to a collision if there is one
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 penetrationDist = sphere.fRadius - toPoint.length();
toPoint.normalize();
sphere.center += toPoint * penetrationDist;
/*** Even if i set the velocity to '0' here, there's still a bit of bouncing/vibration, but not as much ***/
return true;
}
// And then every frame I apply gravity to the velocity, regardless of collision or what not
velocity.y -= 9.8f * timeStep;