Browse Source

- Added in a check to see if the distance of an object from the controllers position is near-zero. If that's the case then ignore the object as it's effectively reached its goal. This stops a bad NAN calculation.

MelvMay-GG 12 years ago
parent
commit
654a15f
1 changed files with 6 additions and 3 deletions
  1. 6 3
      engine/source/2d/controllers/PointForceController.cc

+ 6 - 3
engine/source/2d/controllers/PointForceController.cc

@@ -154,15 +154,18 @@ void PointForceController::integrate( Scene* pScene, const F32 totalTime, const
         // Calculate the force distance to the controllers current position.
         Vector2 distanceForce = currentPosition - pSceneObject->getPosition();
 
-        // Skip if the position is outside the radius.
-        if ( distanceForce.LengthSquared() > radiusSqr )
+        // Fetch distance squared.
+        const F32 distanceSqr = distanceForce.LengthSquared();
+
+        // Skip if the position is outside the radius or is centered on the controller.
+        if ( distanceSqr > radiusSqr || distanceSqr < FLT_EPSILON )
             continue;
 
         // Non-Linear force?
         if ( mNonLinear )
         {
             // Yes, so use an approximation of the inverse-square law.
-            distanceForce *= (1.0f / distanceForce.LengthSquared()) * forceSqr;
+            distanceForce *= (1.0f / distanceSqr) * forceSqr;
         }
         else
         {