Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculating and applying friction
    text
    copied!<p>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).</p> <p>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?</p> <p>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.</p> <pre><code>mTotalForces = D3DXVECTOR3(0.0f, 0.0f, 0.0f); D3DXVECTOR3 vSurfaceNormalized; D3DXVec3Normalize(&amp;vSurfaceNormalized, &amp;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 &gt; 0.0f) { fFrictionForce = m_fKineticFrictionCoefficient; D3DXVECTOR3 vKineticFriction = -m_fKineticFrictionCoefficient * vSurfaceNormalized; vFrictionForce = vKineticFriction; } { float fFrictionForceMagnitude = abs(fFrictionForce * D3DXVec3Dot(&amp;vSurfaceNormalized, &amp;forceAndVelocity)); if(fFrictionForceMagnitude &gt; fVelocityMagnitude) { fFrictionForceMagnitude = 0.0f; m_vVelocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f); } else { m_vVelocity -= vFrictionForce; } } </code></pre> <p>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?) </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload