just a test

Dependencies:   mbed

Fork of scoreLight_Advanced by Alvaro Cassinelli

Revision:
2:34157ebbf56b
Parent:
1:a4050fee11f7
Child:
3:b44ff6de81bd
--- a/classPointMass.cpp	Sat Mar 31 08:19:31 2012 +0000
+++ b/classPointMass.cpp	Sat Mar 31 12:50:32 2012 +0000
@@ -170,19 +170,33 @@
 
 void pointMass::setSpeed(const vector2D& vel) { // VERY IMPORTANT! in the case of verlet integration, we need to set dt BEFORE setting the initial speed. 
     speed.set(vel); // enough for EULER METHOD
-    pos=speed*dt+posOld; // NECESSARY for VERLET METHOD (we assume posOld known). 
+   
+   // NECESSARY for VERLET METHOD (we assume posOld known):
+   // pos=speed*dt+posOld; // when dampMotion=0 (no damping)
+    // With damping: 
+    // we have: speed=(posNew-posOld)/(2*dt) and posNew=pos*(2.0-dampMotion)-posOld*(1.0-dampMotion), so:
+   // pos=(speed*2*dt+posOld+posOld*(1.0-dampMotion))/(2.0-dampMotion);
+    pos=speed*2*dt/(2.0-dampMotion)+posOld;
+    
     // no need to compute newPos
 }
 
 void pointMass::setSpeed(float vx, float vy) { // VERY IMPORTANT! in the case of verlet integration, we need to set dt BEFORE setting the initial speed.
     speed.set(vx, vy); // enough for EULER METHOD
-    pos=speed*dt+posOld; // NECESSARY for VERLET METHOD (we assume posOld known). 
+    
+   // NECESSARY for VERLET METHOD (we assume posOld known):
+   // pos=speed*dt+posOld; // when dampMotion=0 (no damping)
+    // With damping: 
+    // we have: speed=(posNew-posOld)/(2*dt) and posNew=pos*(2.0-dampMotion)-posOld*(1.0-dampMotion), so:
+   // pos=(speed*2*dt+posOld+posOld*(1.0-dampMotion))/(2.0-dampMotion);
+    pos=speed*2*dt/(2.0-dampMotion)+posOld; 
+    
     // no need to compute newPos
 }
 
 void pointMass::setPos(float px, float py) { // assuming the speed is unchanged (must do some tweaking in case of Verlet integration)
     pos.set(px, py);
-    posOld=pos-speed*dt;
+    posOld=pos-speed*dt;// no damping! attn...
 }
 
 //------------------------------------------------------------
@@ -197,9 +211,23 @@
 #else
 
         // The following equations are for VERLET integration with pseudo-damping:
-        vector2D posNew=(pos*(2.0-dampMotion)-posOld*(1.0-dampMotion)+acc*dt*dt);
-        // Without damping this is just: posNew=2*posOld-1*pos+acc*dt*dt; // i.e., dampMotion=0;
+          //Without damping this is just:
+        //vector2D posNew=posOld*2 - pos + acc*dt*dt; // i.e., dampMotion=0;
+        // With damping: 
+//         vector2D posNew=(pos*(2.0-dampMotion)-posOld*(1.0-dampMotion)+acc*dt*dt); // BAD!!! THIS NOTATION will introduce precision artefacts!!!
+        vector2D posNew=pos+(pos-posOld)*(1-dampMotion)+acc*dt*dt;
         
+        // ATTENTION: because of the precision of the float or double used, it may be that (pos - posNew) is not 0, when it should ( this produces VERY strange motion artefacts).
+        // So, I will test if that difference is smaller than an arbitrary tollerance in this numerical implementation (for each coordinate), and if so, will FORCE posNew to be equal to pos. 
+        // Note: no need to compute the norm of the difference (in fact, we need the abs difference for each component of the difference vector). Fortunately, nothing is needed here, because we can put the 
+        // expression in a better way that automatically makes 0 the contribution of something smaller than the precision (so the following is not really necessary) 
+//        vector2D diff=(pos-posOld)*(1-dampMotion);
+        // Precision correction (separate axis or not): 
+       // if (abs(diff.x)<0.00001) diff.x=diff.x;
+       // if (abs(diff.y)<0.00001) diff.y=diff.y;
+       //   if (posOld.match(pos, 0.001)==true) {posNew=pos; speed.set(0,0);}
+//        vector2D posNew=pos+diff+acc*dt*dt;
+     
         posOld=pos;
         pos=posNew;