public HandEventType GetHandState(HandType handType) { HandEventType handEventType = HandEventType.None; if (!InteractionManager.Instance.IsInteractionInited()) { return(handEventType); } else { switch (handType) { case HandType.Left: handEventType = InteractionManager.Instance.GetLastLeftHandEvent(); break; case HandType.Right: handEventType = InteractionManager.Instance.GetLastRightHandEvent(); break; default: break; } } return(handEventType); }
void Update() { KinectManager kinectManager = KinectManager.Instance; // update Kinect interaction if (kinectManager && kinectManager.IsInitialized()) { List <Int64> aluserIds = kinectManager.GetAlUserIds(); while (leftHandState.Count < aluserIds.Count) { leftHandEvent.Add(HandEventType.None); lastLeftHandEvent.Add(HandEventType.None); rightHandEvent.Add(HandEventType.None); lastRightHandEvent.Add(HandEventType.None); leftHandState.Add(KinectInterop.HandState.Unknown); rightHandState.Add(KinectInterop.HandState.Unknown); } for (int userIndex = 0; userIndex < aluserIds.Count; userIndex++) { if (aluserIds[userIndex] != 0) // User found { HandEventType handEvent = HandEventType.None; // get the left hand state leftHandState[userIndex] = kinectManager.GetLeftHandState(aluserIds[userIndex]); // get the right hand state rightHandState[userIndex] = kinectManager.GetRightHandState(aluserIds[userIndex]); // process left hand handEvent = HandStateToEvent(leftHandState[userIndex], lastLeftHandEvent[userIndex]); leftHandEvent[userIndex] = handEvent; if (handEvent != HandEventType.None) { lastLeftHandEvent[userIndex] = handEvent; } // process right hand handEvent = HandStateToEvent(rightHandState[userIndex], lastRightHandEvent[userIndex]); rightHandEvent[userIndex] = handEvent; if (handEvent != HandEventType.None) { lastRightHandEvent[userIndex] = handEvent; } } else // User not found { leftHandState[userIndex] = KinectInterop.HandState.NotTracked; rightHandState[userIndex] = KinectInterop.HandState.NotTracked; leftHandEvent[userIndex] = HandEventType.None; rightHandEvent[userIndex] = HandEventType.None; lastLeftHandEvent[userIndex] = HandEventType.Release; lastRightHandEvent[userIndex] = HandEventType.Release; } } } }
void CatchEvent(InputDeviceHandPart inputDeviceHandPart, HandEventType eventType) { if (eventType == HandEventType.CatchDown && inputDeviceHandPart == inputDataGetHand.inputDeviceHandPart) { inputDataGetHand.inputDevicePartBase.inputDataBase.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.DOWN); } else if (eventType == HandEventType.CatchUp && inputDeviceHandPart == inputDataGetHand.inputDeviceHandPart) { inputDataGetHand.inputDevicePartBase.inputDataBase.inputKeys.InputDataAddKey(InputKeyCode.Enter, InputKeyState.UP); } }
/// <summary> /// constructor of the interface /// </summary> public SmoothedHandDataInterface() { _hd = HandData.Instance; hand_rotation_ = Quaternion.identity; current_event = HandEventType.None; handEventStorage = new HandEventType[storageSize]; for (int i = 0; i < storageSize; i++) { handEventStorage[i] = HandEventType.None; } }
// converts hand state to hand event type public static HandEventType HandStateToEvent(KinectInterop.HandState handState, HandEventType lastEventType) { switch (handState) { case KinectInterop.HandState.Open: return(HandEventType.Release); case KinectInterop.HandState.Closed: case KinectInterop.HandState.Lasso: return(HandEventType.Grip); case KinectInterop.HandState.Unknown: return(lastEventType); } return(HandEventType.None); }
// converts hand state to hand event type private HandEventType HandStateToEvent(HandState handState, HandEventType lastEventType) { switch (handState) { case HandState.Open: return(HandEventType.Release); case HandState.Closed: case HandState.Lasso: return(HandEventType.Grip); case HandState.Unknown: return(lastEventType); } return(HandEventType.None); }
/// <summary> /// returns the state of the hand /// </summary> /// <param name="isLeftHand"></param> /// <returns></returns> public HandEventType getHandEvent(bool isLeftHand) { if (!HandData.IsHandDataInitialized()) _hd = HandData.Instance; HandEventType new_event = _hd.getHandEvent (isLeftHand); int current_event_counter = 0; handEventStorage [storageIterator % storageSize] = new_event; for (int i = 0; i < storageSize; i++) { if(handEventStorage[i] == new_event) current_event_counter++; } if (current_event_counter == storageSize) { current_event = new_event; } storageIterator++; return current_event; }
//Debug.Log ("Player: "+ playerIndex + " Hand Position: " + rightHandPos); // converts hand state to hand event type private HandEventType HandStateToEvent(KinectInterop.HandState handState, HandEventType lastEventType) { switch (handState) { case KinectInterop.HandState.Open: return(HandEventType.Release); case KinectInterop.HandState.Closed: return(HandEventType.Grip); //michael 01/11/17 added closed to return grip only instead of closed and lasso case KinectInterop.HandState.Lasso: return(HandEventType.Release); // return HandEventType.Grip; case KinectInterop.HandState.Unknown: return(lastEventType); //michael 01/11/17 } return(HandEventType.None); }
void Update() { KinectManager kinectManager = KinectManager.Instance; // update Kinect interaction if (kinectManager && kinectManager.IsInitialized()) { playerUserID = kinectManager.GetUserIdByIndex(playerIndex); if (playerUserID != 0) { HandEventType handEvent = HandEventType.None; // get the left hand state leftHandState = kinectManager.GetLeftHandState(playerUserID); // check if the left hand is interacting isleftIboxValid = kinectManager.GetLeftHandInteractionBox(playerUserID, ref leftIboxLeftBotBack, ref leftIboxRightTopFront, isleftIboxValid); //bool bLeftHandPrimaryNow = false; // was the left hand interacting till now bool wasLeftHandInteracting = isLeftHandInteracting; if (isleftIboxValid && //bLeftHandPrimaryNow && kinectManager.GetJointTrackingState(playerUserID, (int)KinectInterop.JointType.HandLeft) != KinectInterop.TrackingState.NotTracked) { leftHandPos = kinectManager.GetJointPosition(playerUserID, (int)KinectInterop.JointType.HandLeft); leftHandScreenPos.x = Mathf.Clamp01((leftHandPos.x - leftIboxLeftBotBack.x) / (leftIboxRightTopFront.x - leftIboxLeftBotBack.x)); leftHandScreenPos.y = Mathf.Clamp01((leftHandPos.y - leftIboxLeftBotBack.y) / (leftIboxRightTopFront.y - leftIboxLeftBotBack.y)); leftHandScreenPos.z = Mathf.Clamp01((leftIboxLeftBotBack.z - leftHandPos.z) / (leftIboxLeftBotBack.z - leftIboxRightTopFront.z)); isLeftHandInteracting = (leftHandPos.x >= (leftIboxLeftBotBack.x - 1.0f)) && (leftHandPos.x <= (leftIboxRightTopFront.x + 0.5f)) && (leftHandPos.y >= (leftIboxLeftBotBack.y - 0.1f)) && (leftHandPos.y <= (leftIboxRightTopFront.y + 0.7f)) && (leftIboxLeftBotBack.z >= leftHandPos.z) && (leftIboxRightTopFront.z * 0.8f <= leftHandPos.z); //bLeftHandPrimaryNow = isLeftHandInteracting; // start interacting? if (!wasLeftHandInteracting && isLeftHandInteracting) { leftHandInteractingSince = Time.realtimeSinceStartup; } // check for left press isLeftHandPress = ((leftIboxRightTopFront.z - 0.1f) >= leftHandPos.z); // check for left hand click if (allowHandClicks && !dragInProgress && isLeftHandInteracting && ((leftHandPos - lastLeftHandPos).magnitude < KinectInterop.Constants.ClickMaxDistance)) { if ((Time.realtimeSinceStartup - lastLeftHandTime) >= KinectInterop.Constants.ClickStayDuration) { if (!isLeftHandClick) { isLeftHandClick = true; leftHandClickProgress = 1f; foreach (InteractionListenerInterface listener in interactionListeners) { if (listener.HandClickDetected(playerUserID, playerIndex, false, leftHandScreenPos)) { isLeftHandClick = false; leftHandClickProgress = 0f; lastLeftHandPos = Vector3.zero; lastLeftHandTime = Time.realtimeSinceStartup; } } if (controlMouseCursor) { MouseControl.MouseClick(); isLeftHandClick = false; leftHandClickProgress = 0f; lastLeftHandPos = Vector3.zero; lastLeftHandTime = Time.realtimeSinceStartup; } } } else { leftHandClickProgress = (Time.realtimeSinceStartup - lastLeftHandTime) / KinectInterop.Constants.ClickStayDuration; } } else { isLeftHandClick = false; leftHandClickProgress = 0f; lastLeftHandPos = leftHandPos; lastLeftHandTime = Time.realtimeSinceStartup; } } else { isLeftHandInteracting = false; isLeftHandPress = false; } // if left hand just stopped interacting, send extra non-interaction event if (wasLeftHandInteracting && !isLeftHandInteracting) { foreach (InteractionListenerInterface listener in interactionListeners) { if (lastLeftHandEvent == HandEventType.Grip) { listener.HandGripDetected(playerUserID, playerIndex, false, isLeftHandInteracting, leftHandScreenPos); } else //if(lastLeftHandEvent == HandEventType.Release) { listener.HandReleaseDetected(playerUserID, playerIndex, false, isLeftHandInteracting, leftHandScreenPos); } } } // get the right hand state rightHandState = kinectManager.GetRightHandState(playerUserID); // check if the right hand is interacting isRightIboxValid = kinectManager.GetRightHandInteractionBox(playerUserID, ref rightIboxLeftBotBack, ref rightIboxRightTopFront, isRightIboxValid); //bool bRightHandPrimaryNow = false; // was the right hand interacting till now bool wasRightHandInteracting = isRightHandInteracting; if (isRightIboxValid && //bRightHandPrimaryNow && kinectManager.GetJointTrackingState(playerUserID, (int)KinectInterop.JointType.HandRight) != KinectInterop.TrackingState.NotTracked) { rightHandPos = kinectManager.GetJointPosition(playerUserID, (int)KinectInterop.JointType.HandRight); rightHandScreenPos.x = Mathf.Clamp01((rightHandPos.x - rightIboxLeftBotBack.x) / (rightIboxRightTopFront.x - rightIboxLeftBotBack.x)); rightHandScreenPos.y = Mathf.Clamp01((rightHandPos.y - rightIboxLeftBotBack.y) / (rightIboxRightTopFront.y - rightIboxLeftBotBack.y)); rightHandScreenPos.z = Mathf.Clamp01((rightIboxLeftBotBack.z - rightHandPos.z) / (rightIboxLeftBotBack.z - rightIboxRightTopFront.z)); isRightHandInteracting = (rightHandPos.x >= (rightIboxLeftBotBack.x - 0.5f)) && (rightHandPos.x <= (rightIboxRightTopFront.x + 1.0f)) && (rightHandPos.y >= (rightIboxLeftBotBack.y - 0.1f)) && (rightHandPos.y <= (rightIboxRightTopFront.y + 0.7f)) && (rightIboxLeftBotBack.z >= rightHandPos.z) && (rightIboxRightTopFront.z * 0.8f <= rightHandPos.z); //bRightHandPrimaryNow = isRightHandInteracting; if (!wasRightHandInteracting && isRightHandInteracting) { rightHandInteractingSince = Time.realtimeSinceStartup; } if (isLeftHandInteracting && isRightHandInteracting) { if (rightHandInteractingSince <= leftHandInteractingSince) { isLeftHandInteracting = false; } else { isRightHandInteracting = false; } } // check for right press isRightHandPress = ((rightIboxRightTopFront.z - 0.1f) >= rightHandPos.z); // check for right hand click if (allowHandClicks && !dragInProgress && isRightHandInteracting && ((rightHandPos - lastRightHandPos).magnitude < KinectInterop.Constants.ClickMaxDistance)) { if ((Time.realtimeSinceStartup - lastRightHandTime) >= KinectInterop.Constants.ClickStayDuration) { if (!isRightHandClick) { isRightHandClick = true; rightHandClickProgress = 1f; foreach (InteractionListenerInterface listener in interactionListeners) { if (listener.HandClickDetected(playerUserID, playerIndex, true, rightHandScreenPos)) { isRightHandClick = false; rightHandClickProgress = 0f; lastRightHandPos = Vector3.zero; lastRightHandTime = Time.realtimeSinceStartup; } } if (controlMouseCursor) { MouseControl.MouseClick(); isRightHandClick = false; rightHandClickProgress = 0f; lastRightHandPos = Vector3.zero; lastRightHandTime = Time.realtimeSinceStartup; } } } else { rightHandClickProgress = (Time.realtimeSinceStartup - lastRightHandTime) / KinectInterop.Constants.ClickStayDuration; } } else { isRightHandClick = false; rightHandClickProgress = 0f; lastRightHandPos = rightHandPos; lastRightHandTime = Time.realtimeSinceStartup; } } else { isRightHandInteracting = false; isRightHandPress = false; } // if right hand just stopped interacting, send extra non-interaction event if (wasRightHandInteracting && !isRightHandInteracting) { foreach (InteractionListenerInterface listener in interactionListeners) { if (lastRightHandEvent == HandEventType.Grip) { listener.HandGripDetected(playerUserID, playerIndex, true, isRightHandInteracting, rightHandScreenPos); } else //if(lastRightHandEvent == HandEventType.Release) { listener.HandReleaseDetected(playerUserID, playerIndex, true, isRightHandInteracting, rightHandScreenPos); } } } // process left hand handEvent = HandStateToEvent(leftHandState, lastLeftHandEvent); if ((isLeftHandInteracting != isLeftHandPrimary) || (isRightHandInteracting != isRightHandPrimary)) { if (controlMouseCursor && dragInProgress) { MouseControl.MouseRelease(); dragInProgress = false; } lastLeftHandEvent = HandEventType.Release; lastRightHandEvent = HandEventType.Release; } if (controlMouseCursor && (handEvent != lastLeftHandEvent)) { if (controlMouseDrag && !dragInProgress && (handEvent == HandEventType.Grip)) { dragInProgress = true; MouseControl.MouseDrag(); } else if (dragInProgress && (handEvent == HandEventType.Release)) { MouseControl.MouseRelease(); dragInProgress = false; } } leftHandEvent = handEvent; if (handEvent != HandEventType.None) { if (leftHandEvent != lastLeftHandEvent) { foreach (InteractionListenerInterface listener in interactionListeners) { if (leftHandEvent == HandEventType.Grip) { listener.HandGripDetected(playerUserID, playerIndex, false, isLeftHandInteracting, leftHandScreenPos); } else if (leftHandEvent == HandEventType.Release) { listener.HandReleaseDetected(playerUserID, playerIndex, false, isLeftHandInteracting, leftHandScreenPos); } } } lastLeftHandEvent = handEvent; } // if the hand is primary, set the cursor position if (isLeftHandInteracting) { isLeftHandPrimary = true; if ((leftHandClickProgress < 0.8f) /**&& !isLeftHandPress*/) { float smooth = smoothFactor * Time.deltaTime; if (smooth == 0f) { smooth = 1f; } cursorScreenPos = Vector3.Lerp(cursorScreenPos, leftHandScreenPos, smooth); } // move mouse-only if there is no cursor texture if (controlMouseCursor && (!showHandCursor || (!gripHandTexture && !releaseHandTexture && !normalHandTexture))) { MouseControl.MouseMove(cursorScreenPos, debugText); } } else { isLeftHandPrimary = false; } // process right hand handEvent = HandStateToEvent(rightHandState, lastRightHandEvent); if (controlMouseCursor && (handEvent != lastRightHandEvent)) { if (controlMouseDrag && !dragInProgress && (handEvent == HandEventType.Grip)) { dragInProgress = true; MouseControl.MouseDrag(); } else if (dragInProgress && (handEvent == HandEventType.Release)) { MouseControl.MouseRelease(); dragInProgress = false; } } rightHandEvent = handEvent; if (handEvent != HandEventType.None) { if (rightHandEvent != lastRightHandEvent) { foreach (InteractionListenerInterface listener in interactionListeners) { if (rightHandEvent == HandEventType.Grip) { listener.HandGripDetected(playerUserID, playerIndex, true, isRightHandInteracting, rightHandScreenPos); } else if (rightHandEvent == HandEventType.Release) { listener.HandReleaseDetected(playerUserID, playerIndex, true, isRightHandInteracting, rightHandScreenPos); } } } lastRightHandEvent = handEvent; } // if the hand is primary, set the cursor position if (isRightHandInteracting) { isRightHandPrimary = true; if ((rightHandClickProgress < 0.8f) /**&& !isRightHandPress*/) { float smooth = smoothFactor * Time.deltaTime; if (smooth == 0f) { smooth = 1f; } cursorScreenPos = Vector3.Lerp(cursorScreenPos, rightHandScreenPos, smooth); } // move mouse-only if there is no cursor texture if (controlMouseCursor && (!showHandCursor || (!gripHandTexture && !releaseHandTexture && !normalHandTexture))) { MouseControl.MouseMove(cursorScreenPos, debugText); } } else { isRightHandPrimary = false; } } else { leftHandState = KinectInterop.HandState.NotTracked; rightHandState = KinectInterop.HandState.NotTracked; isLeftHandPrimary = false; isRightHandPrimary = false; isLeftHandPress = false; isRightHandPress = false; leftHandEvent = HandEventType.None; rightHandEvent = HandEventType.None; lastLeftHandEvent = HandEventType.Release; lastRightHandEvent = HandEventType.Release; if (controlMouseCursor && dragInProgress) { MouseControl.MouseRelease(); dragInProgress = false; } } } }
private void pullState(long userID) { if(userID != 0) { HandEventType handEvent = HandEventType.None; // get the left hand state leftHandState = _km.GetLeftHandState(userID); // get the right hand state rightHandState = _km.GetRightHandState(userID); // process left hand handEvent = HandStateToEvent(leftHandState, lastLeftHandEvent); leftHandEvent = handEvent; if(handEvent != HandEventType.None) { lastLeftHandEvent = handEvent; } // process right hand handEvent = HandStateToEvent(rightHandState, lastRightHandEvent); rightHandEvent = handEvent; if(handEvent != HandEventType.None) { lastRightHandEvent = handEvent; } } else { leftHandState = KinectInterop.HandState.NotTracked; rightHandState = KinectInterop.HandState.NotTracked; leftHandEvent = HandEventType.None; rightHandEvent = HandEventType.None; lastLeftHandEvent = HandEventType.Release; lastRightHandEvent = HandEventType.Release; } }
void Update() { kinectManager = KinectManager.Instance; //Debug.Log ("Player: "+ playerIndex + " Hand Position: " + rightHandScreenPos); // update Kinect interaction if (kinectManager && kinectManager.IsInitialized()) { primaryUserID = kinectManager.GetUserIdByIndex(playerIndex); if (primaryUserID != 0) { HandEventType handEvent = HandEventType.None; // get the left hand state leftHandState = kinectManager.GetLeftHandState(primaryUserID); // check if the left hand is interacting isleftIboxValid = kinectManager.GetLeftHandInteractionBox(primaryUserID, ref leftIboxLeftBotBack, ref leftIboxRightTopFront, isleftIboxValid); //bool bLeftHandPrimaryNow = false; if (isleftIboxValid && //bLeftHandPrimaryNow && kinectManager.GetJointTrackingState(primaryUserID, (int)KinectInterop.JointType.HandLeft) != KinectInterop.TrackingState.NotTracked && (UseLeftHand || UseFreeHand)) { leftHandPos = kinectManager.GetJointPosition(primaryUserID, (int)KinectInterop.JointType.HandLeft); leftHandScreenPos.x = Mathf.Clamp01((leftHandPos.x - leftIboxLeftBotBack.x) / (leftIboxRightTopFront.x) - leftIboxLeftBotBack.x); leftHandScreenPos.y = Mathf.Clamp01((leftHandPos.y - leftIboxLeftBotBack.y) / (leftIboxRightTopFront.y - leftIboxLeftBotBack.y)); leftHandScreenPos.z = Mathf.Clamp01((leftIboxLeftBotBack.z - leftHandPos.z) / (leftIboxLeftBotBack.z - leftIboxRightTopFront.z)); bool wasLeftHandInteracting = isLeftHandInteracting; isLeftHandInteracting = (leftHandPos.x >= (leftIboxLeftBotBack.x - 1.0f)) && (leftHandPos.x <= (leftIboxRightTopFront.x + 0.5f)) && (leftHandPos.y >= (leftIboxLeftBotBack.y - 0.1f)) && (leftHandPos.y <= (leftIboxRightTopFront.y + 0.7f)) && (leftIboxLeftBotBack.z >= leftHandPos.z) && (leftIboxRightTopFront.z * 0.8f <= leftHandPos.z); //bLeftHandPrimaryNow = isLeftHandInteracting; if (!wasLeftHandInteracting && isLeftHandInteracting) { leftHandInteractingSince = Time.realtimeSinceStartup; } // check for left press //isLeftHandPress = ((leftIboxRightTopFront.z - 0.1f) >= leftHandPos.z);//michael commenting out code 1/11/2017 // check for left hand click //michael 1/11/2017 float fClickDist = (leftHandPos - lastLeftHandPos).magnitude; /* * if(allowHandClicks && isLeftHandInteracting && (fClickDist < KinectInterop.Constants.ClickMaxDistance)) * { * if((Time.realtimeSinceStartup - lastLeftHandTime) >= KinectInterop.Constants.ClickStayDuration) * { * if(!isLeftHandClick) * { * isLeftHandClick = true; * leftHandClickProgress = 1f; * * if(controlMouseCursor) * { * MouseControl.MouseClick(); * * isLeftHandClick = false; * leftHandClickProgress = 0f; * lastLeftHandPos = Vector3.zero; * lastLeftHandTime = Time.realtimeSinceStartup; * } * } * } * else * { * leftHandClickProgress = (Time.realtimeSinceStartup - lastLeftHandTime) / KinectInterop.Constants.ClickStayDuration; * } * } */ /*else if(!allowHandClicks) * { * isLeftHandClick = false; * leftHandClickProgress = 0f; * lastLeftHandPos = leftHandPos; * lastLeftHandTime = Time.realtimeSinceStartup; * } */ } else { isLeftHandInteracting = false; isLeftHandPress = false; } //michael 1/11/2017 // get the right hand state rightHandState = kinectManager.GetRightHandState(primaryUserID); // check if the right hand is interacting isRightIboxValid = kinectManager.GetRightHandInteractionBox(primaryUserID, ref rightIboxLeftBotBack, ref rightIboxRightTopFront, isRightIboxValid); //bool bRightHandPrimaryNow = false; if (isRightIboxValid && //bRightHandPrimaryNow && kinectManager.GetJointTrackingState(primaryUserID, (int)KinectInterop.JointType.HandRight) != KinectInterop.TrackingState.NotTracked && (!UseLeftHand || UseFreeHand)) { rightHandPos = kinectManager.GetJointPosition(primaryUserID, (int)KinectInterop.JointType.HandRight); leftShoulder = kinectManager.GetJointPosition(primaryUserID, (int)KinectInterop.JointType.ShoulderLeft); rightShoulder = kinectManager.GetJointPosition(primaryUserID, (int)KinectInterop.JointType.ShoulderRight); rightHip = kinectManager.GetJointPosition(primaryUserID, (int)KinectInterop.JointType.HipRight); head = kinectManager.GetJointPosition(primaryUserID, (int)KinectInterop.JointType.Head); // rightHandPos.x+=10f; // rightHandPos.x=leftShoulder.x-rightHandPos.x; /* * rightHandPos.x+=Math.Abs(rightHandPos.x)*.00001f; * rightHandPos.y+=Math.Abs(rightHandPos.y)*.00001f; */ //Debug.Log("righthandpos="+rightHandPos); //rightHandScreenPos.x=Mathf.Clamp01(rightHandPos.x); //rightHandScreenPos.x=Mathf.InverseLerp(rightHandPos.x,1.4f,1); //Debug.Log("rightHandScreenPos.x "+ rightHandScreenPos.x); //rightHandScreenPos.x = ((rightHandPos.x - leftShoulder.x)/(-.04f-leftShoulder.x)); rightHandScreenPos.x = Mathf.Clamp01((rightHandPos.x - (leftShoulder.x + .2f)) / ((rightShoulder.x + .3f) - (leftShoulder.x + .2f))); //rightHandScreenPos.x= Mathf.Clamp01((rightHandPos.x - rightIboxLeftBotBack.x) / (rightIboxRightTopFront.x - rightIboxLeftBotBack.x));//Mathf.Clamp(((rightHandPos.x + rightIboxLeftBotBack.x) / (rightIboxRightTopFront.x - rightIboxLeftBotBack.x)),0,2); rightHandScreenPos.y = Mathf.Clamp01((((rightHandPos.y) - (rightHip.y + .2f)) / ((head.y) - (rightHip.y + .2f)))); //rightHandScreenPos.y = Mathf.Clamp01((rightHandPos.y - rightIboxLeftBotBack.y) / (rightIboxRightTopFront.y - rightIboxLeftBotBack.y));////Mathf.Clamp(((rightHandPos.y - rightIboxLeftBotBack.y) / (rightIboxRightTopFront.y - rightIboxLeftBotBack.y)),0,2); rightHandScreenPos.z = Mathf.Clamp01((rightIboxLeftBotBack.z - rightHandPos.z) / (rightIboxLeftBotBack.z - rightIboxRightTopFront.z)); //Debug.Log("rightHandScreenPos.x="+rightHandScreenPos.x); bool wasRightHandInteracting = isRightHandInteracting; isRightHandInteracting = (rightHandPos.x >= (rightIboxLeftBotBack.x - 0.5f)) && (rightHandPos.x <= (rightIboxRightTopFront.x + 1.0f)) && (rightHandPos.y >= (rightIboxLeftBotBack.y - 0.1f)) && (rightHandPos.y <= (rightIboxRightTopFront.y + 0.7f)) && (rightIboxLeftBotBack.z >= rightHandPos.z) && (rightIboxRightTopFront.z * 0.8f <= rightHandPos.z); //bRightHandPrimaryNow = isRightHandInteracting; if (!wasRightHandInteracting && isRightHandInteracting) { rightHandInteractingSince = Time.realtimeSinceStartup; } if (isLeftHandInteracting && isRightHandInteracting) { if (rightHandInteractingSince <= leftHandInteractingSince) { isLeftHandInteracting = false; } else { isRightHandInteracting = false; } } // check for right press isRightHandPress = ((rightIboxRightTopFront.z - 0.1f) >= rightHandPos.z); /* * // check for right hand click //michael commenting out code 1/11/2017 * float fClickDist = (rightHandPos - lastRightHandPos).magnitude; * if(allowHandClicks && isRightHandInteracting && (fClickDist < KinectInterop.Constants.ClickMaxDistance)) * { * if((Time.realtimeSinceStartup - lastRightHandTime) >= KinectInterop.Constants.ClickStayDuration) * { * if(!isRightHandClick) * { * isRightHandClick = true; * rightHandClickProgress = 1f; * * if(controlMouseCursor) * { * MouseControl.MouseClick(); * * isRightHandClick = false; * rightHandClickProgress = 0f; * lastRightHandPos = Vector3.zero; * lastRightHandTime = Time.realtimeSinceStartup; * } * } * } * else * { * rightHandClickProgress = (Time.realtimeSinceStartup - lastRightHandTime) / KinectInterop.Constants.ClickStayDuration; * } * } * else * { * isRightHandClick = false; * rightHandClickProgress = 0f; * lastRightHandPos = rightHandPos; * lastRightHandTime = Time.realtimeSinceStartup; * } */ } else { isRightHandInteracting = false; isRightHandPress = false; } // process left hand handEvent = HandStateToEvent(leftHandState, lastLeftHandEvent); if ((isLeftHandInteracting != isLeftHandPrimary) || (isRightHandInteracting != isRightHandPrimary)) { if (controlMouseCursor && dragInProgress) { MouseControl.MouseRelease(); dragInProgress = false; } lastLeftHandEvent = HandEventType.Release; lastRightHandEvent = HandEventType.Release; } if (controlMouseCursor && (handEvent != lastLeftHandEvent)) { if (controlMouseDrag && !dragInProgress && (handEvent == HandEventType.Grip)) { dragInProgress = true; MouseControl.MouseDrag(); } else if (dragInProgress && (handEvent == HandEventType.Release)) { MouseControl.MouseRelease(); dragInProgress = false; } } leftHandEvent = handEvent; if (handEvent != HandEventType.None) { lastLeftHandEvent = handEvent; } // if the hand is primary, set the cursor position if (isLeftHandInteracting) { isLeftHandPrimary = true; if ((leftHandClickProgress < 0.8f) /**&& !isLeftHandPress*/) { cursorScreenPos = Vector3.Lerp(cursorScreenPos, leftHandScreenPos, smoothFactor * Time.deltaTime); } } else { isLeftHandPrimary = false; } // process right hand handEvent = HandStateToEvent(rightHandState, lastRightHandEvent); if (controlMouseCursor && (handEvent != lastRightHandEvent)) { if (controlMouseDrag && !dragInProgress && (handEvent == HandEventType.Grip)) { dragInProgress = true; MouseControl.MouseDrag(); } else if (dragInProgress && (handEvent == HandEventType.Release)) { MouseControl.MouseRelease(); dragInProgress = false; } } rightHandEvent = handEvent; if (handEvent != HandEventType.None) { lastRightHandEvent = handEvent; } // if the hand is primary, set the cursor position if (isRightHandInteracting) { isRightHandPrimary = true; if ((rightHandClickProgress < 0.8f) /**&& !isRightHandPress*/) { cursorScreenPos = Vector3.Lerp(cursorScreenPos, rightHandScreenPos, smoothFactor * Time.deltaTime); } } else { isRightHandPrimary = false; } } else { leftHandState = KinectInterop.HandState.NotTracked; rightHandState = KinectInterop.HandState.NotTracked; isLeftHandPrimary = false; isRightHandPrimary = false; isLeftHandPress = false; isRightHandPress = false; leftHandEvent = HandEventType.None; rightHandEvent = HandEventType.None; lastLeftHandEvent = HandEventType.Release; lastRightHandEvent = HandEventType.Release; if (controlMouseCursor && dragInProgress) { MouseControl.MouseRelease(); dragInProgress = false; } } } }
void Update() { KinectManager kinectManager = KinectManager.Instance; // update Kinect interaction if(kinectManager && kinectManager.IsInitialized()) { primaryUserID = kinectManager.GetUserIdByIndex(playerIndex); if(primaryUserID != 0) { HandEventType handEvent = HandEventType.None; /* // get the left hand state leftHandState = kinectManager.GetLeftHandState(primaryUserID); // check if the left hand is interacting isleftIboxValid = kinectManager.GetLeftHandInteractionBox(primaryUserID, ref leftIboxLeftBotBack, ref leftIboxRightTopFront, isleftIboxValid); //bool bLeftHandPrimaryNow = false; if(isleftIboxValid && //bLeftHandPrimaryNow && kinectManager.GetJointTrackingState(primaryUserID, (int)KinectInterop.JointType.HandLeft) != KinectInterop.TrackingState.NotTracked) { leftHandPos = kinectManager.GetJointPosition(primaryUserID, (int)KinectInterop.JointType.HandLeft); leftHandScreenPos.x = Mathf.Clamp01((leftHandPos.x - leftIboxLeftBotBack.x) / (leftIboxRightTopFront.x - leftIboxLeftBotBack.x)); leftHandScreenPos.y = Mathf.Clamp01((leftHandPos.y - leftIboxLeftBotBack.y) / (leftIboxRightTopFront.y - leftIboxLeftBotBack.y)); leftHandScreenPos.z = Mathf.Clamp01((leftIboxLeftBotBack.z - leftHandPos.z) / (leftIboxLeftBotBack.z - leftIboxRightTopFront.z)); bool wasLeftHandInteracting = isLeftHandInteracting; isLeftHandInteracting = (leftHandPos.x >= (leftIboxLeftBotBack.x - 1.0f)) && (leftHandPos.x <= (leftIboxRightTopFront.x + 0.5f)) && (leftHandPos.y >= (leftIboxLeftBotBack.y - 0.1f)) && (leftHandPos.y <= (leftIboxRightTopFront.y + 0.7f)) && (leftIboxLeftBotBack.z >= leftHandPos.z) && (leftIboxRightTopFront.z * 0.8f <= leftHandPos.z); //bLeftHandPrimaryNow = isLeftHandInteracting; if(!wasLeftHandInteracting && isLeftHandInteracting) { leftHandInteractingSince = Time.realtimeSinceStartup; } // check for left press isLeftHandPress = ((leftIboxRightTopFront.z - 0.1f) >= leftHandPos.z); // check for left hand click float fClickDist = (leftHandPos - lastLeftHandPos).magnitude; if(allowHandClicks && !dragInProgress && isLeftHandInteracting && (fClickDist < KinectInterop.Constants.ClickMaxDistance)) { if((Time.realtimeSinceStartup - lastLeftHandTime) >= KinectInterop.Constants.ClickStayDuration) { if(!isLeftHandClick) { isLeftHandClick = true; leftHandClickProgress = 1f; if(controlMouseCursor) { MouseControl.MouseClick(); isLeftHandClick = false; leftHandClickProgress = 0f; lastLeftHandPos = Vector3.zero; lastLeftHandTime = Time.realtimeSinceStartup; } } } else { leftHandClickProgress = (Time.realtimeSinceStartup - lastLeftHandTime) / KinectInterop.Constants.ClickStayDuration; } } else { isLeftHandClick = false; leftHandClickProgress = 0f; lastLeftHandPos = leftHandPos; lastLeftHandTime = Time.realtimeSinceStartup; } } else { isLeftHandInteracting = false; isLeftHandPress = false; } */ // get the right hand state rightHandState = kinectManager.GetRightHandState(primaryUserID); // check if the right hand is interacting isRightIboxValid = kinectManager.GetRightHandInteractionBox(primaryUserID, ref rightIboxLeftBotBack, ref rightIboxRightTopFront, isRightIboxValid); //bool bRightHandPrimaryNow = false; if(isRightIboxValid && //bRightHandPrimaryNow && kinectManager.GetJointTrackingState(primaryUserID, (int)KinectInterop.JointType.HandRight) != KinectInterop.TrackingState.NotTracked) { rightHandPos = kinectManager.GetJointPosition(primaryUserID, (int)KinectInterop.JointType.HandRight); /* rightHandScreenPos.x = Mathf.Clamp01((rightHandPos.x - rightIboxLeftBotBack.x) / (rightIboxRightTopFront.x - rightIboxLeftBotBack.x)); rightHandScreenPos.y = Mathf.Clamp01((rightHandPos.y - rightIboxLeftBotBack.y) / (rightIboxRightTopFront.y - rightIboxLeftBotBack.y)); rightHandScreenPos.z = Mathf.Clamp01((rightIboxLeftBotBack.z - rightHandPos.z) / (rightIboxLeftBotBack.z - rightIboxRightTopFront.z)); */ rightHandScreenPos.x = rightHandPos.x; rightHandScreenPos.y = rightHandPos.y; rightHandScreenPos.z = -rightHandPos.z; bool wasRightHandInteracting = isRightHandInteracting; isRightHandInteracting = (rightHandPos.x >= (rightIboxLeftBotBack.x - 0.5f)) && (rightHandPos.x <= (rightIboxRightTopFront.x + 1.0f)) && (rightHandPos.y >= (rightIboxLeftBotBack.y - 0.1f)) && (rightHandPos.y <= (rightIboxRightTopFront.y + 0.7f)) && (rightIboxLeftBotBack.z >= rightHandPos.z) && (rightIboxRightTopFront.z * 0.8f <= rightHandPos.z); //bRightHandPrimaryNow = isRightHandInteracting; if(!wasRightHandInteracting && isRightHandInteracting) { rightHandInteractingSince = Time.realtimeSinceStartup; } if(isLeftHandInteracting && isRightHandInteracting) { if(rightHandInteractingSince <= leftHandInteractingSince) isLeftHandInteracting = false; else isRightHandInteracting = false; } // check for right press isRightHandPress = ((rightIboxRightTopFront.z - 0.1f) >= rightHandPos.z); // check for right hand click float fClickDist = (rightHandPos - lastRightHandPos).magnitude; if(allowHandClicks && !dragInProgress && isRightHandInteracting && (fClickDist < KinectInterop.Constants.ClickMaxDistance)) { if((Time.realtimeSinceStartup - lastRightHandTime) >= KinectInterop.Constants.ClickStayDuration) { if(!isRightHandClick) { isRightHandClick = true; rightHandClickProgress = 1f; if(controlMouseCursor) { //MouseControl.MouseClick(); isRightHandClick = false; rightHandClickProgress = 0f; lastRightHandPos = Vector3.zero; lastRightHandTime = Time.realtimeSinceStartup; } } } else { rightHandClickProgress = (Time.realtimeSinceStartup - lastRightHandTime) / KinectInterop.Constants.ClickStayDuration; } } else { isRightHandClick = false; rightHandClickProgress = 0f; lastRightHandPos = rightHandPos; lastRightHandTime = Time.realtimeSinceStartup; } } else { isRightHandInteracting = false; isRightHandPress = false; } /* // process left hand handEvent = HandStateToEvent(leftHandState, lastLeftHandEvent); if((isLeftHandInteracting != isLeftHandPrimary) || (isRightHandInteracting != isRightHandPrimary)) { if(controlMouseCursor && dragInProgress) { //MouseControl.MouseRelease(); dragInProgress = false; } lastLeftHandEvent = HandEventType.Release; lastRightHandEvent = HandEventType.Release; } if(controlMouseCursor && (handEvent != lastLeftHandEvent)) { if(controlMouseDrag && !dragInProgress && (handEvent == HandEventType.Grip)) { dragInProgress = true; //MouseControl.MouseDrag(); } else if(dragInProgress && (handEvent == HandEventType.Release)) { //MouseControl.MouseRelease(); dragInProgress = false; } } leftHandEvent = handEvent; if(handEvent != HandEventType.None) { lastLeftHandEvent = handEvent; } // if the hand is primary, set the cursor position if(isLeftHandInteracting) { isLeftHandPrimary = true; if((leftHandClickProgress < 0.8f) )//&& !isLeftHandPress) { cursorScreenPos = Vector3.Lerp(cursorScreenPos, leftHandScreenPos, smoothFactor * Time.deltaTime); } // else // { // leftHandScreenPos = cursorScreenPos; // } if(controlMouseCursor && !useHandCursor) { //MouseControl.MouseMove(cursorScreenPos, debugText); } } else { isLeftHandPrimary = false; } */ // process right hand handEvent = HandStateToEvent(rightHandState, lastRightHandEvent); if(controlMouseCursor && (handEvent != lastRightHandEvent)) { if(controlMouseDrag && !dragInProgress && (handEvent == HandEventType.Grip)) { dragInProgress = true; //MouseControl.MouseDrag(); } else if(dragInProgress && (handEvent == HandEventType.Release)) { //MouseControl.MouseRelease(); dragInProgress = false; } } rightHandEvent = handEvent; if(handEvent != HandEventType.None) { lastRightHandEvent = handEvent; } // if the hand is primary, set the cursor position if(isRightHandInteracting) { isRightHandPrimary = true; if((rightHandClickProgress < 0.8f) /**&& !isRightHandPress*/) { cursorScreenPos = Vector3.Lerp(cursorScreenPos, rightHandScreenPos, smoothFactor * Time.deltaTime); } // else // { // rightHandScreenPos = cursorScreenPos; // } if(controlMouseCursor && !useHandCursor) { //MouseControl.MouseMove(cursorScreenPos, debugText); } } else { isRightHandPrimary = false; } } else { leftHandState = KinectInterop.HandState.NotTracked; rightHandState = KinectInterop.HandState.NotTracked; isLeftHandPrimary = false; isRightHandPrimary = false; isLeftHandPress = false; isRightHandPress = false; leftHandEvent = HandEventType.None; rightHandEvent = HandEventType.None; lastLeftHandEvent = HandEventType.Release; lastRightHandEvent = HandEventType.Release; if(controlMouseCursor && dragInProgress) { //MouseControl.MouseRelease(); dragInProgress = false; } } } }
void Update() { KinectManager kinectManager = KinectManager.Instance; // update Kinect interaction if (kinectManager && kinectManager.IsInitialized()) { primaryUserID = kinectManager.GetUserIdByIndex(playerIndex); if (primaryUserID != 0) { HandEventType handEvent = HandEventType.None; // get the left hand state leftHandState = kinectManager.GetLeftHandState(primaryUserID); // check if the left hand is interacting isleftIboxValid = kinectManager.GetLeftHandInteractionBox(primaryUserID, ref leftIboxLeftBotBack, ref leftIboxRightTopFront, isleftIboxValid); //bool bLeftHandPrimaryNow = false; if (isleftIboxValid && //bLeftHandPrimaryNow && kinectManager.GetJointTrackingState(primaryUserID, (int)KinectInterop.JointType.HandLeft) != KinectInterop.TrackingState.NotTracked) { leftHandPos = kinectManager.GetJointPosition(primaryUserID, (int)KinectInterop.JointType.HandLeft); leftHandScreenPos.x = Mathf.Clamp01((leftHandPos.x - leftIboxLeftBotBack.x) / (leftIboxRightTopFront.x - leftIboxLeftBotBack.x)); leftHandScreenPos.y = Mathf.Clamp01((leftHandPos.y - leftIboxLeftBotBack.y) / (leftIboxRightTopFront.y - leftIboxLeftBotBack.y)); leftHandScreenPos.z = Mathf.Clamp01((leftIboxLeftBotBack.z - leftHandPos.z) / (leftIboxLeftBotBack.z - leftIboxRightTopFront.z)); bool wasLeftHandInteracting = isLeftHandInteracting; isLeftHandInteracting = (leftHandPos.x >= (leftIboxLeftBotBack.x - 1.0f)) && (leftHandPos.x <= (leftIboxRightTopFront.x + 0.5f)) && (leftHandPos.y >= (leftIboxLeftBotBack.y - 0.1f)) && (leftHandPos.y <= (leftIboxRightTopFront.y + 0.7f)) && (leftIboxLeftBotBack.z >= leftHandPos.z) && (leftIboxRightTopFront.z * 0.8f <= leftHandPos.z); //bLeftHandPrimaryNow = isLeftHandInteracting; if (!wasLeftHandInteracting && isLeftHandInteracting) { leftHandInteractingSince = Time.realtimeSinceStartup; } // check for left press isLeftHandPress = ((leftIboxRightTopFront.z - 0.1f) >= leftHandPos.z); // check for left hand click float fClickDist = (leftHandPos - lastLeftHandPos).magnitude; if (allowHandClicks && !dragInProgress && isLeftHandInteracting && (fClickDist < KinectInterop.Constants.ClickMaxDistance)) { if ((Time.realtimeSinceStartup - lastLeftHandTime) >= KinectInterop.Constants.ClickStayDuration) { if (!isLeftHandClick) { isLeftHandClick = true; leftHandClickProgress = 1f; if (controlMouseCursor) { MouseControl.MouseClick(); isLeftHandClick = false; leftHandClickProgress = 0f; lastLeftHandPos = Vector3.zero; lastLeftHandTime = Time.realtimeSinceStartup; } } } else { leftHandClickProgress = (Time.realtimeSinceStartup - lastLeftHandTime) / KinectInterop.Constants.ClickStayDuration; } } else { isLeftHandClick = false; leftHandClickProgress = 0f; lastLeftHandPos = leftHandPos; lastLeftHandTime = Time.realtimeSinceStartup; } } else { isLeftHandInteracting = false; isLeftHandPress = false; } // get the right hand state rightHandState = kinectManager.GetRightHandState(primaryUserID); // check if the right hand is interacting isRightIboxValid = kinectManager.GetRightHandInteractionBox(primaryUserID, ref rightIboxLeftBotBack, ref rightIboxRightTopFront, isRightIboxValid); //bool bRightHandPrimaryNow = false; if (isRightIboxValid && //bRightHandPrimaryNow && kinectManager.GetJointTrackingState(primaryUserID, (int)KinectInterop.JointType.HandRight) != KinectInterop.TrackingState.NotTracked) { rightHandPos = kinectManager.GetJointPosition(primaryUserID, (int)KinectInterop.JointType.HandRight); rightHandScreenPos.x = Mathf.Clamp01((rightHandPos.x - rightIboxLeftBotBack.x) / (rightIboxRightTopFront.x - rightIboxLeftBotBack.x)); rightHandScreenPos.y = Mathf.Clamp01((rightHandPos.y - rightIboxLeftBotBack.y) / (rightIboxRightTopFront.y - rightIboxLeftBotBack.y)); rightHandScreenPos.z = Mathf.Clamp01((rightIboxLeftBotBack.z - rightHandPos.z) / (rightIboxLeftBotBack.z - rightIboxRightTopFront.z)); bool wasRightHandInteracting = isRightHandInteracting; isRightHandInteracting = (rightHandPos.x >= (rightIboxLeftBotBack.x - 0.5f)) && (rightHandPos.x <= (rightIboxRightTopFront.x + 1.0f)) && (rightHandPos.y >= (rightIboxLeftBotBack.y - 0.1f)) && (rightHandPos.y <= (rightIboxRightTopFront.y + 0.7f)) && (rightIboxLeftBotBack.z >= rightHandPos.z) && (rightIboxRightTopFront.z * 0.8f <= rightHandPos.z); //bRightHandPrimaryNow = isRightHandInteracting; if (!wasRightHandInteracting && isRightHandInteracting) { rightHandInteractingSince = Time.realtimeSinceStartup; } if (isLeftHandInteracting && isRightHandInteracting) { if (rightHandInteractingSince <= leftHandInteractingSince) { isLeftHandInteracting = false; } else { isRightHandInteracting = false; } } // check for right press isRightHandPress = ((rightIboxRightTopFront.z - 0.1f) >= rightHandPos.z); // check for right hand click float fClickDist = (rightHandPos - lastRightHandPos).magnitude; if (allowHandClicks && !dragInProgress && isRightHandInteracting && (fClickDist < KinectInterop.Constants.ClickMaxDistance)) { if ((Time.realtimeSinceStartup - lastRightHandTime) >= KinectInterop.Constants.ClickStayDuration) { if (!isRightHandClick) { isRightHandClick = true; rightHandClickProgress = 1f; if (controlMouseCursor) { MouseControl.MouseClick(); isRightHandClick = false; rightHandClickProgress = 0f; lastRightHandPos = Vector3.zero; lastRightHandTime = Time.realtimeSinceStartup; } } } else { rightHandClickProgress = (Time.realtimeSinceStartup - lastRightHandTime) / KinectInterop.Constants.ClickStayDuration; } } else { isRightHandClick = false; rightHandClickProgress = 0f; lastRightHandPos = rightHandPos; lastRightHandTime = Time.realtimeSinceStartup; } } else { isRightHandInteracting = false; isRightHandPress = false; } // process left hand handEvent = HandStateToEvent(leftHandState, lastLeftHandEvent); if ((isLeftHandInteracting != isLeftHandPrimary) || (isRightHandInteracting != isRightHandPrimary)) { if (controlMouseCursor && dragInProgress) { MouseControl.MouseRelease(); dragInProgress = false; } lastLeftHandEvent = HandEventType.Release; lastRightHandEvent = HandEventType.Release; } if (controlMouseCursor && (handEvent != lastLeftHandEvent)) { if (controlMouseDrag && !dragInProgress && (handEvent == HandEventType.Grip)) { dragInProgress = true; MouseControl.MouseDrag(); } else if (dragInProgress && (handEvent == HandEventType.Release)) { MouseControl.MouseRelease(); dragInProgress = false; } } leftHandEvent = handEvent; if (handEvent != HandEventType.None) { lastLeftHandEvent = handEvent; } // if the hand is primary, set the cursor position if (isLeftHandInteracting) { isLeftHandPrimary = true; if ((leftHandClickProgress < 0.8f) /**&& !isLeftHandPress*/) { cursorScreenPos = Vector3.Lerp(cursorScreenPos, leftHandScreenPos, smoothFactor * Time.deltaTime); } // else // { // leftHandScreenPos = cursorScreenPos; // } if (controlMouseCursor && !useHandCursor) { MouseControl.MouseMove(cursorScreenPos, debugText); } } else { isLeftHandPrimary = false; } // process right hand handEvent = HandStateToEvent(rightHandState, lastRightHandEvent); if (controlMouseCursor && (handEvent != lastRightHandEvent)) { if (controlMouseDrag && !dragInProgress && (handEvent == HandEventType.Grip)) { dragInProgress = true; MouseControl.MouseDrag(); } else if (dragInProgress && (handEvent == HandEventType.Release)) { MouseControl.MouseRelease(); dragInProgress = false; } } rightHandEvent = handEvent; if (handEvent != HandEventType.None) { lastRightHandEvent = handEvent; } // if the hand is primary, set the cursor position if (isRightHandInteracting) { isRightHandPrimary = true; if ((rightHandClickProgress < 0.8f) /**&& !isRightHandPress*/) { cursorScreenPos = Vector3.Lerp(cursorScreenPos, rightHandScreenPos, smoothFactor * Time.deltaTime); } // else // { // rightHandScreenPos = cursorScreenPos; // } if (controlMouseCursor && !useHandCursor) { MouseControl.MouseMove(cursorScreenPos, debugText); } } else { isRightHandPrimary = false; } } else { leftHandState = KinectInterop.HandState.NotTracked; rightHandState = KinectInterop.HandState.NotTracked; isLeftHandPrimary = false; isRightHandPrimary = false; isLeftHandPress = false; isRightHandPress = false; leftHandEvent = HandEventType.None; rightHandEvent = HandEventType.None; lastLeftHandEvent = HandEventType.Release; lastRightHandEvent = HandEventType.Release; if (controlMouseCursor && dragInProgress) { MouseControl.MouseRelease(); dragInProgress = false; } } } }
/// <summary> /// initialization /// </summary> void Start() { // set the singleton instance instance = this; _km = KinectManager.Instance; primaryUserID = 0; //left init leftHandPos = Vector3.zero; leftHandOri = Quaternion.identity; lastLeftHandEvent = HandEventType.Release; leftHandEvent = HandEventType.None; leftHandState = KinectInterop.HandState.Unknown; //right init rightHandPos = Vector3.zero; rightHandOri = Quaternion.identity; lastRightHandEvent = HandEventType.Release; rightHandEvent = HandEventType.None; rightHandState = KinectInterop.HandState.Unknown; }
// converts hand state to hand event type private HandEventType HandStateToEvent(KinectInterop.HandState handState, HandEventType lastEventType) { switch(handState) { case KinectInterop.HandState.Open: return HandEventType.Release; case KinectInterop.HandState.Closed: case KinectInterop.HandState.Lasso: return HandEventType.Grip; case KinectInterop.HandState.Unknown: return lastEventType; } return HandEventType.None; }
public static string EncodeInteraction(int skeletonTrackingId, HandEventType handEventType, HandType handType, float x, float y, float pressExtent, bool isActive, bool isInteractive, bool isPressed, bool isTracked) { return string.Format(CultureInfo.InvariantCulture, "{0}|{1} {2} {3} {4} {5} {6} {7} {8} {9} {10}", InteractionFrameType, skeletonTrackingId, handEventType, handType, x, y, pressExtent, isActive, isInteractive, isPressed, isTracked); }
public HandEventArgs(HandEventType type, PositionEventArgs e) { this.EventType = type; this.PositionEventArgs = e; }
/// <summary> /// Gets the position, orientation and event data and handles which method is called. /// </summary> void FixedUpdate() { HandEventType new_event = _hdi.getHandEvent (isLeftHand); //hand_position_ is set to the position of the avatar-hands rather than the raw _hdi.getHandPosition return value. hand_position_ = gameObject.transform.position; hand_rotation_ = _hdi.getHandOrientation(isLeftHand); // State transitions from the last frame (current_event) to the next frame (new_event). if (current_event == HandEventType.Release) { if (new_event == HandEventType.Grip) { //From release to grip. OnGrab (); } else { //Hand remains open. Hover (); } } else { if (current_event == HandEventType.Grip){ if(new_event == HandEventType.Release){ //From grip to release. OnRelease(); }else { if(active_object_ != null){ //Hand remains closed if there is still an object. ContinueGrab(); } else { //Lost object in the hand. OnRelease(); } } } else { //If the current event is neither Grip nor Release OnRelease(); } } current_event = new_event; }
public static string EncodeInteraction(int skeletonTrackingId, HandEventType handEventType, HandType handType, float x, float y, float pressExtent, bool isActive, bool isInteractive, bool isPressed, bool isTracked) { return(string.Format(CultureInfo.InvariantCulture, "{0}|{1} {2} {3} {4} {5} {6} {7} {8} {9} {10}", InteractionFrameType, skeletonTrackingId, handEventType, handType, x, y, pressExtent, isActive, isInteractive, isPressed, isTracked)); }
/// <summary> /// Initialization. Sets up the interface and the other variables /// </summary> void Start() { //To add additional Interfaces, extend the cases and extend the HandDataType Enumeration (in this class) switch (handDataType) { case HandDataType.SmoothedStateData: _hdi = new SmoothedHandDataInterface (); break; case HandDataType.UnalteredData: _hdi = new UnalteredHandDataInterface (); break; default: _hdi = new UnalteredHandDataInterface (); break; } hand_position_ = Vector3.zero; current_event = HandEventType.Release; hand_rotation_ = Quaternion.identity; }
void Update() { KinectManager kinectManager = KinectManager.Instance; // update Kinect interaction if (kinectManager && kinectManager.IsInitialized()) { playerUserID = kinectManager.GetUserIdByIndex(playerIndex); if (playerUserID != 0) { lastUserID = playerUserID; HandEventType handEvent = HandEventType.None; // get the left hand state leftHandState = kinectManager.GetLeftHandState(playerUserID); // check if the left hand is interacting isleftIboxValid = kinectManager.GetLeftHandInteractionBox(playerUserID, ref leftIboxLeftBotBack, ref leftIboxRightTopFront, isleftIboxValid); //bool bLeftHandPrimaryNow = false; // was the left hand interacting till now bool wasLeftHandInteracting = isLeftHandInteracting; if (isleftIboxValid && //bLeftHandPrimaryNow && kinectManager.GetJointTrackingState(playerUserID, (int)KinectInterop.JointType.HandLeft) != KinectInterop.TrackingState.NotTracked) { leftHandPos = kinectManager.GetJointPosition(playerUserID, (int)KinectInterop.JointType.HandLeft); leftHandScreenPos.z = Mathf.Clamp01((leftIboxLeftBotBack.z - leftHandPos.z) / (leftIboxLeftBotBack.z - leftIboxRightTopFront.z)); if (!handOverlayCursor) { leftHandScreenPos.x = Mathf.Clamp01((leftHandPos.x - leftIboxLeftBotBack.x) / (leftIboxRightTopFront.x - leftIboxLeftBotBack.x)); leftHandScreenPos.y = Mathf.Clamp01((leftHandPos.y - leftIboxLeftBotBack.y) / (leftIboxRightTopFront.y - leftIboxLeftBotBack.y)); isLeftHandInteracting = (leftHandPos.x >= (leftIboxLeftBotBack.x - 1.0f)) && (leftHandPos.x <= (leftIboxRightTopFront.x + 0.5f)) && (leftHandPos.y >= (leftIboxLeftBotBack.y - 0.1f)) && (leftHandPos.y <= (leftIboxRightTopFront.y + 0.7f)) && (leftIboxLeftBotBack.z >= leftHandPos.z) && (leftIboxRightTopFront.z * 0.8f <= leftHandPos.z); } else { isLeftHandInteracting = GetHandOverlayScreenPos(kinectManager, (int)KinectInterop.JointType.HandLeft, ref leftHandScreenPos) && (leftHandPos.y >= (leftIboxLeftBotBack.y - 0.15f)) && (leftHandPos.y <= (leftIboxRightTopFront.y + 0.7f)) && (leftIboxLeftBotBack.z >= leftHandPos.z) && (leftIboxRightTopFront.z * 0.8f <= leftHandPos.z); } //bLeftHandPrimaryNow = isLeftHandInteracting; // start interacting? if (!wasLeftHandInteracting && isLeftHandInteracting) { leftHandInteractingSince = Time.realtimeSinceStartup; } // check for left press isLeftHandPress = leftHandScreenPos.z > 0.99f; // ((leftIboxRightTopFront.z - 0.1f) >= leftHandPos.z); leftHandPressProgress = (Time.realtimeSinceStartup - lastLeftHandPressTime) >= KinectInterop.Constants.ClickStayDuration && leftHandScreenPos.z >= 0.7f ? (leftHandScreenPos.z - 0.7f) / 0.3f : 0f; // check for left hand click if (!dragInProgress && isLeftHandInteracting && ((allowHandClicks && ((leftHandPos - lastLeftHandPos).magnitude < KinectInterop.Constants.ClickMaxDistance)) || (allowPushToClick && leftHandPressProgress > 0f))) { if ((allowHandClicks && (Time.realtimeSinceStartup - lastLeftHandClickTime) >= KinectInterop.Constants.ClickStayDuration) || (allowPushToClick && leftHandPressProgress > 0.99f && isLeftHandPress)) { if (!isLeftHandClick) { isLeftHandClick = true; cursorClickProgress = leftHandClickProgress = 1f; foreach (InteractionListenerInterface listener in interactionListeners) { if (listener.HandClickDetected(playerUserID, playerIndex, false, leftHandScreenPos)) { isLeftHandClick = false; cursorClickProgress = leftHandClickProgress = 0f; lastLeftHandPos = Vector3.zero; lastLeftHandClickTime = Time.realtimeSinceStartup; lastLeftHandPressTime = Time.realtimeSinceStartup; } } if (controlMouseCursor) { MouseControl.MouseClick(); isLeftHandClick = false; cursorClickProgress = leftHandClickProgress = 0f; lastLeftHandPos = Vector3.zero; lastLeftHandClickTime = Time.realtimeSinceStartup; lastLeftHandPressTime = Time.realtimeSinceStartup; } } } else { // show progress after the 1st half of the needed duration float leftHandTimeProgress = allowHandClicks && (Time.realtimeSinceStartup - lastLeftHandClickTime) >= (KinectInterop.Constants.ClickStayDuration / 2f) ? ((Time.realtimeSinceStartup - lastLeftHandClickTime - (KinectInterop.Constants.ClickStayDuration / 2f)) * 2f / KinectInterop.Constants.ClickStayDuration) : 0f; cursorClickProgress = leftHandClickProgress = allowPushToClick && leftHandScreenPos.z >= 0.7f ? leftHandPressProgress : leftHandTimeProgress; } } else { isLeftHandClick = false; leftHandClickProgress = 0f; lastLeftHandPos = leftHandPos; lastLeftHandClickTime = Time.realtimeSinceStartup; } } else { isLeftHandInteracting = false; isLeftHandPress = false; leftHandPressProgress = 0f; } // get the right hand state rightHandState = kinectManager.GetRightHandState(playerUserID); // check if the right hand is interacting isRightIboxValid = kinectManager.GetRightHandInteractionBox(playerUserID, ref rightIboxLeftBotBack, ref rightIboxRightTopFront, isRightIboxValid); //bool bRightHandPrimaryNow = false; // was the right hand interacting till now bool wasRightHandInteracting = isRightHandInteracting; if (isRightIboxValid && //bRightHandPrimaryNow && kinectManager.GetJointTrackingState(playerUserID, (int)KinectInterop.JointType.HandRight) != KinectInterop.TrackingState.NotTracked) { rightHandPos = kinectManager.GetJointPosition(playerUserID, (int)KinectInterop.JointType.HandRight); rightHandScreenPos.z = Mathf.Clamp01((rightIboxLeftBotBack.z - rightHandPos.z) / (rightIboxLeftBotBack.z - rightIboxRightTopFront.z)); if (!handOverlayCursor) { rightHandScreenPos.x = Mathf.Clamp01((rightHandPos.x - rightIboxLeftBotBack.x) / (rightIboxRightTopFront.x - rightIboxLeftBotBack.x)); rightHandScreenPos.y = Mathf.Clamp01((rightHandPos.y - rightIboxLeftBotBack.y) / (rightIboxRightTopFront.y - rightIboxLeftBotBack.y)); isRightHandInteracting = (rightHandPos.x >= (rightIboxLeftBotBack.x - 0.5f)) && (rightHandPos.x <= (rightIboxRightTopFront.x + 1.0f)) && (rightHandPos.y >= (rightIboxLeftBotBack.y - 0.1f)) && (rightHandPos.y <= (rightIboxRightTopFront.y + 0.7f)) && (rightIboxLeftBotBack.z >= rightHandPos.z) && (rightIboxRightTopFront.z * 0.8f <= rightHandPos.z); } else { isRightHandInteracting = GetHandOverlayScreenPos(kinectManager, (int)KinectInterop.JointType.HandRight, ref rightHandScreenPos) && (rightHandPos.y >= (rightIboxLeftBotBack.y - 0.15f)) && (rightHandPos.y <= (rightIboxRightTopFront.y + 0.7f)) && (rightIboxLeftBotBack.z >= rightHandPos.z) && (rightIboxRightTopFront.z * 0.8f <= rightHandPos.z); } //bRightHandPrimaryNow = isRightHandInteracting; if (!wasRightHandInteracting && isRightHandInteracting) { rightHandInteractingSince = Time.realtimeSinceStartup; } // check for right press isRightHandPress = rightHandScreenPos.z > 0.99f; // ((rightIboxRightTopFront.z - 0.1f) >= rightHandPos.z); rightHandPressProgress = (Time.realtimeSinceStartup - lastRightHandPressTime) >= KinectInterop.Constants.ClickStayDuration && rightHandScreenPos.z >= 0.7f ? (rightHandScreenPos.z - 0.7f) / 0.3f : 0f; // check for right hand click if (!dragInProgress && isRightHandInteracting && ((allowHandClicks && ((rightHandPos - lastRightHandPos).magnitude < KinectInterop.Constants.ClickMaxDistance)) || (allowPushToClick && rightHandPressProgress > 0f))) { if ((allowHandClicks && (Time.realtimeSinceStartup - lastRightHandClickTime) >= KinectInterop.Constants.ClickStayDuration) || (allowPushToClick && rightHandPressProgress > 0.99f && isRightHandPress)) { if (!isRightHandClick) { isRightHandClick = true; cursorClickProgress = rightHandClickProgress = 1f; foreach (InteractionListenerInterface listener in interactionListeners) { if (listener.HandClickDetected(playerUserID, playerIndex, true, rightHandScreenPos)) { isRightHandClick = false; cursorClickProgress = rightHandClickProgress = 0f; lastRightHandPos = Vector3.zero; lastRightHandClickTime = Time.realtimeSinceStartup; lastRightHandPressTime = Time.realtimeSinceStartup; } } if (controlMouseCursor) { MouseControl.MouseClick(); isRightHandClick = false; cursorClickProgress = rightHandClickProgress = 0f; lastRightHandPos = Vector3.zero; lastRightHandClickTime = Time.realtimeSinceStartup; lastRightHandPressTime = Time.realtimeSinceStartup; } } } else { // show progress after the 1st half of the needed duration float rightHandTimeProgress = allowHandClicks && (Time.realtimeSinceStartup - lastRightHandClickTime) >= (KinectInterop.Constants.ClickStayDuration / 2f) ? ((Time.realtimeSinceStartup - lastRightHandClickTime - (KinectInterop.Constants.ClickStayDuration / 2f)) * 2f / KinectInterop.Constants.ClickStayDuration) : 0f; cursorClickProgress = rightHandClickProgress = allowPushToClick && rightHandScreenPos.z >= 0.7f ? rightHandPressProgress : rightHandTimeProgress; } } else { isRightHandClick = false; rightHandClickProgress = 0f; lastRightHandPos = rightHandPos; lastRightHandClickTime = Time.realtimeSinceStartup; } } else { isRightHandInteracting = false; isRightHandPress = false; rightHandPressProgress = 0f; } // stop the cursor click progress, if both left and right hand are not clicking if (leftHandClickProgress == 0f && rightHandClickProgress == 0f && cursorClickProgress > 0f) { cursorClickProgress = 0f; } // if both hands are interacting, check which one interacts longer than the other if (isLeftHandInteracting && isRightHandInteracting) { if (rightHandInteractingSince <= leftHandInteractingSince) { isLeftHandInteracting = false; } else { isRightHandInteracting = false; } } // if left hand just stopped interacting, send extra non-interaction event if (wasLeftHandInteracting && !isLeftHandInteracting) { foreach (InteractionListenerInterface listener in interactionListeners) { if (lastLeftHandEvent == HandEventType.Grip) { listener.HandReleaseDetected(playerUserID, playerIndex, false, true, leftHandScreenPos); } } lastLeftHandEvent = HandEventType.Release; } // if right hand just stopped interacting, send extra non-interaction event if (wasRightHandInteracting && !isRightHandInteracting) { foreach (InteractionListenerInterface listener in interactionListeners) { if (lastRightHandEvent == HandEventType.Grip) { listener.HandReleaseDetected(playerUserID, playerIndex, true, true, rightHandScreenPos); } } lastRightHandEvent = HandEventType.Release; } // process left hand handEvent = HandStateToEvent(leftHandState, lastLeftHandEvent); if ((isLeftHandInteracting != isLeftHandPrimary) || (isRightHandInteracting != isRightHandPrimary)) { if (controlMouseCursor && dragInProgress) { MouseControl.MouseRelease(); dragInProgress = false; } lastLeftHandEvent = HandEventType.Release; lastRightHandEvent = HandEventType.Release; } if (controlMouseCursor && (handEvent != lastLeftHandEvent)) { if (controlMouseDrag && !dragInProgress && (handEvent == HandEventType.Grip)) { dragInProgress = true; MouseControl.MouseDrag(); } else if (dragInProgress && (handEvent == HandEventType.Release)) { MouseControl.MouseRelease(); dragInProgress = false; } } leftHandEvent = handEvent; if (handEvent != HandEventType.None) { // no clicks, while hand grip is detected if (leftHandEvent == HandEventType.Grip && leftHandClickProgress > 0f) { cursorClickProgress = leftHandClickProgress = 0f; lastLeftHandClickTime = Time.realtimeSinceStartup; } if (leftHandEvent != lastLeftHandEvent) { // invoke interaction listeners foreach (InteractionListenerInterface listener in interactionListeners) { if (leftHandEvent == HandEventType.Grip) { listener.HandGripDetected(playerUserID, playerIndex, false, isLeftHandInteracting, leftHandScreenPos); } else if (leftHandEvent == HandEventType.Release) { listener.HandReleaseDetected(playerUserID, playerIndex, false, isLeftHandInteracting, leftHandScreenPos); } } } lastLeftHandEvent = handEvent; } // if the hand is primary, set the cursor position if (isLeftHandInteracting) { isLeftHandPrimary = true; if (leftHandClickProgress < 0.8f) // stop the cursor after 80% click progress { float smooth = smoothFactor * Time.deltaTime; if (smooth == 0f) { smooth = 1f; } cursorScreenPos = Vector3.Lerp(cursorScreenPos, leftHandScreenPos, smooth); } // move mouse-only if there is no cursor texture if (controlMouseCursor && (!guiHandCursor || (!gripHandTexture && !releaseHandTexture && !normalHandTexture))) { MouseControl.MouseMove(cursorScreenPos, debugText); } } else { isLeftHandPrimary = false; } // process right hand handEvent = HandStateToEvent(rightHandState, lastRightHandEvent); if (controlMouseCursor && (handEvent != lastRightHandEvent)) { if (controlMouseDrag && !dragInProgress && (handEvent == HandEventType.Grip)) { dragInProgress = true; MouseControl.MouseDrag(); } else if (dragInProgress && (handEvent == HandEventType.Release)) { MouseControl.MouseRelease(); dragInProgress = false; } } rightHandEvent = handEvent; if (handEvent != HandEventType.None) { // no clicks, while hand grip is detected if (rightHandEvent == HandEventType.Grip && rightHandClickProgress > 0f) { cursorClickProgress = rightHandClickProgress = 0f; lastRightHandClickTime = Time.realtimeSinceStartup; } if (rightHandEvent != lastRightHandEvent) { // invoke interaction listeners foreach (InteractionListenerInterface listener in interactionListeners) { if (rightHandEvent == HandEventType.Grip) { listener.HandGripDetected(playerUserID, playerIndex, true, isRightHandInteracting, rightHandScreenPos); } else if (rightHandEvent == HandEventType.Release) { listener.HandReleaseDetected(playerUserID, playerIndex, true, isRightHandInteracting, rightHandScreenPos); } } } lastRightHandEvent = handEvent; } // if the hand is primary, set the cursor position if (isRightHandInteracting) { isRightHandPrimary = true; if (rightHandClickProgress < 0.8f) // stop the cursor after 80% click progress { float smooth = smoothFactor * Time.deltaTime; if (smooth == 0f) { smooth = 1f; } cursorScreenPos = Vector3.Lerp(cursorScreenPos, rightHandScreenPos, smooth); } // move mouse-only if there is no cursor texture if (controlMouseCursor && (!guiHandCursor || (!gripHandTexture && !releaseHandTexture && !normalHandTexture))) { MouseControl.MouseMove(cursorScreenPos, debugText); } } else { isRightHandPrimary = false; } } else { // send release events if (lastLeftHandEvent == HandEventType.Grip || lastRightHandEvent == HandEventType.Grip) { foreach (InteractionListenerInterface listener in interactionListeners) { if (lastLeftHandEvent == HandEventType.Grip) { listener.HandReleaseDetected(lastUserID, playerIndex, false, true, leftHandScreenPos); } if (lastRightHandEvent == HandEventType.Grip) { listener.HandReleaseDetected(lastUserID, playerIndex, true, true, leftHandScreenPos); } } } leftHandState = KinectInterop.HandState.NotTracked; rightHandState = KinectInterop.HandState.NotTracked; isLeftHandPrimary = isRightHandPrimary = false; isLeftHandInteracting = isRightHandInteracting = false; leftHandInteractingSince = rightHandInteractingSince = 0f; isLeftHandClick = isRightHandClick = false; cursorClickProgress = leftHandClickProgress = rightHandClickProgress = 0f; lastLeftHandClickTime = lastRightHandClickTime = Time.realtimeSinceStartup; lastLeftHandPressTime = lastRightHandPressTime = Time.realtimeSinceStartup; isLeftHandPress = false; isRightHandPress = false; leftHandPressProgress = 0f; rightHandPressProgress = 0f; leftHandEvent = HandEventType.None; rightHandEvent = HandEventType.None; lastLeftHandEvent = HandEventType.Release; lastRightHandEvent = HandEventType.Release; if (controlMouseCursor && dragInProgress) { MouseControl.MouseRelease(); dragInProgress = false; } } // update cursor texture and position UpdateGUI(); } }
public HandEventArgs(HandEventType type) { this.EventType = type; }