private void clickGameOver()
 {
     if (lifeCount > 0)
     {
         return;
     }
     if (kinectManager.IsUserDetected())
     {
         long userId    = kinectManager.GetPrimaryUserID();
         int  jointType = (int)KinectInterop.JointType.HandRight;
         if (kinectManager.IsJointTracked(userId, jointType))
         {
             Vector3 rHandPosition = kinectManager.GetJointKinectPosition(userId, jointType);
             rightTrail.position = rHandPosition;
             Vector3 screenPositionV3           = Camera.main.WorldToScreenPoint(rHandPosition);
             Vector2 screenPositionV2           = new Vector2(screenPositionV3.x, screenPositionV3.y);
             KinectInterop.HandState rHandState = kinectManager.GetRightHandState(userId);
             RectTransform           gameOverRt = gameOverImg.transform as RectTransform;
             if (rHandState == KinectInterop.HandState.Closed && RectTransformUtility.RectangleContainsScreenPoint(gameOverRt, screenPositionV2, Camera.main))
             {
                 Destroy(gameObject);
                 panelCenter.showStartPanel();
             }
         }
     }
 }
示例#2
0
    private void detectedClickGameOver()
    {
        if (gameOverImg.gameObject.activeSelf == false)
        {
            return;
        }
        if (KinectManager.Instance.IsUserDetected())
        {
            //检测到玩家

            long userId = KinectManager.Instance.GetPrimaryUserID();             //获取玩家id
            //Vector2 userPos = KinectManager.Instance.GetUserPosition(userId);//获取整个玩家相对于体感的坐标信息
            int jointLeftType = (int)KinectInterop.JointType.HandLeft;           //表示右手

            if (KinectManager.Instance.IsJointTracked(userId, jointLeftType))
            {
                //追踪到关节点
                Vector3 HandPos = KinectManager.Instance.GetJointKinectPosition(userId, jointLeftType); //获取右手信息
                leftTrail.position = HandPos;
                Vector3 HandScreenPos = Camera.main.WorldToScreenPoint(HandPos);                        //右手转换到屏幕坐标
                Vector2 HandScrPos    = new Vector2(HandScreenPos.x, HandScreenPos.y);                  //三维坐标转换到二维
                KinectInterop.HandState leftHandState = KinectManager.Instance.GetLeftHandState(userId);
                if (leftHandState == KinectInterop.HandState.Closed && RectTransformUtility.RectangleContainsScreenPoint(gameOverImg.transform as RectTransform, HandScrPos, Camera.main))
                {
                    Destroy(gameObject);
                    panelCenter.showFirstPanel();
                }
            }
        }
    }
示例#3
0
    /// <summary>
    /// 获取手势姿势
    /// </summary>
    private void GetHandGesture()
    {
        //判断是否有获取到用户ID
        if (manager.IsUserDetected())
        {
            //获取用户ID
            long userID = manager.GetPrimaryUserID();
            //获取骨骼节点类型
            int jointType = (int)KinectInterop.JointType.HandRight;
            //判断是否骨骼节点有捕获到
            if (manager.IsJointTracked(userID, jointType))
            {
                KinectInterop.HandState handState = manager.GetRightHandState(userID);
                switch (handState)
                {
                case KinectInterop.HandState.Open:
                    print("右手展开");
                    break;

                case KinectInterop.HandState.Closed:
                    print("右手握拳");
                    break;

                case KinectInterop.HandState.Lasso:
                    print("yes 手势");
                    break;
                }
            }
        }
    }
    public void ActionWithHand()
    {
        KinectInterop.JointType handJoint = KinectInterop.JointType.HandRight;
        KinectManager           manager   = KinectManager.Instance;

        if (manager && manager.IsInitialized())
        {
            if (manager.IsUserDetected())
            {
                long userId = manager.GetPrimaryUserID();
                if (manager.IsJointTracked(userId, (int)handJoint))
                {
                    KinectInterop.HandState handState = manager.GetRightHandState(userId);
                    if (handState == KinectInterop.HandState.Closed)
                    {
                        focus = true;
                    }
                    else if (handState == KinectInterop.HandState.Open)
                    {
                        focus = false;
                    }
                }
            }
        }
    }
    private void GetHandStateAndConf(uint handState, InteractionHandEventType handEvent,
                                     ref KinectInterop.HandState refHandState, ref KinectInterop.TrackingConfidence refHandConf)
    {
        bool bHandPrimary = (handState & (uint)NuiHandpointerState.PrimaryForUser) != 0;

        refHandConf = bHandPrimary ? KinectInterop.TrackingConfidence.High : KinectInterop.TrackingConfidence.Low;

        if (bHandPrimary)
        {
            switch (handEvent)
            {
            case InteractionHandEventType.Grip:
                refHandState = KinectInterop.HandState.Closed;
                break;

            case InteractionHandEventType.Release:
                //case InteractionHandEventType.None:
                refHandState = KinectInterop.HandState.Open;
                break;
            }
        }
        else
        {
            refHandState = KinectInterop.HandState.NotTracked;
        }
    }
示例#6
0
 /// <summary>
 /// 坐标转换
 /// </summary>
 private void UIInteraction()
 {
     if (IsUser())
     {
         long userID = KinectManager.Instance.GetPrimaryUserID();
         if (KinectManager.Instance.IsJointTracked(userID, (int)KinectInterop.JointType.HandRight))
         {
             //获取右手坐标
             Vector3 rightHandPos = KinectManager.Instance.GetJointKinectPosition(userID, (int)KinectInterop.JointType.HandRight);
             //Vector3 rightHandScreenPos = Camera.main.WorldToScreenPoint(rightHandPos);
             //把右手坐标转成屏幕坐标
             Vector2 rightHandScreenPos = RectTransformUtility.WorldToScreenPoint(Camera.main, rightHandPos);
             //转成Local屏幕坐标
             Vector2 rightHandLocalScreenPos;
             RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform, rightHandScreenPos, null, out rightHandLocalScreenPos);
             //把坐标位置赋值给Cursor
             cursor.transform.localPosition = rightHandLocalScreenPos;
             //var cursorRTF = cursor.transform as RectTransform;
             //cursorRTF.anchoredPosition = rightHandLocalScreenPos;
             if (RectTransformUtility.RectangleContainsScreenPoint(Button_NewGame as RectTransform, rightHandScreenPos, null))
             {
                 KinectInterop.HandState handState = KinectManager.Instance.GetRightHandState(userID);
                 if (handState == KinectInterop.HandState.Closed)
                 {
                     Debug.Log("进入游戏");
                 }
             }
         }
     }
 }
示例#7
0
    private void InteractonWithHand(long userID, Vector2 JointScreenPos)
    {
        KinectInterop.HandState handState = KinectManager.Instance.GetRightHandState(userID);
        if (handState == KinectInterop.HandState.Closed)
        {
            isHandClose   = true;
            Cursor.sprite = cursorSprites[1];
        }
        else
        {
            isHandClose   = false;
            Cursor.sprite = cursorSprites[0];
        }

        if (ContainCursor(mButton_NewGame, JointScreenPos) && isHandClose == true && mButton_Strawberry.gameObject != null)
        {
            StartCoroutine(IconDown(mButton_Strawberry));
        }
        else if (ContainCursor(mButton_Dojo, JointScreenPos) && isHandClose == true && mButton_Apple.gameObject != null)
        {
            StartCoroutine(IconDown(mButton_Apple));
        }
        else if (ContainCursor(mButton_Quit, JointScreenPos) && isHandClose == true && mbutton_Boom.gameObject != null)
        {
            Application.Quit();
        }
    }
    // Update is called once per frame
    void Update()
    {
        bool isInit = KinectManager.Instance.IsInitialized();

        if (isInit)
        {
            // Debug.Log("stream");
            if (kinectImg.texture == null)
            {
                Texture kinectPic = KinectManager.Instance.GetUsersLblTex();
                //Debug.Log("stream");
                //Texture kinectPic = KinectManager.Instance.GetUsersClrTex();
                kinectImg.texture = kinectPic;
            }
            if (KinectManager.Instance.IsUserDetected())
            {
                long UserId     = KinectManager.Instance.GetPrimaryUserID();
                int  jointTypel = (int)KinectInterop.JointType.HandLeft;
                // int jointTyper = (int)KinectInterop.JointType.HandRight;
                if (KinectManager.Instance.IsJointTracked(UserId, jointTypel))
                {
                    //print("left");
                    Vector3 leftHandPos = KinectManager.Instance.GetJointKinectPosition(UserId, jointTypel);
                    //print(leftHandPos);
                    //Vector3 leftHandPos = KinectManager.Instance.GetJointPosition(UserId,jointTypel);
                    // print("x:" + leftHandPos.x + ",,,,y:" + leftHandPos.y + ",,,,,z : " + leftHandPos.z);
                    // Vector3 leftHandscreenPostion = Camera.main.WorldToScreenPoint(leftHandPos);
                    // print("xxxxxxxx");
                    // Vector2 leftHandSenPos = new Vector2(leftHandscreenPostion.x, leftHandscreenPostion.y);
                    // Vector2 leftHandUguiPos;


                    // if (RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)canvas.transform, leftHandSenPos, null, out leftHandUguiPos))
                    // {
                    //  print("left");
                    // RectTransform liftRectTf = leftHand.transform as RectTransform;
                    // liftRectTf.anchoredPosition = leftHandUguiPos;
                    //  print("left" + leftHandUguiPos.x+"y"+leftHandUguiPos.y);
                    // }



                    KinectInterop.HandState rightHandStay = KinectManager.Instance.GetRightHandState(UserId);



                    if (rightHandStay == KinectInterop.HandState.Closed)
                    {
                        print("右手closed");
                        Debug.Log("leftclosed");
                    }
                    else if (rightHandStay == KinectInterop.HandState.Open)
                    {
                        print("rightHandOpened");
                    }
                }
            }
        }
    }
    private string getInfo(KinectInterop.HandState show, 手势 shoushi)
    {
        string info = string.Empty;

        switch (show)
        {
        case KinectInterop.HandState.Closed:
            info = "你出的是石头\n";
            break;

        case KinectInterop.HandState.Lasso:
            info = "你出的是剪刀\n";
            break;

        case KinectInterop.HandState.Open:
            info = "你出的是布\n";
            break;

        default:
            info = "请出招...\n";
            return(info);
        }

        switch (shoushi)
        {
        case 手势.石头:
            info += "电脑出的是石头\n";
            break;

        case 手势.剪刀:
            info += "电脑出的是剪刀\n";
            break;

        case 手势.布:
            info += "电脑出的是布\n";
            break;
        }
        int res = contrast(show, shoushi);

        if (res == 1)
        {
            info += "哈哈哈,你赢了\n";
        }
        else if (res == -1)
        {
            info += "哈哈哈,你输了\n";
        }
        else if (res == 0)
        {
            info += "哈哈哈,平手";
        }
        else
        {
            info += "你的手势未识别";
        }

        state = true;//识别完成
        return(info);
    }
示例#10
0
    public void ShowHandHelper(Vector3 worldPosition, KinectInterop.HandState leftHandState,
                               KinectInterop.HandState rightHandState, float canvasRadius = 98.5f)
    {
        GameObject handHelperObject = Utils.Spawn(HandHelperPrefab, HandHelperRoot);
        HandHelper handHelper       = handHelperObject.GetComponent <HandHelper>();

        handHelper.Init(worldPosition, leftHandState, rightHandState, canvasRadius);
    }
示例#11
0
 bool IsRightHandClosed(long userId)
 {
     KinectInterop.HandState rightHandState = KinectManager.Instance.GetRightHandState(userId);
     if (rightHandState == KinectInterop.HandState.Closed)
     {
         return(true);
     }
     return(false);
 }
示例#12
0
 bool IsLeftHandOpen()
 {
     KinectInterop.HandState leftHandState = KinectManager.Instance.GetLeftHandState(this.userId);
     if (leftHandState == KinectInterop.HandState.Open)
     {
         return(true);
     }
     return(false);
 }
示例#13
0
 private void GetBallBack()
 {
     if (KinectManager.Instance.IsInitialized() && KinectManager.Instance.IsUserDetected())
     {
         long userID = KinectManager.Instance.GetPrimaryUserID();
         KinectInterop.HandState handState = KinectManager.Instance.GetRightHandState(userID);
         if (handState == KinectInterop.HandState.Closed)
         {
             SetBallOnHand();
         }
     }
 }
示例#14
0
 private void isCutFruit(RectTransform curFruitRt, ref bool needDestroyFruit)
 {
     if (kinectManager.IsUserDetected())
     {
         //获取用户ID
         long userId    = kinectManager.GetPrimaryUserID();
         int  jointType = (int)KinectInterop.JointType.HandRight;
         if (kinectManager.IsJointTracked(userId, jointType))
         {
             // 获取用户相对Kinect的位置信息
             Vector3 rHandPosition = kinectManager.GetJointKinectPosition(userId, jointType);
             rightTrail.position = rHandPosition;
             Vector3 screenPositionV3           = Camera.main.WorldToScreenPoint(rHandPosition);       //右手位置信息转换成在屏幕上的三维坐标
             Vector2 screenPositionV2           = new Vector2(screenPositionV3.x, screenPositionV3.y); //转换成屏幕上的二维坐标
             KinectInterop.HandState rHandState = kinectManager.GetRightHandState(userId);
             if (rHandState == KinectInterop.HandState.Open && RectTransformUtility.RectangleContainsScreenPoint(curFruitRt, screenPositionV2, Camera.main))
             {
                 //右手切中水果了
                 needDestroyFruit = true;
                 if (curFruit.getType() != Constant.Type_Bomb)
                 {
                     splatterAudio.Play();
                 }
                 else
                 {
                     boomAudio.Play();
                 }
             }
         }
         jointType = (int)KinectInterop.JointType.HandLeft;
         if (kinectManager.IsJointTracked(userId, jointType))
         {
             Vector3 lHandPosition = kinectManager.GetJointKinectPosition(userId, jointType);
             leftTrail.position = lHandPosition;
             Vector3 screenPositionV3           = Camera.main.WorldToScreenPoint(lHandPosition);
             Vector2 screenPositionV2           = new Vector2(screenPositionV3.x, screenPositionV3.y);
             KinectInterop.HandState lHandState = kinectManager.GetLeftHandState(userId);
             if (lHandState == KinectInterop.HandState.Open && RectTransformUtility.RectangleContainsScreenPoint(curFruitRt, screenPositionV2, Camera.main))
             {
                 needDestroyFruit = true;
                 if (curFruit.getType() != Constant.Type_Bomb)
                 {
                     splatterAudio.Play();
                 }
                 else
                 {
                     boomAudio.Play();
                 }
             }
         }
     }
 }
示例#15
0
    private void GetHandState()
    {
        if (manager == null)
        {
            manager = KinectManager.Instance;
        }

        if (manager.IsUserDetected())
        {
            long userId = manager.GetUserIdByIndex(0);
            leftHandState = manager.GetLeftHandState(userId);
            rightHandState = manager.GetRightHandState(userId);
        }
    }
示例#16
0
    // 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);
    }
示例#17
0
    // Update is called once per frame
    void Update()
    {
        if (manager != null)
        {
            //设备准备好了  可以读取了
            if (kinectImg.texture == null)
            {
                Texture2D kinectPic = KinectManager.Instance.GetUsersClrTex(); //从设备获取彩色数据
                                                                               // Texture2D kinectPic = KinectManager.Instance.GetUsersLblTex();  //获取深度数据量
                kinectImg.texture = kinectPic;                                 //把彩色数据给控件显示
            }

            if (KinectManager.Instance.IsUserDetected())
            {
                //检测到玩家
                long userId = KinectManager.Instance.GetPrimaryUserID();          //获取用户id

                Vector3 userPos = KinectManager.Instance.GetUserPosition(userId); //获取用户离Kinect的距离信息
                print("x = " + userPos.x + " y = " + userPos.y + " z = " + userPos.z);


                int jointType = (int)KinectInterop.JointType.HandLeft;
                if (KinectManager.Instance.IsJointTracked(userId, jointType))
                {
                    //关节点被追踪到
                    Vector3 leftHandPos = KinectManager.Instance.GetJointKinectPosition(userId, jointType);
                    //Vector3 leftHandPos = KinectManager.Instance.GetJointPosition(userId, jointType);  //y轴输出不一样
                    // print("x = " + leftHandPos.x + " y = " + leftHandPos.y + " z = " + leftHandPos.z);

                    KinectInterop.HandState leftHandState = KinectManager.Instance.GetLeftHandState(userId); //获取左手姿势
                    if (leftHandState == KinectInterop.HandState.Closed)
                    {
                        print("左手握拳");
                    }
                    else if (leftHandState == KinectInterop.HandState.Open)
                    {
                        print("左手张开");
                    }
                    else if (leftHandState == KinectInterop.HandState.Lasso)
                    {
                        print("yes手势");
                    }
                }
            }
        }
    }
 // Update is called once per frame
 void Update()
 {
     if (kinectManager.IsUserDetected())
     {
         //获取用户ID
         long userId     = kinectManager.GetPrimaryUserID();
         int  rHandJoint = (int)KinectInterop.JointType.HandRight;
         if (kinectManager.IsJointTracked(userId, rHandJoint))
         {
             // 获取用户相对Kinect的位置信息
             Vector3 rHandPosition    = kinectManager.GetJointKinectPosition(userId, rHandJoint);
             Vector3 screenPositionV3 = Camera.main.WorldToScreenPoint(rHandPosition);       //右手位置信息转换成在屏幕上的三维坐标
             Vector2 screenPositionV2 = new Vector2(screenPositionV3.x, screenPositionV3.y); //转换成屏幕上的二维坐标
             Vector2 rHandLocalPosition;                                                     //UGUI坐标
             if (RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)canvas.transform, screenPositionV2, Camera.main, out rHandLocalPosition))
             {
                 // 将屏幕坐标转换成UGUI坐标
                 RectTransform rightRectTf = rHandImage.transform as RectTransform;
                 rightRectTf.anchoredPosition = rHandLocalPosition;     //将右手的UGUI坐标赋给rightHandImage的坐标
             }
             KinectInterop.HandState rHandState = kinectManager.GetRightHandState(userId);
             rHandImage.sprite = rHandStateSprites[0];
             if (rHandState == KinectInterop.HandState.Closed)
             {
                 rHandImage.sprite = rHandStateSprites[1];      //握拳状态
                 isRHandClose      = true;
                 if (circle1.enabled == true && RectTransformUtility.RectangleContainsScreenPoint(DojoButton.rectTransform, screenPositionV2, Camera.main))
                 {
                     //if true 表示手在Button上,也就是手在Button所表示的矩形范围内
                     clickFruit(DojoButton);
                 }
                 else if (circle2.enabled == true && RectTransformUtility.RectangleContainsScreenPoint(NewGameButton.rectTransform, screenPositionV2, Camera.main))
                 {
                     clickFruit(NewGameButton);
                 }
                 else if (circle3.enabled == true && RectTransformUtility.RectangleContainsScreenPoint(quitButton.rectTransform, screenPositionV2, Camera.main))
                 {
                     clickFruit(quitButton);
                 }
             }
         }
     }
     detectCurBtn();
 }
 void UpdateHandState()
 {
     if (kinectManager)
     {
         rightHandState = kinectManager.GetRightHandState(UserID);
         if (UserID != kinectManager.GetUserIdByIndex(playerIndex))
         {
             UserID = kinectManager.GetUserIdByIndex(playerIndex);
             Debug.LogError(UserID);
         }
         //Debug.LogError("hand state updated: ");
         //Debug.LogError(rightHandState);
     }
     else
     {
         kinectManager = KinectManager.Instance;
         Debug.LogError("kinect manager found");
     }
 }
示例#20
0
    private void GetHandState()
    {
        if (manager == null)
        {
            manager = KinectManager.Instance;
        }

        if (manager.IsUserDetected())
        {
            long userId = manager.GetUserIdByIndex(0);
            leftHandState  = manager.GetLeftHandState(userId);
            rightHandState = manager.GetRightHandState(userId);
            if (leftHandState == KinectInterop.HandState.NotTracked)
            {
                if (hands[0].activeSelf)
                {
                    hands[0].SetActive(false);
                }
            }
            else
            {
                if (!hands[0].activeSelf)
                {
                    hands[0].SetActive(true);
                }
            }
            if (rightHandState == KinectInterop.HandState.NotTracked)
            {
                if (hands[1].activeSelf)
                {
                    hands[1].SetActive(false);
                }
            }
            else
            {
                if (!hands[1].activeSelf)
                {
                    hands[1].SetActive(true);
                }
            }
        }
    }
示例#21
0
    public void Init(Vector3 worldPosition, KinectInterop.HandState leftHandState,
                     KinectInterop.HandState rightHandState, float canvasRadius = 98.5f)
    {
        _rectTransform = GetComponent <RectTransform>();

        Vector3 screenPosition = Camera.main.WorldToScreenPoint(worldPosition);
        Vector3 canvasPosition = Utility.ConvertScreenPositionToCanvasPosition(KinectInputModule.Instance.TargetCanvas,
                                                                               screenPosition);

        _rectTransform.anchoredPosition = canvasPosition;

        LeftImage.rectTransform.anchoredPosition  = new Vector2(-canvasRadius, 0);
        RightImage.rectTransform.anchoredPosition = new Vector2(canvasRadius, 0);

        if (leftHandState == KinectInterop.HandState.Closed)
        {
            LeftImage.texture = LeftHandClose;
        }
        else
        {
            LeftImage.texture = LeftHandOpen;
        }

        if (rightHandState == KinectInterop.HandState.Closed)
        {
            RightImage.texture = RightHandClose;
        }
        else
        {
            RightImage.texture = RightHandOpen;
        }

        LeftImage.SetNativeSize();
        RightImage.SetNativeSize();

        LeftImage.color  = FromColor;
        RightImage.color = FromColor;

        LeanTween.color(LeftImage.rectTransform, ToColor, TweenTime).setLoopPingPong();
        LeanTween.color(RightImage.rectTransform, ToColor, TweenTime).setLoopPingPong();
    }
    //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);
    }
示例#23
0
    void Update()
    {
        if (manager == null)
        {
            manager = KinectManager.Instance;
        }

        if (manager && manager.IsInitialized() && foregroundCamera)
        {
            if (backgroundImage && (backgroundImage.texture == null))
            {
                backgroundImage.texture = manager.GetUsersClrTex();
            }


            Rect backgroundRect             = foregroundCamera.pixelRect;
            PortraitBackground portraitBack = PortraitBackground.Instance;

            if (portraitBack && portraitBack.enabled)
            {
                backgroundRect = portraitBack.GetBackgroundRect();
            }

            // overlay the joints
            if (manager.IsUserDetected())
            {
                long userId = manager.GetUserIdByIndex(playerIndex);

                OverlayJoint(userId, (int)KinectInterop.JointType.HandLeft, leftHandOverlay, backgroundRect);
                OverlayJoint(userId, (int)KinectInterop.JointType.HandRight, rightHandOverlay, backgroundRect);
                OverlayJoint(userId, (int)KinectInterop.JointType.SpineMid, neck, backgroundRect);
                OverlayJoint(userId, (int)KinectInterop.JointType.Head, head, backgroundRect);
                leftHandState  = manager.GetLeftHandState(userId);
                RightHandState = manager.GetRightHandState(userId);

                long userId1 = manager.GetUserIdByIndex(playerIndex1);
            }
        }
    }
示例#24
0
 /// <summary>
 /// 切中水果
 /// </summary>
 private void CutFruit(KinectInterop.JointType joint, GameObject trail)
 {
     if (UIManager.instance.IsUser())
     {
         long userID = KinectManager.Instance.GetPrimaryUserID();
         if (KinectManager.Instance.IsJointTracked(userID, (int)joint))
         {
             //获取关节坐标
             Vector3 jointPos = KinectManager.Instance.GetJointKinectPosition(userID, (int)joint);
             //拖尾效果
             trail.transform.position = jointPos;
             //把关节坐标转成屏幕坐标
             KinectInterop.HandState handState = 0;
             if (joint == KinectInterop.JointType.HandLeft)
             {
                 handState = KinectManager.Instance.GetLeftHandState(userID);
             }
             else if (joint == KinectInterop.JointType.HandRight)
             {
                 handState = KinectManager.Instance.GetRightHandState(userID);
             }
             Vector2 jointSenPos = RectTransformUtility.WorldToScreenPoint(Camera.main, jointPos);
             if (fruitGo == null)
             {
                 return;
             }
             if (UIManager.instance.ContainCursor(fruitGo.transform, jointSenPos) && handState == KinectInterop.HandState.Open)
             {
                 CutFruitScoreUpdate();
                 UIManager.instance.UpdateLifeUI(life);
                 IsGameOver();
                 Destroy(fruitGo);
                 CreateCutFruits();
                 CreateFruit();
             }
         }
     }
 }
示例#25
0
    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();
        }
    }
示例#26
0
        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;
                }
            }
        }
    }
示例#28
0
    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;
                }
            }
        }
    }
示例#29
0
    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;
                }
            }
        }
    }
示例#30
0
        /// <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;
        }
    /// <summary>
    /// Updates the avatar each frame.
    /// </summary>
    /// <param name="UserID">User ID</param>
    public void UpdateAvatar(Int64 UserID)
    {
        PlayerDetected = true;
        if (!gameObject.activeInHierarchy)
        {
            return;
        }

        // Get the KinectManager instance
        if (kinectManager == null)
        {
            kinectManager    = KinectManager.Instance;
            ColorImageWidth  = kinectManager.GetColorImageWidth();
            ColorImageHeight = kinectManager.GetColorImageHeight();
        }
        // for rendering the head
        //****************************************
        //
        if (rawImage)
        {
            rawImage.texture = kinectManager.GetUsersClrTex();
            int HeadIndex = 3;// (int)TrackedHead;


            if (kinectManager.IsJointTracked(UserID, HeadIndex))
            {
                Vector3 posJoint = kinectManager.GetRawSkeletonJointPos(UserID, HeadIndex);

                if (posJoint != Vector3.zero)
                {
                    // 3d position to depth
                    Vector2 posDepth = kinectManager.GetDepthMapPosForJointPos(posJoint);

                    // depth pos to color pos
                    Vector2 posColor = kinectManager.GetColorMapPosForDepthPos(posDepth);

                    float UVrecPosX = (float)posColor.x / ColorImageWidth;
                    float UVrecPosY = (float)posColor.y / ColorImageHeight;
                    UVrecPosX -= HeadImageWidth / 2.5f;
                    UVrecPosY -= HeadImageHeight / 2f;

                    //						Vector3 localPos = new Vector3(scaleX * 10f - 5f, 0f, scaleY * 10f - 5f); // 5f is 1/2 of 10f - size of the plane
                    //						Vector3 vPosOverlay = backgroundImage.transform.TransformPoint(localPos);
                    //Vector3 vPosOverlay = BottomLeft + ((vRight * scaleX) + (vUp * scaleY));

                    rawImage.uvRect = new Rect(Mathf.Lerp(rawImage.uvRect.x, UVrecPosX, smoothFactor * Time.deltaTime), Mathf.Lerp(rawImage.uvRect.y, UVrecPosY, smoothFactor * Time.deltaTime), HeadImageHeight, HeadImageWidth);
                    //	Vector3 vPosOverlay = Camera.main.ViewportToWorldPoint(new Vector3(scaleX, scaleY, distanceToCamera));
                    //	OverlayObject.transform.position = Vector3.Lerp(OverlayObject.transform.position, vPosOverlay, smoothFactor * Time.deltaTime);
                }
            }
        }
        //****************************		// move the avatar to its Kinect position
        if (!externalRootMotion)
        {
            MoveAvatar(UserID);
        }

        // get the left hand state and event
        if (kinectManager && kinectManager.GetJointTrackingState(UserID, (int)KinectInterop.JointType.HandLeft) != KinectInterop.TrackingState.NotTracked)
        {
            KinectInterop.HandState          leftHandState = kinectManager.GetLeftHandState(UserID);
            InteractionManager.HandEventType leftHandEvent = InteractionManager.HandStateToEvent(leftHandState, lastLeftHandEvent);

            if (leftHandEvent != InteractionManager.HandEventType.None)
            {
                lastLeftHandEvent = leftHandEvent;
            }
        }

        // get the right hand state and event
        if (kinectManager && kinectManager.GetJointTrackingState(UserID, (int)KinectInterop.JointType.HandRight) != KinectInterop.TrackingState.NotTracked)
        {
            KinectInterop.HandState          rightHandState = kinectManager.GetRightHandState(UserID);
            InteractionManager.HandEventType rightHandEvent = InteractionManager.HandStateToEvent(rightHandState, lastRightHandEvent);

            if (rightHandEvent != InteractionManager.HandEventType.None)
            {
                lastRightHandEvent = rightHandEvent;
            }
        }

        // rotate the avatar bones
        for (var boneIndex = 0; boneIndex < bones.Length; boneIndex++)
        {
            if (!bones[boneIndex])
            {
                continue;
            }

            if (boneIndex2JointMap.ContainsKey(boneIndex))
            {
                KinectInterop.JointType joint = !mirroredMovement ? boneIndex2JointMap[boneIndex] : boneIndex2MirrorJointMap[boneIndex];
                TransformBone(UserID, joint, boneIndex, !mirroredMovement);
            }
            else if (specIndex2JointMap.ContainsKey(boneIndex))
            {
                // special bones (clavicles)
                List <KinectInterop.JointType> alJoints = !mirroredMovement ? specIndex2JointMap[boneIndex] : specIndex2MirrorMap[boneIndex];

                if (alJoints.Count >= 2)
                {
                    //Debug.Log(alJoints[0].ToString());
                    Vector3 baseDir = alJoints[0].ToString().EndsWith("Left") ? Vector3.left : Vector3.right;
                    TransformSpecialBone(UserID, alJoints[0], alJoints[1], boneIndex, baseDir, !mirroredMovement);
                }
            }
        }
    }
示例#32
0
    /// <summary>
    /// Updates the avatar each frame.
    /// </summary>
    /// <param name="UserID">User ID</param>
    public void UpdateAvatar(Int64 UserID)
    {
        if (!gameObject.activeInHierarchy)
        {
            return;
        }

        // Get the KinectManager instance
        if (kinectManager == null)
        {
            kinectManager = KinectManager.Instance;
        }

        // move the avatar to its Kinect position
        if (!externalRootMotion)
        {
            MoveAvatar(UserID);
        }

        // get the left hand state and event
        if (kinectManager && kinectManager.GetJointTrackingState(UserID, (int)KinectInterop.JointType.HandLeft) != KinectInterop.TrackingState.NotTracked)
        {
            KinectInterop.HandState          leftHandState = kinectManager.GetLeftHandState(UserID);
            InteractionManager.HandEventType leftHandEvent = InteractionManager.HandStateToEvent(leftHandState, lastLeftHandEvent);

            if (leftHandEvent != InteractionManager.HandEventType.None)
            {
                lastLeftHandEvent = leftHandEvent;
            }
        }

        // get the right hand state and event
        if (kinectManager && kinectManager.GetJointTrackingState(UserID, (int)KinectInterop.JointType.HandRight) != KinectInterop.TrackingState.NotTracked)
        {
            KinectInterop.HandState          rightHandState = kinectManager.GetRightHandState(UserID);
            InteractionManager.HandEventType rightHandEvent = InteractionManager.HandStateToEvent(rightHandState, lastRightHandEvent);

            if (rightHandEvent != InteractionManager.HandEventType.None)
            {
                lastRightHandEvent = rightHandEvent;
            }
        }

        // rotate the avatar bones
        for (var boneIndex = 0; boneIndex < bones.Length; boneIndex++)
        {
            if (!bones[boneIndex])
            {
                continue;
            }

            if (boneIndex2JointMap.ContainsKey(boneIndex))
            {
                KinectInterop.JointType joint = !mirroredMovement ? boneIndex2JointMap[boneIndex] : boneIndex2MirrorJointMap[boneIndex];
                TransformBone(UserID, joint, boneIndex, !mirroredMovement);
            }
            else if (specIndex2JointMap.ContainsKey(boneIndex))
            {
                // special bones (clavicles)
                List <KinectInterop.JointType> alJoints = !mirroredMovement ? specIndex2JointMap[boneIndex] : specIndex2MirrorMap[boneIndex];

                if (alJoints.Count >= 2)
                {
                    //Debug.Log(alJoints[0].ToString());
                    Vector3 baseDir = alJoints[0].ToString().EndsWith("Left") ? Vector3.left : Vector3.right;
                    TransformSpecialBone(UserID, alJoints[0], alJoints[1], boneIndex, baseDir, !mirroredMovement);
                }
            }
        }

        //update relative position info
        updatePositionInfo_(UserID);
    }
    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;
                }
            }
        }
    }