Esempio n. 1
0
            private void TrackWave(Skeleton skeleton, bool isLeft, ref WaveGestureTracker tracker, long timestamp)
            {
                JointType handJointId  = (isLeft) ? JointType.HandLeft : JointType.HandRight;
                JointType elbowJointId = (isLeft) ? JointType.ElbowLeft : JointType.ElbowRight;
                Joint     hand         = skeleton.Joints[handJointId];
                Joint     elbow        = skeleton.Joints[elbowJointId];

                if (hand.TrackingState != JointTrackingState.NotTracked && elbow.TrackingState != JointTrackingState.NotTracked)
                {
                    if (tracker.State == WaveGestureState.InProgress && tracker.Timestamp + WAVE_MOVEMENT_TIMEOUT < timestamp)
                    {
                        tracker.UpdateState(WaveGestureState.Failure, timestamp);
                        System.Diagnostics.Debug.WriteLine("Fail!");
                    }
                    else if (hand.Position.Y > elbow.Position.Y)
                    {
                        //center position (0, 0)
                        if (hand.Position.X <= elbow.Position.X - WAVE_THRESHOLD)
                        {
                            tracker.UpdatePosition(WavePosition.Left, timestamp);
                        }
                        else if (hand.Position.X >= elbow.Position.X + WAVE_THRESHOLD)
                        {
                            tracker.UpdatePosition(WavePosition.Right, timestamp);
                        }
                        else
                        {
                            tracker.UpdatePosition(WavePosition.Neutral, timestamp);
                        }

                        if (tracker.State != WaveGestureState.Success && tracker.IterationCount == REQUIRED_ITERATIONS)
                        {
                            tracker.UpdateState(WaveGestureState.Success, timestamp);
                            System.Diagnostics.Debug.WriteLine("Success!");

                            if (GestureDetected != null)
                            {
                                GestureDetected(this, new EventArgs());
                            }
                        }
                    }
                    else
                    {
                        if (tracker.State == WaveGestureState.InProgress)
                        {
                            tracker.UpdateState(WaveGestureState.Failure, timestamp);
                            System.Diagnostics.Debug.WriteLine("Fail!");
                        }
                        else
                        {
                            tracker.Reset();
                        }
                    }
                }
                else
                {
                    tracker.Reset();
                }
            }
        private void TrackWave(Skeleton skeleton, bool isLeft, ref WaveGestureTracker tracker, long timestamp)
        {
            JointType handJointId = (isLeft) ? JointType.HandLeft : JointType.HandRight;
            JointType elbowJointId = (isLeft) ? JointType.ElbowLeft : JointType.ElbowRight;
            Joint hand = skeleton.Joints[handJointId];
            Joint elbow = skeleton.Joints[elbowJointId];

            if (hand.TrackingState != JointTrackingState.NotTracked && elbow.TrackingState != JointTrackingState.NotTracked)
            {
                if (tracker.State == WaveGestureState.InProgress && tracker.Timestamp + WAVE_MOVEMENT_TIMEOUT < timestamp)
                {
                    tracker.UpdateState(WaveGestureState.Failure, timestamp);
                    System.Diagnostics.Debug.WriteLine("Fail!");
                }
                else if (hand.Position.Y > elbow.Position.Y)
                {
                    //Using the raw values where (0, 0) is the middle of the screen.  From the user's perspective, the X-Axis grows more negative left and more positive right.
                    if (hand.Position.X <= elbow.Position.X - WAVE_THRESHOLD)
                    {
                        tracker.UpdatePosition(WavePosition.Left, timestamp);
                    }
                    else if (hand.Position.X >= elbow.Position.X + WAVE_THRESHOLD)
                    {
                        tracker.UpdatePosition(WavePosition.Right, timestamp);
                    }
                    else
                    {
                        tracker.UpdatePosition(WavePosition.Neutral, timestamp);
                    }

                    if (tracker.State != WaveGestureState.Success && tracker.IterationCount == REQUIRED_ITERATIONS)
                    {
                        tracker.UpdateState(WaveGestureState.Success, timestamp);
                        System.Diagnostics.Debug.WriteLine("Success!");

                        if (GestureDetected != null)
                        {
                            GestureDetected(this, new EventArgs());
                        }
                    }
                }
                else
                {
                    if (tracker.State == WaveGestureState.InProgress)
                    {
                        tracker.UpdateState(WaveGestureState.Failure, timestamp);
                        System.Diagnostics.Debug.WriteLine("Fail!");
                    }
                    else
                    {
                        tracker.Reset();
                    }
                }
            }
            else
            {
                tracker.Reset();
            }
        }
        private void TrackWave(SkeletonWrapper skeleton, int userID, ref WaveGestureTracker tracker, long timestamp)
        {
            // The check for a user wave in this implementation is fairly simple. If the user raises their hand directly above their
            // shoulder then they are merely primed for a wave. If they raise it above and to the left or right of their shoulder,
            // or if they move that hand left or right after having primed it, then we count that as a wave. For such an implementation
            // the WaveGestureState is not genuinley necessary however if we choose to change our approach (eg, require three motions to
            // constitute a valid wave) then the use of this struct will render these changes much simpler.

            // Retrieve the relevent hand and shoulder for the user depending on which hand/shoulder pair that we
            // are checking for a wave.
            Joint leftHand = skeleton.getLeftHandJoint();
            Joint rightHand = skeleton.getRightHandJoint();

            Joint centerOfShoulders = skeleton.getCenterShoulderJoint();

            // Metric measurement
            double distanceBetweenHandsSq = (Math.Pow(leftHand.Position.X - rightHand.Position.X, 2) + Math.Pow(leftHand.Position.Y - rightHand.Position.Y, 2));

            // If the user joints are tracked...
            if (leftHand.TrackingState == JointTrackingState.Tracked && rightHand.TrackingState == JointTrackingState.Tracked)
            {
                // If the user is not taking forever...
                if (tracker.State == WaveGestureState.InProgress && tracker.Timestamp + WAVE_MOVEMENT_TIMEOUT < timestamp)
                {
                    tracker.UpdateState(WaveGestureState.Failure, timestamp);
                }
                // If the user's hands are overlapping...
                else if (distanceBetweenHandsSq <= HAND_DISTANCE_THRESHHOLD_SQ)
                {
                    // If the user is waving to the left...
                    // (We assume that, through the act of overlapping their hands, users will naturally center their hands over their torso.)
                    if (leftHand.Position.X < (centerOfShoulders.Position.X + WAVE_THRESHOLD))
                    {
                        tracker.UpdatePosition(WavePosition.Left, timestamp);
                    }
                    // Check if the user is waving to the right...
                    else if (leftHand.Position.X > (centerOfShoulders.Position.X - WAVE_THRESHOLD))
                    {
                        tracker.UpdatePosition(WavePosition.Right, timestamp);
                    }
                    else
                    {
                        tracker.UpdatePosition(WavePosition.Neutral, timestamp);
                    } // end if-else if-else

                    // If the user has not yet achieved a wave but have satisfied the number of wave iterations...
                    if (tracker.State != WaveGestureState.Success && tracker.IterationCount == REQUIRED_ITERATIONS)
                    {
                        // Then they're waving.
                        tracker.UpdateState(WaveGestureState.Success, timestamp);

                        // If we have observers...
                        if (GestureDetected != null)
                        {
                            // Notify them and let them know which direction the user waved and with which hand they waved.
                            GestureDetected(this, new WaveGestureEventArgs(userID));
                        } // end if

                    } // end if

                }
                else
                {
                    if (tracker.State == WaveGestureState.InProgress)
                    {
                        tracker.UpdateState(WaveGestureState.Failure, timestamp);
                    }
                    else
                    {
                        tracker.Reset();
                    } // end if-else
                } // end if-else if-else
            } // end if
        }