Advertisement

Objects are bouncing do to gravity/collision

Started by June 21, 2013 06:34 AM
3 comments, last by Muzzy A 11 years, 8 months ago

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;

Don't apply gravity if your object has collided. Instead you could raise a "resting" flag, which will save you from many checks.

This flag could be lowered when the surroundings change, when other objects enter the region etc.

(Or you could lower a surroundings_changed flag)

Advertisement

but that brings in other problems like, if the plane is at a slant and the sphere should roll down the hill, what should it do then?

Apply gravity force before collision detection and response. Collision should then nullify any penetrating forces before its converted into velocity and change in position.

o3o

that's what I assumed as well and that's what I'm doing, but i guess my collision reaction or the collision check for the SphereToPlane is wrong.

If so I'm guessing it's the vector I'm getting from the sphere's center to the plane is wrong.

EDIT:

Yup the problem was my collision check


// 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;
 
// This check is wrong, it's supposed to be " if( dist < -sphere.fRadius || dist > sphere.fRadius )  return false "
// to make sure it's not fully in front or fully behind the plane
    if(dist < sphere.fRadius)
        return true;

This topic is closed to new replies.

Advertisement