I have been trying to have a sphere to plane eventually lead to the sphere coming to a resting contact, but my sphere will end up always bouncing forever. It will bounce correctly for the first few times, but eventually it will just be in a constant state of a very small bounce. I have tried to exhaust every article I can find but I don't see where I am going wrong here. Does anyone see the issue? I have attempted to put as much information into this post so I apologize if it is a bit long.
I first attempt to see if there is resting contact, collision, no collision or if the sphere and plane will collide
Vector3 forces = (sphereTotalForces + sphereVelocity);
float denom = Vec3Dot(&plane->GetNormal(), &forces);
if(sphereVelocity.GetMagnitude() > 0 && dist <= sphereRadius + .0013f) {
crCollisionResult.enCollisionState = RESTING_CONTACT;
crCollisionResult.vCollisionNormal = plane->GetNormal();
return crCollisionResult;
}
else if(fabs(dist) + 0.001f <= sphere->GetRadius()) {
crCollisionResult.enCollisionState = COLLISION_HAS;
return crCollisionResult;
}
else if(denom * dist > 0.0f) {
crCollisionResult.enCollisionState = COLLISION_NO;
return crCollisionResult;
}
where
sphereRadius = 1
denom = -9.98206139
dist = 2.34482479
Since none of those are true I will then attempt to calculate when the collision will happen
F32 fIntersectionTime = (sphere->GetRadius() - dist) / denom;
F32 r;
if(dist > 0.0f)
r = sphere->GetRadius();
else
r = -sphere->GetRadius();
Vector3 collision = spherePosition + fIntersectionTime * rigidBodySphere->GetVelocity() - r * plane->GetNormal();
crCollisionResult.fCollisionTime = fIntersectionTime;
crCollisionResult.vCollisionNormal = plane->GetNormal();
crCollisionResult.vAdjustedPosition = collision;
The intersection time will be calculated to be 0.00334050134 , which is less than the fElapsedTime of 0.00703109941. As a result, I will update my sphere using the intersection time as the delta (so fElapsedTime here will be 0.00334050134)
mAcceleration = mTotalForces / mMass;
mVelocity += mAcceleration * fElapsedTime;
Vector3 translation = (mVelocity * fElapsedTime);
mTransform->Translate(translation.x, translation.y, translation.z);
mTotalForces = Vector3(0.0f, 0.0f, 0.0f);
where
mTotalForces = (0, -10, 0)
mMass = 22
After I update there sphere with its new position, I will then calculate the new velocity of it being reflected
Vector3 vSurfaceNormalized;
Vec3Normalize(&vSurfaceNormalized, &crCollisionResult.vCollisionNormal);
Vector3 vIncomingVelocity = pSphere->GetVelocity();
F32 fIDotN = Vec3Dot(&vIncomingVelocity, &vSurfaceNormalized);
Vector3 vReflectedVelocity = vIncomingVelocity - ((1.0f+pSphere->GetCoefficientOfRestitution()) * vSurfaceNormalized * fIDotN);
pSphere->SetVelocity(vReflectedVelocity);
where
pSphere->mCoefficientOfRestitution = .85
vIncomingVelocity = (0, -5.30, 0)
mNormal = (0, 1, 0)
fIDotN = -5.30350208
vReflectedVelocity = (0, 4.50797653)
abs
(absolute value) in C probably: stackoverflow.com/a/3118188/796832 \$\endgroup\$