KinectGestures is utility class that processes programmatic Kinect gestures
        /// <summary>
        /// Invoked if a gesture is cancelled.
        /// </summary>
        /// <returns>true</returns>
        /// <c>false</c>
        /// <param name="userId">User ID</param>
        /// <param name="userIndex">User index</param>
        /// <param name="gesture">Gesture type</param>
        /// <param name="joint">Joint type</param>
        public bool GestureCancelled(long userId, int userIndex, KinectGestures.Gestures gesture,
                                      KinectInterop.JointType joint)
        {
            // the gestures are allowed for the primary user only
            KinectManager manager = KinectManager.Instance;
            if (!manager || (userId != manager.GetPrimaryUserID()))
                return false;

            if (progressDisplayed)
            {
                progressDisplayed = false;

                /*if (gestureInfo != null)
                {
                    gestureInfo.GetComponent<GUIText>().text = String.Empty;
                }*/
            }

            return true;
        }
示例#2
0
        // check if the calibration pose is complete for given user
        private bool CheckForCalibrationPose(Int64 UserId, int bodyIndex, KinectGestures.Gestures calibrationGesture)
        {
            if (calibrationGesture == KinectGestures.Gestures.None)
                return true;

            KinectGestures.GestureData gestureData = playerCalibrationData.ContainsKey(UserId) ?
                playerCalibrationData[UserId] : new KinectGestures.GestureData();

            // init gesture data if needed
            if (gestureData.userId != UserId)
            {
                gestureData.userId = UserId;
                gestureData.gesture = calibrationGesture;
                gestureData.state = 0;
                gestureData.timestamp = Time.realtimeSinceStartup;
                gestureData.joint = 0;
                gestureData.progress = 0f;
                gestureData.complete = false;
                gestureData.cancelled = false;
            }

            // get joint positions and tracking
            int iAllJointsCount = sensorData.jointCount;
            bool[] playerJointsTracked = new bool[iAllJointsCount];
            Vector3[] playerJointsPos = new Vector3[iAllJointsCount];


            int[] aiNeededJointIndexes = KinectGestures.GetNeededJointIndexes(instance);
            int iNeededJointsCount = aiNeededJointIndexes.Length;

            for (int i = 0; i < iNeededJointsCount; i++)
            {
                int joint = aiNeededJointIndexes[i];

                if (joint >= 0)
                {
                    KinectInterop.JointData jointData = bodyFrame.bodyData[bodyIndex].joint[joint];

                    playerJointsTracked[joint] = jointData.trackingState != KinectInterop.TrackingState.NotTracked;
                    playerJointsPos[joint] = jointData.kinectPos;
                }
            }

            // estimate the gesture progess
            KinectGestures.CheckForGesture(UserId, ref gestureData, Time.realtimeSinceStartup,
                ref playerJointsPos, ref playerJointsTracked);
            playerCalibrationData[UserId] = gestureData;

            // check if gesture is complete
            if (gestureData.complete)
            {
                gestureData.userId = 0;
                playerCalibrationData[UserId] = gestureData;

                return true;
            }

            return false;
        }
示例#3
0
        // return the index of gesture in the list, or -1 if not found
        private int GetGestureIndex(KinectGestures.Gestures gesture, ref List<KinectGestures.GestureData> gesturesData)
        {
            int listSize = gesturesData.Count;

            for (int i = 0; i < listSize; i++)
            {
                if (gesturesData[i].gesture == gesture)
                    return i;
            }

            return -1;
        }
示例#4
0
        private bool IsConflictingGestureInProgress(KinectGestures.GestureData gestureData, ref List<KinectGestures.GestureData> gesturesData)
        {
            foreach (KinectGestures.Gestures gesture in gestureData.checkForGestures)
            {
                int index = GetGestureIndex(gesture, ref gesturesData);

                if (index >= 0)
                {
                    if (gesturesData[index].progress > 0f)
                        return true;
                }
            }

            return false;
        }
示例#5
0
        /// <summary>
        /// Gets the normalized screen position of the given gesture for the specified user.
        /// </summary>
        /// <returns>The normalized screen position.</returns>
        /// <param name="UserId">User ID</param>
        /// <param name="gesture">Gesture type</param>
        public Vector3 GetGestureScreenPos(Int64 UserId, KinectGestures.Gestures gesture)
        {
            List<KinectGestures.GestureData> gesturesData = playerGesturesData.ContainsKey(UserId) ? playerGesturesData[UserId] : null;
            int index = gesturesData != null ? GetGestureIndex(gesture, ref gesturesData) : -1;

            if (index >= 0)
            {
                KinectGestures.GestureData gestureData = gesturesData[index];
                return gestureData.screenPos;
            }

            return Vector3.zero;
        }
示例#6
0
        /// <summary>
        /// Gets the progress (in range [0, 1]) of the given gesture for the specified user.
        /// </summary>
        /// <returns>The gesture progress.</returns>
        /// <param name="UserId">User ID</param>
        /// <param name="gesture">Gesture type</param>
        public float GetGestureProgress(Int64 UserId, KinectGestures.Gestures gesture)
        {
            List<KinectGestures.GestureData> gesturesData = playerGesturesData.ContainsKey(UserId) ? playerGesturesData[UserId] : null;
            int index = gesturesData != null ? GetGestureIndex(gesture, ref gesturesData) : -1;

            if (index >= 0)
            {
                KinectGestures.GestureData gestureData = gesturesData[index];
                return gestureData.progress;
            }

            return 0f;
        }
示例#7
0
        /// <summary>
        /// Determines whether the given gesture for the specified user is canceled.
        /// </summary>
        /// <returns><c>true</c> if the gesture is canceled; otherwise, <c>false</c>.</returns>
        /// <param name="UserId">User ID</param>
        /// <param name="gesture">Gesture type</param>
        public bool IsGestureCancelled(Int64 UserId, KinectGestures.Gestures gesture)
        {
            List<KinectGestures.GestureData> gesturesData = playerGesturesData.ContainsKey(UserId) ? playerGesturesData[UserId] : null;
            int index = gesturesData != null ? GetGestureIndex(gesture, ref gesturesData) : -1;

            if (index >= 0)
            {
                KinectGestures.GestureData gestureData = gesturesData[index];
                return gestureData.cancelled;
            }

            return false;
        }
示例#8
0
        /// <summary>
        /// Determines whether the given gesture for the specified user is complete.
        /// </summary>
        /// <returns><c>true</c> if the gesture is complete; otherwise, <c>false</c>.</returns>
        /// <param name="UserId">User ID</param>
        /// <param name="gesture">Gesture type</param>
        /// <param name="bResetOnComplete">If set to <c>true</c>, resets the gesture state.</param>
        public bool IsGestureComplete(Int64 UserId, KinectGestures.Gestures gesture, bool bResetOnComplete)
        {
            List<KinectGestures.GestureData> gesturesData = playerGesturesData.ContainsKey(UserId) ? playerGesturesData[UserId] : null;
            int index = gesturesData != null ? GetGestureIndex(gesture, ref gesturesData) : -1;

            if (index >= 0)
            {
                KinectGestures.GestureData gestureData = gesturesData[index];

                if (bResetOnComplete && gestureData.complete)
                {
                    ResetPlayerGestures(UserId);
                    return true;
                }

                return gestureData.complete;
            }

            return false;
        }
示例#9
0
        /// <summary>
        /// Determines whether the given gesture is in the list of gestures for the specified user.
        /// </summary>
        /// <returns><c>true</c> if the gesture is in the list of gestures for the specified user; otherwise, <c>false</c>.</returns>
        /// <param name="UserId">User ID</param>
        /// <param name="gesture">Gesture type</param>
        public bool IsTrackingGesture(Int64 UserId, KinectGestures.Gestures gesture)
        {
            List<KinectGestures.GestureData> gesturesData = playerGesturesData.ContainsKey(UserId) ? playerGesturesData[UserId] : null;
            int index = gesturesData != null ? GetGestureIndex(gesture, ref gesturesData) : -1;

            return index >= 0;
        }
示例#10
0
        /// <summary>
        /// Deletes the gesture for the specified user.
        /// </summary>
        /// <returns><c>true</c>, if gesture was deleted, <c>false</c> otherwise.</returns>
        /// <param name="UserId">User ID</param>
        /// <param name="gesture">Gesture type</param>
        public bool DeleteGesture(Int64 UserId, KinectGestures.Gestures gesture)
        {
            List<KinectGestures.GestureData> gesturesData = playerGesturesData.ContainsKey(UserId) ? playerGesturesData[UserId] : null;
            int index = gesturesData != null ? GetGestureIndex(gesture, ref gesturesData) : -1;
            if (index < 0)
                return false;

            gesturesData.RemoveAt(index);
            playerGesturesData[UserId] = gesturesData;

            return true;
        }
示例#11
0
        /// <summary>
        /// Resets the gesture state for the given gesture of the specified user.
        /// </summary>
        /// <returns><c>true</c>, if gesture was reset, <c>false</c> otherwise.</returns>
        /// <param name="UserId">User ID</param>
        /// <param name="gesture">Gesture type</param>
        public bool ResetGesture(Int64 UserId, KinectGestures.Gestures gesture)
        {
            List<KinectGestures.GestureData> gesturesData = playerGesturesData.ContainsKey(UserId) ? playerGesturesData[UserId] : null;
            int index = gesturesData != null ? GetGestureIndex(gesture, ref gesturesData) : -1;
            if (index < 0)
                return false;

            KinectGestures.GestureData gestureData = gesturesData[index];

            gestureData.state = 0;
            gestureData.joint = 0;
            gestureData.progress = 0f;
            gestureData.complete = false;
            gestureData.cancelled = false;
            gestureData.startTrackingAtTime = Time.realtimeSinceStartup + KinectInterop.Constants.MinTimeBetweenSameGestures;

            gesturesData[index] = gestureData;
            playerGesturesData[UserId] = gesturesData;

            return true;
        }
示例#12
0
        /// <summary>
        /// Adds a gesture to the list of detected gestures for the specified user.
        /// </summary>
        /// <param name="UserId">User ID</param>
        /// <param name="gesture">Gesture type</param>
        public void DetectGesture(Int64 UserId, KinectGestures.Gestures gesture)
        {
            List<KinectGestures.GestureData> gesturesData = playerGesturesData.ContainsKey(UserId) ? playerGesturesData[UserId] : new List<KinectGestures.GestureData>();
            int index = GetGestureIndex(gesture, ref gesturesData);

            if (index >= 0)
            {
                DeleteGesture(UserId, gesture);
            }

            KinectGestures.GestureData gestureData = new KinectGestures.GestureData();

            gestureData.userId = UserId;
            gestureData.gesture = gesture;
            gestureData.state = 0;
            gestureData.joint = 0;
            gestureData.progress = 0f;
            gestureData.complete = false;
            gestureData.cancelled = false;

            gestureData.checkForGestures = new List<KinectGestures.Gestures>();
            switch (gesture)
            {
                case KinectGestures.Gestures.ZoomIn:
                    gestureData.checkForGestures.Add(KinectGestures.Gestures.ZoomOut);
                    gestureData.checkForGestures.Add(KinectGestures.Gestures.Wheel);
                    break;

                case KinectGestures.Gestures.ZoomOut:
                    gestureData.checkForGestures.Add(KinectGestures.Gestures.ZoomIn);
                    gestureData.checkForGestures.Add(KinectGestures.Gestures.Wheel);
                    break;

                case KinectGestures.Gestures.Wheel:
                    gestureData.checkForGestures.Add(KinectGestures.Gestures.ZoomIn);
                    gestureData.checkForGestures.Add(KinectGestures.Gestures.ZoomOut);
                    break;
            }

            gesturesData.Add(gestureData);
            playerGesturesData[UserId] = gesturesData;

            if (!gesturesTrackingAtTime.ContainsKey(UserId))
            {
                gesturesTrackingAtTime[UserId] = 0f;
            }
        }
        /// <summary>
        /// Invoked if a gesture is completed.
        /// </summary>
        /// <returns>true</returns>
        /// <c>false</c>
        /// <param name="userId">User ID</param>
        /// <param name="userIndex">User index</param>
        /// <param name="gesture">Gesture type</param>
        /// <param name="joint">Joint type</param>
        /// <param name="screenPos">Normalized viewport position</param>
        public bool GestureCompleted(long userId, int userIndex, KinectGestures.Gestures gesture,
                                      KinectInterop.JointType joint, Vector3 screenPos)
        {
            // the gestures are allowed for the primary user only
            KinectManager manager = KinectManager.Instance;
            if (!manager || (userId != manager.GetPrimaryUserID()))
                return false;

            /*if (gestureInfo != null)
            {
                string sGestureText = gesture + " detected";
                gestureInfo.GetComponent<GUIText>().text = sGestureText;
            }*/
            //else
                //Debug.LogWarning(gesture + " detected");

            switch (gesture)
            {
                case KinectGestures.Gestures.SwipeLeft:
                    swipeLeft = true;
                    break;
                case KinectGestures.Gestures.SwipeRight:
                    swipeRight = true;
                    break;
                case KinectGestures.Gestures.SwipeUp:
                    swipeUp = true;
                    break;
                /*case KinectGestures.Gestures.Click:
                    click = true;
                    break;*/
                case KinectGestures.Gestures.HiddenGesture:
                    hiddenGesture = true;
                    break;
            }

            return true;
        }
        /// <summary>
        /// Invoked when a gesture is in progress.
        /// </summary>
        /// <param name="userId">User ID</param>
        /// <param name="userIndex">User index</param>
        /// <param name="gesture">Gesture type</param>
        /// <param name="progress">Gesture progress [0..1]</param>
        /// <param name="joint">Joint type</param>
        /// <param name="screenPos">Normalized viewport position</param>
        public void GestureInProgress(long userId, int userIndex, KinectGestures.Gestures gesture,
                                      float progress, KinectInterop.JointType joint, Vector3 screenPos)
        {
            // the gestures are allowed for the primary user only
            KinectManager manager = KinectManager.Instance;
            if (!manager || (userId != manager.GetPrimaryUserID()))
                return;

            // this function is currently needed only to display gesture progress, skip it otherwise
            //if (gestureInfo == null)
                //return;

            if ((gesture == KinectGestures.Gestures.ZoomOut || gesture == KinectGestures.Gestures.ZoomIn) && progress > 0.5f)
            {
                /*if (gestureInfo != null)
                {
                    string sGestureText = string.Format("{0} - {1:F0}%", gesture, screenPos.z * 100f);
                    gestureInfo.GetComponent<GUIText>().text = sGestureText;

                    progressDisplayed = true;
                    //progressGestureTime = Time.realtimeSinceStartup;
                }*/
                //else
                    //Debug.LogWarning(string.Format("{0} - {1:F0}%", gesture, screenPos.z * 100f));
            }
            else if ((gesture == KinectGestures.Gestures.Wheel || gesture == KinectGestures.Gestures.LeanLeft ||
                     gesture == KinectGestures.Gestures.LeanRight) && progress > 0.5f)
            {
                /*if (gestureInfo != null)
                {
                    string sGestureText = string.Format("{0} - {1:F0} degrees", gesture, screenPos.z);
                    gestureInfo.GetComponent<GUIText>().text = sGestureText;

                    progressDisplayed = true;
                    //progressGestureTime = Time.realtimeSinceStartup;
                }*/
                //else
                    //Debug.LogWarning(string.Format("{0} - {1:F0} degrees", gesture, screenPos.z));
            }
        }