Advertisement
Apidcloud

Untitled

Apr 7th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  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 First Attempt
  9.         static float comparisonValue = 0.6f;
  10.  
  11.         enum CustomGestures { None, Closing, Opening } ;
  12.  
  13.         static float storedSphereRadius = -1;
  14.  
  15.         static void FirstAttempt(Frame frame, GameTime gameTime)
  16.         {
  17.             if (frame.Id == lastFrameID || frame.Hands.Count <= 0) return;
  18.  
  19.             Hand hand;
  20.             bool knownHand = false;
  21.             CustomGestures detectedGesture = CustomGestures.None;
  22.  
  23.             float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
  24.             timer -= elapsed;
  25.  
  26.             //if timer expired
  27.             if (timer < 0)
  28.             {
  29.                 //Reset Timer and stored values in order to retrieve new ones
  30.                 handID = -1;
  31.                 storedSphereRadius = -1;
  32.                 timer = TIMER;
  33.             }
  34.  
  35.             // get hand
  36.             if (handID == -1)
  37.                 hand = frame.Hands[0];
  38.             else
  39.             {
  40.                 knownHand = true;
  41.                 hand = frame.Hand(handID);
  42.             }
  43.  
  44.             // is hand invalid?
  45.             if (!hand.IsValid)
  46.             {
  47.                 handID = -1;
  48.                 storedSphereRadius = -1;
  49.                 timer = -1;
  50.                 return;
  51.             }
  52.             else
  53.             {
  54.                 handID = hand.Id;
  55.             }
  56.  
  57.             // if it's the same hand, then we have a sphereRadius value
  58.             // from which we can do a comparison
  59.             if (knownHand && timer >= 0 && timer < TIMER)
  60.             {
  61.                
  62.                 // if actual hand's sphere radius is around 65% from stored one,
  63.                 // then we can assume the that the hand is closed
  64.                 if (hand.SphereRadius < storedSphereRadius * comparisonValue)
  65.                 {
  66.                     detectedGesture = CustomGestures.Closing;
  67.  
  68.                     Console.WriteLine("Closing");
  69.                     Console.WriteLine(storedSphereRadius.ToString() + "::::" + hand.SphereRadius.ToString());
  70.                    
  71.                 }
  72.                 else if (hand.SphereRadius * comparisonValue > storedSphereRadius)
  73.                 {
  74.                     detectedGesture = CustomGestures.Opening;
  75.  
  76.                     Console.WriteLine("Opening");
  77.                     Console.WriteLine(storedSphereRadius.ToString() + "::::" + hand.SphereRadius.ToString());
  78.                 }
  79.             }
  80.             // in this case, it means we have a new Hand object,
  81.             // hence the need to restore sphereRadius value
  82.             else if (!knownHand)
  83.             {
  84.                 storedSphereRadius = hand.SphereRadius;
  85.             }
  86.            
  87.             // reset since we detected either a closing or an opening gesture
  88.             if (detectedGesture != CustomGestures.None)
  89.             {
  90.                 handID = -1;
  91.                 storedSphereRadius = -1;
  92.                 timer = -1;
  93.             }
  94.  
  95.             lastFrameID = frame.Id;
  96.         }
  97.  
  98.         #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement