Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static Int64 lastFrameID = 0;
- static int handID = -1;
- static float timer = 1; //Initialize a 1 second timer
- static float TIMER = 1;
- #region Second Attempt
- enum States { None, Fist, Opened } ;
- static States storedState = States.None;
- static void SecondAttempt(Frame frame, GameTime gameTime)
- {
- if (frame.Id == lastFrameID || frame.Hands.Count <= 0) return;
- Hand hand;
- bool knownHand = false;
- float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
- timer -= elapsed;
- //if timer expired
- if (timer < 0)
- {
- //Reset Timer and stored values in order to retrieve new ones
- handID = -1;
- storedState = States.None;
- timer = TIMER;
- }
- // get hand: tests purpose only
- // Perhaps choose a hand based on the user's preference => rightmost or leftmost
- if (handID == -1)
- hand = frame.Hands[0];
- else
- {
- knownHand = true;
- hand = frame.Hand(handID);
- }
- // is hand invalid?
- if (!hand.IsValid)
- {
- // Reset values
- // 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
- timer = -1;
- return;
- }
- else
- {
- handID = hand.Id;
- }
- States currentState = isLeapHandFist(hand) ? States.Fist : States.Opened;
- // (storedState != States.None && storedState != currentState)
- bool detectedChange = (storedState != States.None && storedState != currentState);//((storedState == States.Fist && currentState == States.Opened) || (storedState == States.Opened && currentState == States.Fist));
- if (knownHand && timer < TIMER && detectedChange) // if changed from a state to another in the interval set
- {
- if (currentState == States.Opened)
- Console.WriteLine("Detected opening gesture");
- else
- Console.WriteLine("Detected closing gesture");
- timer = -1;
- }
- else if (!knownHand)
- {
- // Update stored state
- storedState = currentState;
- }
- lastFrameID = frame.Id;
- }
- static bool isLeapHandFist(Hand leapHand)
- {
- if (leapHand.Fingers.Count > 1)
- return false;
- Vector handXBasis = leapHand.PalmNormal.Cross(leapHand.Direction).Normalized;
- Vector handYBasis = -leapHand.PalmNormal;
- Vector handZBasis = -leapHand.Direction;
- Vector handOrigin = leapHand.PalmPosition;
- Leap.Matrix handTransform = new Leap.Matrix(handXBasis, handYBasis, handZBasis, handOrigin);
- handTransform = handTransform.RigidInverse();
- Vector transformedPosition = handTransform.TransformPoint(leapHand.SphereCenter);
- float sphereCenterOffset = transformedPosition.z;
- if (sphereCenterOffset > 0 || (leapHand.SphereRadius < 90 && sphereCenterOffset > -26 && sphereCenterOffset <= -21) ||
- (leapHand.SphereRadius < 75 && sphereCenterOffset > -21 && sphereCenterOffset <= -15) ||
- (leapHand.SphereRadius < 87 && sphereCenterOffset > -15 && sphereCenterOffset <= -10) ||
- (leapHand.SphereRadius < 110 && sphereCenterOffset > -10))
- {
- //Console.WriteLine("Closed Fist");
- return true;
- }
- else
- {
- //Console.WriteLine("Open Hand");
- return false;
- }
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment