title: Determining Resting contact between sphere and plane when using external forces
body: <p>This question has one major question, and one minor question. I believe I am right in either question from my research, but not both.</p>
  
  <p>For my physics loop, the first thing I do is apply a gravitational force to my <code>TotalForce</code> for a rigid body object. I then check for collisions using my <code>TotalForce</code> and my <code>Velocity</code>. My <code>TotalForce</code> is reset to <code>(0, 0, 0)</code> after every physics loop, although I will keep my <code>velocity</code>.</p>
  
  <p>I am familiar with doing a collision check between a moving sphere and a static plane when using only velocity. However, what if I have other forces besides <code>velocity</code>, such as gravity? I put the other forces into <code>TotalForces</code> (right now I only have gravity). To compensate for that, when I determine that the sphere is not currently overlapping the plane, I do</p>
  
  <pre><code>    Vector3 forces = (sphereTotalForces + sphereVelocity);
      Vector3 forcesDT = forces * fElapsedTime;
      float denom = Vec3Dot(&amp;plane-&gt;GetNormal(), &amp;forces);
  </code></pre>
  
  <p>However, this can be problematic for how I thought was suppose to be resting contact. I thought resting contact was computed by</p>
  
  <pre><code>denom * dist == 0.0f
  </code></pre>
  
  <p>Where <code>dist</code> is</p>
  
  <pre><code>float dist = Vec3Dot(&amp;plane-&gt;GetNormal(), &amp;spherePosition) - plane-&gt;d;
  </code></pre>
  
  <p>(For reference, the obvious <code>denom * dist &gt; 0.0f</code> meaning the sphere is moving away from the plane)</p>
  
  <p>However, this can never be true. Even when there appears to be "resting contact". This is due to my <code>forces</code> calculation above always having at least a .y of -9.8 (my gravity). When when moving towards a plane with a normal of (0, 1, 0) will produce a y of <code>denom</code> of -9.8.</p>
  
  <p>My question is</p>
  
  <p>1) Am I calculating resting contact correctly with how I mentioned with my first two code snippets?</p>
  
  <p>If so,</p>
  
  <p>2) How should my "other forces" such as gravity be used? Is my use of <code>TotalForces</code> incorrect?</p>
  
  <p>For reference, my timestep is</p>
  
  <pre><code>  mAcceleration = mTotalForces / mMass;
    mVelocity += mAcceleration * fElapsedTime;
    Vector3 translation = (mVelocity * fElapsedTime);
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>Since it appears that some suggested changes will change my collision code, here is how i detect my collision states</p>
  
  <pre><code>if(fabs(dist) &lt;= sphereRadius)
  { // There already is a collision }
  else
  {
      Vector3 forces = (sphereTotalForces + sphereVelocity);
      float denom = Vec3Dot(&amp;plane-&gt;GetNormal(), &amp;forces);
  
      // Resting contact
      if(dist == 0) { }
      // Sphere is moving away from plane
      else if(denom * dist &gt; 0.0f) { }
      // There will eventually be a collision
      else
      {
          float fIntersectionTime = (sphereRadius - dist) / denom;
          float r;
          if(dist &gt; 0.0f)
              r = sphereRadius;
          else
              r = -sphereRadius;
  
          Vector3 collisionPosition = spherePosition + fIntersectionTime * sphereVelocity - r * planeNormal;
      }
  }
  </code></pre>