LEAPMotion VR 各种手势的判断~


LEAPMotion VR 各种手势的判断~

Created by miccall (转载请注明出处 miccall.tech)

1.手划向左边

        protected bool isMoveLeft (Hand hand)   /*手划向左边*/
        {
           /*  x轴移动的速度   deltaVelocity = 0.7f    isStationary (hand)  判断hand是否禁止 */
            return hand.PalmVelocity.x < -deltaVelocity && !isStationary (hand);
        }  

                [Tooltip ("Velocity (m/s) move toward ")]
                protected float deltaVelocity = 0.7f;

2.手划向右边

        protected bool isMoveRight (Hand hand) /*手划向右边*/
        {
            return hand.PalmVelocity.x > deltaVelocity && !isStationary (hand);
        }  

        [Tooltip ("Velocity (m/s) move toward ")]
        protected float deltaVelocity = 0.7f;

3.手划向上边

        protected bool isMoveUp (Hand hand)   /*手向上 */
        {
            return hand.PalmVelocity.y > deltaVelocity && !isStationary (hand);
        }

4.手划向下边

        protected bool isMoveDown (Hand hand) /*手向下*/  
        {
            return hand.PalmVelocity.y < -deltaVelocity && !isStationary (hand);
        }

5.手固定不动

        protected bool isStationary (Hand hand)/* 固定不动的*/ 
        {
            return hand.PalmVelocity.Magnitude < smallestVelocity;
        }  
        [Tooltip ("Velocity (m/s) move toward ")]                   //速度(m/s)走向 
        protected float smallestVelocity = 0.4f;

6.是否抓取

        protected bool isGrabHand (Hand hand)  /*是否抓取*/
        {
            return hand.GrabStrength > 0.8f;    /*抓取力*/ 
        }

7.是否握拳

        protected bool isCloseHand (Hand hand)     /*是否握拳*/ 
        {
            List listOfFingers = hand.Fingers;
            int count = 0;
            for (int f = 0; f < listOfFingers.Count; f++) { /*循环遍历所有的手*/
                Finger finger = listOfFingers [f];    
                if ((finger.TipPosition - hand.PalmPosition).Magnitude < deltaCloseFinger)    /* Magnitude  向量的长度 。是(x*x+y*y+z*z)的平方根。*/
                     /*float deltaCloseFinger = 0.05f;*/
                {  
                    count++;
                                    //if (finger.Type == Finger.FingerType.TYPE_THUMB)
                                   //Debug.Log ((finger.TipPosition - hand.PalmPosition).Magnitude);
                }
            }
            return (count == 5);
        }

8.手掌是否全张开

        protected bool isOpenFullHand (Hand hand)         /*手掌全展开~*/
        {
            //Debug.Log (hand.GrabStrength + " " + hand.PalmVelocity + " " + hand.PalmVelocity.Magnitude);
            return hand.GrabStrength == 0;
        }

9.向量的判断

(1)向量转化成 角度

        protected float angle2LeapVectors (Leap.Vector a, Leap.Vector  b)
        {
                   //向量转化成 角度
                    return Vector3.Angle (UnityVectorExtension.ToVector3 (a), UnityVectorExtension.ToVector3 (b));
        }

(2)判断两个向量是否 相同 方向

        protected bool isSameDirection (Vector a, Vector b)
        {
                    //判断两个向量是否 相同 方向
                    //Debug.Log (angle2LeapVectors (a, b) + " " + b);
                    return angle2LeapVectors (a, b) < handForwardDegree;
        }

(3)判断两个向量是否 相反 方向

        protected bool isOppositeDirection (Vector a, Vector b      
        {
        //判断两个向量是否 相反 方向
                return angle2LeapVectors (a, b) > (180 - handForwardDegree);
        }

(4)判断手的掌心方向于一个 向量 是否方向相同

        protected bool isPalmNormalSameDirectionWith (Hand hand, Vector3 dir)
        {
              //判断手的掌心方向于一个  向量   是否方向相同 
            return isSameDirection (hand.PalmNormal, UnityVectorExtension.ToVector (dir));
        }  
            [Tooltip ("Delta degree to check 2 vectors same direction")]  //三角度检查2个向量的方向相同
                protected float handForwardDegree = 30;

10.向手掌的方向 移动

        protected bool isHandMoveForward (Hand hand)
        {
            return isSameDirection (hand.PalmNormal, hand.PalmVelocity) && !isStationary (hand);
        }

11.手掌是否垂直(掌心水平)

        protected bool checkPalmNormalInXZPlane (Hand hand)  //   hand.PalmNormal 垂直于掌心的向量 
        {
            float anglePalmNormal = angle2LeapVectors (hand.PalmNormal, UnityVectorExtension.ToVector (Vector3.up));
            return (anglePalmNormal > 70 && anglePalmNormal < 110);
        }

12.判断大拇指是否竖直或向下

         protected bool isThumbDirection (Hand hand, Vector3 dir)
        {
            List listOfFingers = hand.Fingers;
            for (int f = 0; f < listOfFingers.Count; f++) {
                Finger finger = listOfFingers [f];
                if (finger.Type == Finger.FingerType.TYPE_THUMB) {
                    float angleThumbFinger = angle2LeapVectors (finger.Direction, 
                                                UnityVectorExtension.ToVector (dir));
                    float angleThumbFinger2 = angle2LeapVectors (
                                                 finger.StabilizedTipPosition - hand.PalmPosition, UnityVectorExtension.ToVector (dir));
                    //Debug.Log (angleThumbFinger + " " + angleThumbFinger2);
                    if (angleThumbFinger < deltaAngleThumb
                       || angleThumbFinger2 < deltaAngleThumb)
                        return true;
                    else
                        return false;
                } 
            }
            return false;
        }

13.判断四指是否靠拢掌心

        protected bool checkFingerCloseToHand (Hand hand)
        {
                    List listOfFingers = hand.Fingers;
                    int count = 0;
                    for (int f = 0; f < listOfFingers.Count; f++) {
                        Finger finger = listOfFingers [f];
                        if ((finger.TipPosition - hand.PalmPosition).Magnitude < deltaCloseFinger) {
                            if (finger.Type == Finger.FingerType.TYPE_THUMB) {
                                return false;
                            } else {
                                count++;
                            }
                        }
                    }
                    //Debug.Log (count);
                    return (count == 4);
        }