LEAPMotion VR 各种手势的判断~
Created by miccall (转载请注明出处)
1.手划向左边
protected bool isMoveLeft (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 ")]
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)
{
count++;
}
}
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)
{
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")]
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));
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++;
}
}
}
return (count == 4);
}