Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code has a number of problems. So, I'm going to step through them, in addition to answering your actual question. First, </p> <pre><code>D3DXVECTOR3 forceAndVelocity = mTotalForces + m_vVelocity; </code></pre> <p>is incorrect as force and velocity are conceptually very different. This is embodied in their different units, so they can not be added. From the next line, </p> <pre><code>float fVelocityMagnitude = sqrt((forceAndVelocity.x * forceAndVelocity.x) + ... </code></pre> <p>I believe you want <code>forceAndVelocity</code> to be strictly velocity. </p> <p>I'd redo the first <code>if</code> statement as follows,</p> <pre><code>float fFrictionCoefficient = 0.0f; if(fVelocityMagnitude == 0.0f) { float fFrictionCoefficient = m_fStaticFrictionCoefficient; } else if(fVelocityMagnitude &gt; 0.0f) { float fFrictionCoefficient = m_fKineticFrictionCoefficient; } </code></pre> <p>I dropped the vector calculations as they were parallel to the normal not perpendicular.</p> <p>The magnitude of the friction is the coefficient of friction times the magnitude of the normal force, so</p> <pre><code>float fFrictionForceMagnitude = fFrictionCoefficient * sqrt(D3DXVec3Dot(&amp;vSurfaceNormal, &amp;vSurfaceNormal)); </code></pre> <p>In the second <code>if</code> statement, you're again comparing force and velocity, and what you're trying to do is determine when static friction may be overcome. Essentially, you need to determine if all other forces exceed the friction, so you need to compare the magnitude of the total forces without friction to the magnitude of the friction force. So, I'd redo the <code>if</code> block as</p> <pre><code>float fForceMagnitude = sqrt(D3DXVec3Dot(&amp;mTotalForces, &amp;mTotalForces)); if(fFrictionForceMagnitude &gt; fForceMagnitude) { float m_vVelocity = D3DXVECTOR3(0.0f, 0.0f, 0.0f); } else { float mTotalForces -= fFrictionForceMagnitude * m_vVelocity / fVelocityMagnitude; } </code></pre> <p>where the last line is the answer to your question. The friction force opposes velocity, so it is opposite in direction to your velocity, where <code>m_vVelocity / fVelocityMagnitude</code> is the normalized velocity. Note, this doesn't directly affect the velocity, instead your velocity is </p> <pre><code>D3DXVECTOR3 m_vVelocity = mTotalForces * time / mass; </code></pre> <p>where <code>mass</code> is the mass of the object;</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