Apidcloud

Untitled

Apr 7th, 2014
104
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         static Int64 lastFrameID = 0;
  2.  
  3.         static int handID = -1;
  4.  
  5.         static float timer = 1;         //Initialize a 1 second timer
  6.         static float TIMER = 1;
  7.  
  8.         #region Second Attempt
  9.  
  10.         enum States { None, Fist, Opened } ;
  11.  
  12.         static States storedState = States.None;
  13.  
  14.         static void SecondAttempt(Frame frame, GameTime gameTime)
  15.         {
  16.             if (frame.Id == lastFrameID || frame.Hands.Count <= 0) return;
  17.  
  18.             Hand hand;
  19.             bool knownHand = false;
  20.  
  21.             float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  22.             timer -= elapsed;
  23.  
  24.             //if timer expired
  25.             if (timer < 0)
  26.             {
  27.                 //Reset Timer and stored values in order to retrieve new ones
  28.                 handID = -1;
  29.                 storedState = States.None;
  30.                 timer = TIMER;
  31.             }
  32.  
  33.             // get hand: tests purpose only
  34.             // Perhaps choose a hand based on the user's preference => rightmost or leftmost
  35.             if (handID == -1)
  36.                 hand = frame.Hands[0];
  37.             else
  38.             {
  39.                 knownHand = true;
  40.                 hand = frame.Hand(handID);
  41.             }
  42.  
  43.             // is hand invalid?
  44.             if (!hand.IsValid)
  45.             {
  46.                 // Reset values
  47.                 // if timer is set to less than 0, then handID and storedState will be set to -1 and States.None respectively, in the next iteration
  48.                 timer = -1;
  49.                 return;
  50.             }
  51.             else
  52.             {
  53.                 handID = hand.Id;
  54.             }
  55.  
  56.             States currentState = isLeapHandFist(hand) ? States.Fist : States.Opened;
  57.  
  58.             // (storedState != States.None && storedState != currentState)
  59.             bool detectedChange = (storedState != States.None && storedState != currentState);//((storedState == States.Fist && currentState == States.Opened) || (storedState == States.Opened && currentState == States.Fist));
  60.  
  61.             if (knownHand && timer < TIMER && detectedChange) // if changed from a state to another in the interval set
  62.             {
  63.                 if (currentState == States.Opened)
  64.                     Console.WriteLine("Detected opening gesture");
  65.                 else
  66.                     Console.WriteLine("Detected closing gesture");
  67.  
  68.                 timer = -1;
  69.             }
  70.             else if (!knownHand)
  71.             {
  72.                 // Update stored state
  73.                 storedState = currentState;
  74.             }
  75.  
  76.             lastFrameID = frame.Id;
  77.         }
  78.  
  79.         static bool isLeapHandFist(Hand leapHand)
  80.         {
  81.             if (leapHand.Fingers.Count > 1)
  82.                 return false;
  83.  
  84.             Vector handXBasis = leapHand.PalmNormal.Cross(leapHand.Direction).Normalized;
  85.             Vector handYBasis = -leapHand.PalmNormal;
  86.             Vector handZBasis = -leapHand.Direction;
  87.             Vector handOrigin = leapHand.PalmPosition;
  88.  
  89.             Leap.Matrix handTransform = new Leap.Matrix(handXBasis, handYBasis, handZBasis, handOrigin);
  90.             handTransform = handTransform.RigidInverse();
  91.  
  92.             Vector transformedPosition = handTransform.TransformPoint(leapHand.SphereCenter);
  93.  
  94.             float sphereCenterOffset = transformedPosition.z;
  95.  
  96.             if (sphereCenterOffset > 0 || (leapHand.SphereRadius < 90 && sphereCenterOffset > -26 && sphereCenterOffset <= -21) ||
  97.                 (leapHand.SphereRadius < 75 && sphereCenterOffset > -21 && sphereCenterOffset <= -15) ||
  98.                 (leapHand.SphereRadius < 87 && sphereCenterOffset > -15 && sphereCenterOffset <= -10) ||
  99.                 (leapHand.SphereRadius < 110 && sphereCenterOffset > -10))
  100.             {
  101.                 //Console.WriteLine("Closed Fist");
  102.                 return true;
  103.             }
  104.             else
  105.             {
  106.                 //Console.WriteLine("Open Hand");
  107.                 return false;
  108.             }
  109.         }
  110.  
  111.         #endregion
RAW Paste Data