I have been researching how to apply friction and there is a part I am stuck on, which is how to apply that friction to the velocityy (if in fact I am calculating the friction force correctly that is).
When I have a ball on a surface, with a normal of (0, 1, 0), with a present velocity of (2, 0, 0) then I will calculate my friction force as (0, -0.3, 0). However, what I don't understand is how to properly apply that to my velocity. If I simply, subtract it then my velocity will be (2, -0.3, 0), however, shouldn't the friction be cause the x component to be more less?
Here is my current code, if anyone could please take a look at I would greatly appreciate it. There are some optimizations to be made, I am aware of that.
mTotalForces = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 vSurfaceNormalized;
D3DXVec3Normalize(&vSurfaceNormalized, &vSurfaceNormal);
D3DXVECTOR3 vFrictionForce(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 forceAndVelocity = mTotalForces + m_vVelocity;
float fVelocityMagnitude = sqrt((forceAndVelocity.x * forceAndVelocity.x) + (forceAndVelocity.y * forceAndVelocity.y) + forceAndVelocity.z * (forceAndVelocity.z));
float fFrictionForceMagnitude = 0.0f;
float fFrictionForce = 0.0f;
if(fVelocityMagnitude == 0.0f)
{
fFrictionForce = m_fStaticFrictionCoefficient;
D3DXVECTOR3 vStaticFriction = -m_fStaticFrictionCoefficient * vSurfaceNormalized;
vFrictionForce = vStaticFriction;
}
else if(fVelocityMagnitude > 0.0f)
{
fFrictionForce = m_fKineticFrictionCoefficient;
D3DXVECTOR3 vKineticFriction = -m_fKineticFrictionCoefficient * vSurfaceNormalized;
vFrictionForce = vKineticFriction;
}
{
float fFrictionForceMagnitude = abs(fFrictionForce * D3DXVec3Dot(&vSurfaceNormalized, &forceAndVelocity));
if(fFrictionForceMagnitude > fVelocityMagnitude)
{
fFrictionForceMagnitude = 0.0f;
m_vVelocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
}
else
{
m_vVelocity -= vFrictionForce;
}
}
I make the total force zero at the start of this friction function because if there is friction, then the previous force (gravity) should not influence the velocity later. (... right?)