예제 #1
0
    protected GraspState GetNewGraspState()
    {
        HandModel handModel = this.GetComponent <HandModel>();
        Hand      leapHand  = handModel.GetLeapHand();

        // check destruction
        if (this.CurrentGraspState == GraspState.GRASPED && this.ActiveObject != null)
        {
            // TODO: implement some kind of check for the overall grip aperture, if it falls
            // below a certain threshold, the object should be destroyed and the returned
            // state should be 'RELEASED'. Please stick to the initiateDestructionByGrabbing
            // method from the ContainerController. To search within the scene you can
            // use for instance the FindGameObjectWithTag method.
            Vector3 centroid = leapHand.Fingers[1].TipPosition.ToUnityScaled() * .25f + leapHand.Fingers[2].TipPosition.ToUnityScaled() * .25f + leapHand.Fingers[3].TipPosition.ToUnityScaled() * .25f + leapHand.Fingers[4].TipPosition.ToUnityScaled() * .25f;
            float   distance = Vector3.Distance(leapHand.PalmPosition.ToUnityScaled(), centroid);
            if (distance <= GraspController.MIN_FINGER_TO_PALM_DISTANCE)
            {
                ContainerController containerController = GameObject.FindGameObjectWithTag("Container").GetComponent <ContainerController>();
                containerController.initiateDestructionByGrabbing(this.ActiveObject.gameObject);
                return(GraspState.RELEASED);
            }
        }

        // power grasp conditions:
        // - grabstrength > .5 -> difficult for rotated hands otherwise
        if (leapHand.GrabStrength > .35f)
        {
            return(GraspState.GRASPED);
        }

        // Scale trigger distance by thumb proximal bone length.
        float proximalLength = leapHand.Fingers[0].Bone(Bone.BoneType.TYPE_PROXIMAL).Length;

        if (this.CurrentGraspState == GraspState.GRASPED && this.ActiveObject != null)
        {
            //return GraspState.GRASPED;
            // check if fingers point away from palm
            Vector3 axis             = handModel.GetPalmDirection();
            int     stretchedFingers = 0;
            for (int i = 1; i < 5; i++)
            {
                Vector3 fingerAxis = handModel.fingers[i].GetRay().direction;
                float   angle      = Vector3.Angle(axis, fingerAxis);
                //UnityEngine.Debug.Log(i + ": " + angle);
                if (angle > 0 && angle < 40)
                {
                    stretchedFingers++;
                }
            }

            if (stretchedFingers >= 3)
            {
                return(GraspState.RELEASED);
            }
            else
            {
                return(GraspState.GRASPED);
            }
        }

        return(GraspState.RELEASED);
    }