just a test

Dependencies:   mbed

Fork of scoreLight_Advanced by Alvaro Cassinelli

Revision:
32:52273c3291fe
Parent:
31:5f039cbddee8
Child:
33:43e8bc451ef0
--- a/rigidLoop.cpp	Sun Sep 23 10:11:43 2012 +0000
+++ b/rigidLoop.cpp	Mon Oct 29 14:28:47 2012 +0000
@@ -32,7 +32,10 @@
     periodSendingData=10; // in ms
     sendingRecenteringAngle=true;
     sendingAnchorPosition=true;
+    sendingBlobMaxMin=true;
+    // sending only on EVENT (which means the spot is touching something) in case of rigid loop:
     sendingOnlyWhenTouch=true;
+    
 
     // Gravity field:
     gravity.set(0,0);
@@ -101,9 +104,9 @@
             //setColor(0x07);//0x04+0x02>>i);
             setColor(0x04);
 
-            saccadeRadius=50;//+rand()%20;
+            saccadeRadius=60;//+rand()%20;
             // default (initial) shape (the scafold belongs to the base class):
-            bluePrint.buildCircularScafold(saccadeRadius, vector2Dd(0,0), 18); //(float _radius, vector2D _pos,vector2D _vel, int _numScafoldPoints);
+            bluePrint.buildCircularScafold(saccadeRadius, vector2Dd(0,0), 20); //(float _radius, vector2D _pos,vector2D _vel, int _numScafoldPoints);
 
             // Numeric parameters for the simulated mechanical system:
             massCenter=0.001;//+0.000005*(rand()%100);
@@ -129,8 +132,8 @@
             setColor(0x04);
 
             // default (initial) shape (the scafold belongs to the base class):
-            saccadeRadius=23;
-            bluePrint.buildCircularScafold(saccadeRadius, vector2Dd(0,0), 18); //(float _radius, vector2D _pos,vector2D _vel, int _numScafoldPoints);
+            saccadeRadius=14;//23;
+            bluePrint.buildCircularScafold(saccadeRadius, vector2Dd(0,0), 20); //(float _radius, vector2D _pos,vector2D _vel, int _numScafoldPoints);
 
             // Note: We may assume NO MASS for the center of the contour following loop. Adding mass may be interesting though (smooth motion).
             massCenter=0.01;
@@ -180,6 +183,35 @@
 
             break;
 
+       case  SPOT_BOUNCING_FACTOR:
+            // Name of this kind of spot:
+            sprintf(spotName,"rigid_bouncing");
+
+            setColor(0x05);
+
+            saccadeRadius=30;//+rand()%20;
+            // default (initial) shape (the scafold belongs to the base class):
+            bluePrint.buildCircularScafold(saccadeRadius, vector2Dd(0,0), 18); //(float _radius, vector2D _pos,vector2D _vel, int _numScafoldPoints);
+
+            // Numeric parameters for the simulated mechanical system:
+            massCenter=0.0007;//0.0008;//+0.000005*(rand()%100);
+            dampMotionCenterMass=0.0002;//0.00015;//00003;
+            
+            factorBouncingForce=0.004; // this is because we will use a force on the central mass (not used in SPOT_BOUNCING_FACTOR)
+
+            factorAbsorptionShock=0.9; // coef elastic bouncing
+            
+            // Finally, we can create the loop (not much to do in this case, only set the central position, and some other things):
+            createLoopFromScafold();
+
+            // per-blob mirror delay (if things were well adjusted - in particular mirror waiting times, then this could be 0.
+            // But in case of unique blobs, it may be interesting to accelerate display AND correct the delay by software).
+            // Even more interesting: in case of rigid circular blob, this can be coorected using angleCorrectionForceLoop:
+            displaySensingBuffer.setDelayMirrors(3);
+            angleCorrectionForceLoop=-5;// in degrees
+
+            break;
+
         case SPOT_PACMAN:
             // this will define the behaviour of the "ghosts" in pacman. The spot just moves at a constant speed when there is no object.
             // When it collides, we use the position of the pacman (another spot) and the tangent vector to the blocking line to define the new direction of the
@@ -321,16 +353,6 @@
     draw();
 }
 
-
-void rigidLoop::initSizeBlob(int _numPoints)
-{
-    // Iinitialize blob size (number of points for the loop, as well as other structures such as lsdTrajectory)
-    numPoints=_numPoints;
-
-    // Sensing and Display trajectory:
-    displaySensingBuffer.lsdTrajectory.resize(numPoints); // the lsdTrajectory and the elastic loop will have the same number of points (this could be different - decimation?).
-}
-
 void rigidLoop::createLoopFromScafold(void)
 {
 
@@ -346,6 +368,15 @@
 
 }
 
+void rigidLoop::initSizeBlob(int _numPoints)
+{
+    // Iinitialize blob size (number of points for the loop, as well as other structures such as lsdTrajectory)
+    numPoints=_numPoints;
+
+    // Sensing and Display trajectory:
+    displaySensingBuffer.lsdTrajectory.resize(numPoints); // the lsdTrajectory and the elastic loop will have the same number of points (this could be different - decimation?).
+}
+
 void rigidLoop::setRegionMotion(float mmix, float mmiy, float mmax, float mmay)   // wrapper for setWallLimits, because there is no more things to do than set this for a unique mass
 {
     // centerMass.setWallLimits(mmix+10, mmiy+10, mmax-10, mmay-10);
@@ -366,6 +397,13 @@
 
 }
 
+void rigidLoop::sizeFactor(float sizeFactor) {
+    saccadeRadius*=sizeFactor;
+    saccadeRadius_initial*=sizeFactor;
+    // rebuild the blueprint (that's all it's needed in the case of a rigid loop, IF we don't change the number of points in the scafold of course):
+    bluePrint.resizeDimensionScafold(sizeFactor);
+}
+
 
 void rigidLoop::explosion()
 {
@@ -472,7 +510,7 @@
             break;
                  // ================================================================
         case  SPOT_TRACK_DOT: // here, a dot in the center of the saccade should remain in the center:
-            centerMass.pos -=recenteringVectorLoop;
+            centerMass.pos -=recenteringVectorLoop*2.5;
             centerMass.posOld=centerMass.pos; // this is necessary to compute bouceOffWalls using Verlet method... (MAKE A new variable INTEGRATION METHOD?)
             centerMass.bounceOffWalls(); // constrain position (and compute wall "hit")
 
@@ -593,7 +631,7 @@
                 auxVector=centerMass.getSpeed();
                 float aux=unitTowardsLight.dot(auxVector);
                 if (aux<0) {
-                    slidingVector=auxVector-(unitTowardsLight*aux*2); // get only the component perpendicular to unitTowards light
+                    slidingVector=auxVector-(unitTowardsLight*aux*2); 
                    // slidingVector.scale(auxVector.length()); // rescale to the size of the initial speed.
                     centerMass.setSpeed(slidingVector);
                 }
@@ -617,6 +655,58 @@
 
             break;
 
+        case SPOT_BOUNCING_FACTOR:
+          
+          centerMass.resetForce();
+          
+            if (momentNorm>0) {
+                // (a) Compute the new speed (will use slidingVector as auxiliary vector2Df):
+                auxVector=centerMass.getSpeed();
+                float aux=unitTowardsLight.dot(auxVector);
+                if (aux<0) {
+                    slidingVector=auxVector-(unitTowardsLight*aux*2); // symmetric speed with respet to unitTowardsLight
+                    slidingVector.scale(auxVector.length()*factorAbsorptionShock); // rescale to the size of the initial speed.
+                    centerMass.setSpeed(slidingVector);
+                }
+                
+                // This is a hack: let's ADD spring force if the penetration is de facto large:
+               if (recenteringVectorLoop.length()>(saccadeRadius/4)) centerMass.addForce(recenteringVectorLoop*factorBouncingForce);
+                
+                // Also, to avoid "tunneling" through dark zones, let's translate the spot:
+                centerMass.posOld+=recenteringVectorLoop*0.3;
+                centerMass.pos+=recenteringVectorLoop*0.3; 
+            }
+
+            // Gravity? - side or central attraction?
+            centerMass.addForce(gravity*centerMass.mass);
+
+            // or central spring attraction;
+            //vector2Df centerAttraction(CENTER_AD_MIRROR_X, CENTER_AD_MIRROR_X);
+            //vector2Df dist=centerMass.pos-centerAttraction;
+            //centerMass.addForce(-dist*centerMass.mass*0.0007);
+
+            // or "radial gravity":
+            //vector2Df centerAttraction(CENTER_AD_MIRROR_X, CENTER_AD_MIRROR_X);
+            //vector2Df dist=centerMass.pos-centerAttraction;
+            //centerMass.addForce(dist.normalize()*centerMass.mass*0.5);
+
+
+            // update dynamics for the central mass:
+#ifndef VERLET_METHOD
+            centerMass.addDampingForce();  // // only in case of EULER method (damping in VERLET mode is done automatically when updating)
+#endif
+
+            centerMass.update();  // unconstrained
+            centerMass.bounceOffWalls(); // constrain position (and compute wall "hit")
+
+            // Change color with touch? NO
+            // if (displaySensingBuffer.lightTouched)
+            //      transientBlobColor=blobColor|0x02; // set green ON on the trajectory, regardless of the initial color
+            // else
+            transientBlobColor=blobColor; // just the original blob color
+
+
+            break;
 
 
             // ================================================================