GetLeapHand() public méthode

public GetLeapHand ( ) : Hand,
Résultat Hand,
    protected override void UpdateTracker()
    {
        previousPosition = Position;
        previousOrientation = Orientation;
        previousGrabbingStrength = currentGrabbingStrength;

        //get the rightmost hand in the frame
        if (handController.GetAllGraphicsHands().Length != 0)
        {
            handModel = handController.GetAllGraphicsHands()[0];
            handModel.transform.GetComponentInChildren<SkinnedMeshRenderer>().enabled = visibleHand;
            hand = handModel.GetLeapHand();
            currentGrabbingStrength = lowPassFilter(hand.GrabStrength, previousGrabbingStrength);

            Position = lowPassFilter(handModel.GetPalmPosition(), previousPosition);
            Orientation = lowPassFilter(handModel.GetPalmDirection(), previousOrientation);
        }

        //mask/display the graphical hand on key down
        if (Input.GetKeyDown(visibleHandKey))
        {
            var smr = handModel.transform.GetComponentInChildren<SkinnedMeshRenderer>();
            visibleHand = !visibleHand;
        }

        Translation = Position - previousPosition;
        Rotation = previousOrientation - Orientation;
    }
	void ReleaseFireBall(HandModel hand){


		if (currentFireBall && IsCastingStarted && !HandRecog.IsHandClenchingNonStrict (hand, clenchingAngle) && hand.GetLeapHand().Confidence > confidenceLevel) {

			currentFireBall.Release (hand.GetPalmDirection (), 5.0f);
			IsCastingStarted = false;
			currentFireBall = null;

			spellControl.ReleaseCastingControl ();
		}


	}
	void CastFireBall(HandModel hand){
		
		if (!IsCastingStarted && hand.GetLeapHand().Confidence >= confidenceLevel && !spellControl.SpellCasting()) {

			spellControl.SpellCasting ();

			IsCastingStarted = true;

			currentFireBall = GameObject.Instantiate (fireBallPrefab, GetFireBallSpawnPosition (hand, fireBallSpawnLocation), hand.gameObject.transform.rotation) as FireBall;

			//currentFireBall.transform.SetParent (handController.transform);
		}
			
	}
Exemple #4
0
 void OnTriggerStay(Collider coll)
 {
     if ((PartFilter.value & 1 << coll.gameObject.layer) == 1 << coll.gameObject.layer)
     {
         HandModel hm = coll.transform.gameObject.GetComponentInParent <HandModel> ();
         if (hm)
         {
             int id = hm.GetLeapHand().Id;
             if (!HandEnteredTimes.ContainsKey(id))
             {
                 HandEnteredTimes [id] = Time.time;
                 EmitEnter(id);
             }
             LastTouchTimes [id] = Time.time;
         }
     }
 }
Exemple #5
0
    protected void StartPinch(Vector3 pinch_position)
    {
        HandModel hand_model = GetComponent <HandModel>();

        if (active_object_ != null)
        {
            IgnoreCollisions(active_object_.gameObject, true);

            palm_rotation_      = hand_model.GetPalmRotation();
            rotation_from_palm_ = Quaternion.Inverse(palm_rotation_) * active_object_.transform.rotation;
            current_pinch_      = active_object_.transform.position;

            GrabbableObject grabbable = active_object_.GetComponent <GrabbableObject>();
            if (grabbable == null || grabbable.rotateQuickly)
            {
                last_max_angular_velocity_ = active_object_.rigidbody.maxAngularVelocity;
                active_object_.rigidbody.maxAngularVelocity = Mathf.Infinity;
            }

            if (grabbable != null)
            {
                grabbable.OnGrab();

                if (grabbable.preferredOrientation)
                {
                    Vector3 palm_vector = grabbable.palmOrientation;
                    if (hand_model.GetLeapHand().IsLeft)
                    {
                        palm_vector = Vector3.Scale(palm_vector, new Vector3(-1, 1, 1));
                    }

                    Quaternion relative_rotation = Quaternion.Inverse(palm_rotation_) *
                                                   active_object_.transform.rotation;
                    Vector3    axis_in_palm    = relative_rotation * grabbable.objectOrientation;
                    Quaternion axis_correction = Quaternion.FromToRotation(axis_in_palm, palm_vector);
                    if (Vector3.Dot(axis_in_palm, palm_vector) < 0)
                    {
                        axis_correction = Quaternion.FromToRotation(axis_in_palm, -palm_vector);
                    }

                    rotation_from_palm_ = axis_correction * relative_rotation;
                }
            }
        }
    }
 // Use this for initialization
 void Start()
 {
     /*Controller controller = new Controller();
      * Frame frame = controller.Frame(); // controller is a Controller object
      * if (frame.Hands.Count > 0)
      * {
      *  List<Hand> hands = frame.Hands;
      *  leapHand = hands[0];
      * }
      * finger = leapHand.Fingers;
      * Finger.FingerType fingerType = finger.Type;*/
     handModel = GetComponent <HandModel>();
     leapHand  = handModel.GetLeapHand();
     if (leapHand == null)
     {
         Debug.LogError("No leap_hand founded");
     }
 }
Exemple #7
0
        public override void Update()
        {
            var hand = HandModel.GetLeapHand();

            if (hand == null)
            {
                Triggered = false;
                return;
            }
            if (hand.PinchDistance < PINCH_DISTANCE - DEBOUNCE_FACTOR)
            {
                Triggered = true;
            }
            else if (hand.PinchDistance > PINCH_DISTANCE + DEBOUNCE_FACTOR)
            {
                Triggered = false;
            }
        }
Exemple #8
0
    /// <summary>
    /// Start viene eseguito una volta quando parte lo script.
    /// </summary>
    void Start()
    {
        // Prende il componente RigidHand presente in questo oggetto (Mano destra).
        hand_model = this.GetComponent <RigidHand>();
        // Seleziona la mano presente nel modello hand_model (Mano destra).
        leap_hand = hand_model.GetLeapHand();
        // Se la mano non viene rilevata solleva un errore.
        if (leap_hand == null)
        {
            Debug.LogError("No leap_hand founded");
        }

        // assegna l'azione UpdateBrushSize all'evento di movimento dello slider
        BrushSizeSlider.HorizontalSlideEvent = new Action <float>(UpdateBrushSize);

        // Debugging code
        // Assegnazione della texture.
        Texture2D tt = (Texture2D)tela.GetComponent <Renderer>().material.mainTexture;
        //DrawCircle(tt,Color.black, 0, 0, 10);
    }
    void Start()
    {
        //initialize Leap Motion
        hand_model = GetComponent <HandModel> ();
        leap_hand  = hand_model.GetLeapHand();
        if (leap_hand == null)
        {
            Debug.LogError("No leap_hand founded");
        }
        LEAPcontroller = new Controller();
        LEAPcontroller.EnableGesture(Gesture.GestureType.TYPE_CIRCLE);
        LEAPcontroller.EnableGesture(Gesture.GestureType.TYPE_SWIPE);

        indexFinger  = hand_model.fingers [1];
        middleFinger = hand_model.fingers [2];
        ringFinger   = hand_model.fingers [3];

        initSelect();
        initScale();
    }
 void Start()
 {
     active = false;
     hand_model = GetComponent<HandModel>();
     leap_hand = hand_model.GetLeapHand();
     if (leap_hand == null) Debug.LogError("No leap_hand founded");
     if(isRight)
     {
         if(!lControl.rightHanded)
         {
             isRight = false;
         }
     }
     else
     {
         if(lControl.leftHanded)
         {
             isRight = true;
         }
     }
 }
Exemple #11
0
    protected PinchState GetNewPinchState()
    {
        HandModel hand_model = GetComponent <HandModel>();

        Leap.Hand leap_hand = hand_model.GetLeapHand();

        Leap.Vector leap_thumb_tip   = leap_hand.Fingers[0].TipPosition;
        float       closest_distance = Mathf.Infinity;

        // Check thumb tip distance to joints on all other fingers.
        // If it's close enough, you're pinching.
        for (int i = 1; i < HandModel.NUM_FINGERS; ++i)
        {
            Leap.Finger finger = leap_hand.Fingers[i];

            for (int j = 0; j < FingerModel.NUM_BONES; ++j)
            {
                Leap.Vector leap_joint_position = finger.Bone((Leap.Bone.BoneType)j).NextJoint;

                float thumb_tip_distance = leap_joint_position.DistanceTo(leap_thumb_tip);
                closest_distance = Mathf.Min(closest_distance, thumb_tip_distance);
            }
        }

        // Scale trigger distance by thumb proximal bone length.
        float proximal_length  = leap_hand.Fingers[0].Bone(Leap.Bone.BoneType.TYPE_PROXIMAL).Length;
        float trigger_distance = proximal_length * grabTriggerDistance;
        float release_distance = proximal_length * releaseTriggerDistance;

        if (closest_distance <= trigger_distance)
        {
            return(PinchState.kPinched);
        }
        if (closest_distance <= release_distance && pinch_state_ != PinchState.kReleased &&
            !ObjectReleaseBreak(current_pinch_position_))
        {
            return(PinchState.kReleasing);
        }
        return(PinchState.kReleased);
    }
Exemple #12
0
    void OnTriggerEnter(Collider other)
    {
        HandModel hand_model = GetHand(other);

        if (hand_model != null)
        {
            int         handID      = hand_model.GetLeapHand().Id;
            HandModel[] hand_models = leap_controller_.GetAllGraphicsHands();
            for (int i = 0; i < hand_models.Length; ++i)
            {
                if (hand_models[i].GetLeapHand().Id == handID)
                {
                    GameObject part      = hand_models[i].transform.Find(other.transform.parent.name).Find(other.name).gameObject;
                    Renderer[] renderers = part.GetComponentsInChildren <Renderer>();
                    foreach (Renderer renderer in renderers)
                    {
                        renderer.material.color = Color.red;
                    }
                }
            }
        }
    }
Exemple #13
0
    /*
     * public float laserWidth = 0.1f;
     * public float laserMaxLength = 5f;
     *
     * void Start() {
     *   Vector3[] initLaserPositions = new Vector3[ 2 ] { Vector3.zero, Vector3.zero };
     *   laserLineRenderer.SetPositions( initLaserPositions );
     *   laserLineRenderer.SetWidth( laserWidth, laserWidth );
     * }
     *
     * void Update()
     * {
     *   if( Input.GetKeyDown( KeyCode.Space ) ) {
     *       ShootLaserFromTargetPosition( transform.position, Vector3.forward, laserMaxLength );
     *       laserLineRenderer.enabled = true;
     *   }
     *   else {
     *       laserLineRenderer.enabled = false;
     *   }
     * }
     *
     *
     */
    void Start()
    {
        GameObject gameObj = GameObject.Find("OVRCameraRig");

        if (gameObj != null)
        {
            modify = gameObj.GetComponent <Modify>();
        }

        cam = GameObject.Find("CenterEyeAnchor");
        if (this.name.Contains("L"))
        {
            isLeft = true;
        }
        else
        {
            isLeft = false;
        }
        hand_model = GetComponent <HandModel>();
        leap_hand  = hand_model.GetLeapHand();
        finger0    = hand_model.fingers[0];
        finger1    = hand_model.fingers[1];
        finger2    = hand_model.fingers[2];
        if (leap_hand == null)
        {
            Debug.LogError("No leap_hand founded");
        }
        if (!isLeft)
        {
            lineRenderer = GetComponent <LineRenderer>();
            //  Vector3[] initLaserPositions = new Vector3[2] { Vector3.zero, Vector3.zero };
            //  lineRenderer.SetPositions(initLaserPositions);
            //lineRenderer.SetWidth(laserWidth, laserWidth);
            lineRenderer.enabled = true;
        }

        ClearDirection();
    }
Exemple #14
0
    void OnTriggerEnter(Collider other)
    {
        HandModel hand_model = GetHand(other);

        if (hand_model != null)
        {
            int         handID      = hand_model.GetLeapHand().Id;
            HandModel[] hand_models = leap_controller_.GetAllGraphicsHands();
            for (int i = 0; i < hand_models.Length; ++i)
            {
                if (hand_models[i].GetLeapHand().Id == handID)
                {
                    Transform part = null;
                    if (other.transform.parent.GetComponent <HandModel>() != null)
                    {
                        // Palm or Forearm components
                        part = FindPart(hand_models[i].transform, other.name);
                    }
                    else if (other.transform.parent.GetComponent <FingerModel>() != null)
                    {
                        // Bone in a finger
                        part = FindPart(FindPart(hand_models[i].transform, other.transform.parent.name), other.name);
                    }
                    //Debug.Log ("Detected: " + other.transform.parent.name + "/" + other.gameObject.name);
                    if (part != null)
                    {
                        Renderer[] renderers = part.GetComponentsInChildren <Renderer>();
                        foreach (Renderer renderer in renderers)
                        {
                            //Debug.Log ("Marked: " + renderer.gameObject.transform.parent.name + "/" + renderer.gameObject.name);
                            renderer.material.color = Color.red;
                        }
                    }
                }
            }
        }
    }
Exemple #15
0
 void Start()
 {
     active     = false;
     hand_model = GetComponent <HandModel>();
     leap_hand  = hand_model.GetLeapHand();
     if (leap_hand == null)
     {
         Debug.LogError("No leap_hand founded");
     }
     if (isRight)
     {
         if (!lControl.rightHanded)
         {
             isRight = false;
         }
     }
     else
     {
         if (lControl.leftHanded)
         {
             isRight = true;
         }
     }
 }
Exemple #16
0
    public bool Pinching()
    {
        HandModel hand_model = GetComponent <HandModel>();
        Hand      leap_hand  = hand_model.GetLeapHand();

        Vector leap_thumb_tip   = leap_hand.Fingers[0].TipPosition;
        float  closest_distance = Mathf.Infinity;

        // Check thumb trip distance to joints on all other fingers.
        // If it's close enough, you're pinching.
        for (int i = 1; i < HandModel.NUM_FINGERS; ++i)
        {
            Finger finger = leap_hand.Fingers[i];

            for (int j = 0; j < FingerModel.NUM_BONES; ++j)
            {
                Vector leap_joint_position = finger.Bone((Bone.BoneType)j).NextJoint;

                float thumb_tip_distance = leap_joint_position.DistanceTo(leap_thumb_tip);
                closest_distance = Mathf.Min(closest_distance, thumb_tip_distance);
            }
        }

        // Scale trigger distance by thumb proximal bone length.
        float proximal_length  = leap_hand.Fingers[0].Bone(Bone.BoneType.TYPE_PROXIMAL).Length;
        float trigger_distance = proximal_length * grabTriggerDistance;

        if (closest_distance <= trigger_distance)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
Exemple #17
0
    // Update is called once per frame
    void LateUpdate()
    {
        if (handController.GetAllGraphicsHands().Length != 0)
        {
            handModel = handController.GetAllGraphicsHands()[0];
            hand      = handModel.GetLeapHand();
            finger0   = hand.Fingers[0]; //thumb
            finger1   = hand.Fingers[1]; //index
            finger2   = hand.Fingers[2]; //middle finger
            finger3   = hand.Fingers[3]; //ring finger
            pos       = hand.PalmPosition;
            camRot    = this.transform.rotation;
            //movement and rotation speed
            velocity = hand.PalmVelocity;
            speed    = velocity.Magnitude / 50f;

            /* The transformations are based on the palm position and movement.
             * The fingers are used to create new commands */

            Debug.DrawRay(this.GetComponentInChildren <Camera>().transform.position, Vector3.forward);

            //Extend index to activate camera movement
            if (finger1.IsExtended && !finger2.IsExtended && !finger0.IsExtended)
            {
                moveCamera();
            }

            //Extend index and middle finger to activate camera rotation
            if (finger1.IsExtended && finger2.IsExtended && !finger3.IsExtended && !finger0.IsExtended)
            {
                rotateCamera();
            }
            oldPos    = pos;
            oldCamRot = camRot;
        }
    }
Exemple #18
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);
    }
Exemple #19
0
    void FixedUpdate()
    {
        UpdatePalmRotation();
        UpdatePinchPosition();
        HandModel hand_model = GetComponent <HandModel>();
        Hand      leap_hand  = hand_model.GetLeapHand();

        //-------------------------------------------------------------------------------

//		Debug.Log (pinch_state_.ToString()+" : "+hand_model.GetLeapHand().IsLeft);


        if (pinch_state_ == PinchState.kPinched && hand_model.GetLeapHand().IsLeft)
        {
            closedLeft = true;
        }
        else
        {
            closedLeft = false;
        }

        if (pinch_state_ == PinchState.kPinched && hand_model.GetLeapHand().IsRight)
        {
            closedRight = true;
        }
        else
        {
            closedRight = false;
        }

//		Debug.Log("closedLeft: " + closedLeft + " - closedRight: " + closedRight);

        //-------------------------------------------------------------------------------


        if (leap_hand == null)
        {
            return;
        }

        PinchState new_pinch_state = GetNewPinchState();

        if (pinch_state_ == PinchState.kPinched)
        {
            if (new_pinch_state == PinchState.kReleased)
            {
                OnRelease();
            }
            else if (active_object_ != null)
            {
                ContinueHardPinch();
            }
        }
        else if (pinch_state_ == PinchState.kReleasing)
        {
            if (new_pinch_state == PinchState.kReleased)
            {
                OnRelease();
            }
            else if (new_pinch_state == PinchState.kPinched)
            {
                StartPinch();
            }
            else if (active_object_ != null)
            {
                ContinueSoftPinch();
            }
        }
        else
        {
            if (new_pinch_state == PinchState.kPinched)
            {
                StartPinch();
            }
            else
            {
                Hover();
            }
        }
        pinch_state_ = new_pinch_state;
    }
Exemple #20
0
 protected virtual float GetUnsmoothedConfidence()
 {
     return(_handModel.GetLeapHand().Confidence);
 }
Exemple #21
0
    void Update()
    {
        bool      trigger_pinch = false;
        HandModel hand_model    = GetComponent <HandModel> ();
        Hand      leap_hand     = hand_model.GetLeapHand();

        if (leap_hand == null)
        {
            return;
        }
        if (fleeing)
        {
            float speed = 2.0F;
            float step  = speed * Time.deltaTime;

            grabbed_.GetComponent <Rigidbody> ().transform.position = Vector3.MoveTowards(
                grabbed_.GetComponent <Rigidbody> ().transform.position,
                GameObject.Find("Map").transform.position, step);
            return;
        }
        if (runaway)
        {
            Debug.Log("runaway");
            grabbed_.GetComponent <Rigidbody>().velocity = grabbed_.GetComponent <Rigidbody>().velocity * 0.8F;
            return;
        }

        // Scale trigger distance by thumb proximal bone length.
        Vector leap_thumb_tip   = leap_hand.Fingers [0].TipPosition;
        float  proximal_length  = leap_hand.Fingers [0].Bone(Bone.BoneType.TYPE_PROXIMAL).Length;
        float  trigger_distance = proximal_length * TRIGGER_DISTANCE_RATIO;

        // Check thumb tip distance to joints on all other fingers.
        // If it's close enough, start pinching.
        for (int i = 1; i < HandModel.NUM_FINGERS && !trigger_pinch; ++i)
        {
            Finger finger = leap_hand.Fingers [i];

            for (int j = 0; j < FingerModel.NUM_BONES && !trigger_pinch; ++j)
            {
                Vector leap_joint_position = finger.Bone((Bone.BoneType)j).NextJoint;
                if (leap_joint_position.DistanceTo(leap_thumb_tip) < trigger_distance)
                {
                    trigger_pinch = true;
                }
            }
        }

        Vector3 pinch_position = hand_model.fingers [0].GetTipPosition();

        // Only change state if it's different.
        if (trigger_pinch && !pinching_)
        {
            OnPinch(pinch_position);
        }
        else if (!trigger_pinch && pinching_)
        {
            OnRelease(pinch_position - grabbed_.transform.position);
        }

        // Accelerate what we are grabbing toward the pinch.
        if (grabbed_ != null)
        {
            Vector3 minDist  = new Vector3(.2F, .2F, .2F);
            Vector3 distance = pinch_position - grabbed_.transform.position;
            if (distance.sqrMagnitude > minDist.sqrMagnitude)
            {
                grabbed_.GetComponent <Rigidbody> ().AddForce(forceSpringConstant * distance);
            }
            else
            {
                wasGrabbed = true;
                grabbed_.GetComponent <Rigidbody> ().velocity = Vector3.zero;
            }
        }
    }
 void Update()
 {
     hand = HM.GetLeapHand();
 }
Exemple #23
0
    // Update is called once per frame
    void Update()
    {
        Frame       frame       = controller.Frame();
        GestureList gesturelist = frame.Gestures();

        palmnormal = m_HandModel.GetPalmNormal();

        for (int i = 0; i < gesturelist.Count; i++)
        {
            Gesture gesture = gesturelist [i];
            change = 0;
            if (m_HandModel.GetLeapHand().IsRight)
            {
                if (indxextend = m_HandModel.fingers [1].GetLeapFinger().IsExtended)
                {
                    if (palmnormal.y < 0)                                               //when palmnormal.y > 0, it means the palm is facing up.
                    {
                        Debug.Log("The hand is facing down");
                        if (gesture.Type == Gesture.GestureType.TYPESWIPE)
                        {
                            SwipeGesture Swipe          = new SwipeGesture(gesture);
                            Vector       swipeDirection = Swipe.Direction;
                            if (swipeDirection.y < 0)
                            {
                                Debug.Log("Tempo Down");
                                change += 2f;
                                ch      = 1;
                                if (audio.pitch > 0)
                                {
                                    audio.pitch -= Time.deltaTime * change / timeToDecrease;
                                }
                                else
                                {
                                    Debug.Log("The pitch is below zero. Exitting application.");
                                }
                            }
                        }
                    }
                    else if (palmnormal.y > 0)                                                  // when palmnormal.y < 0, it means the palm is facing down.
                    {
                        Debug.Log("The hand is facing up");
                        if (gesture.Type == Gesture.GestureType.TYPESWIPE)
                        {
                            SwipeGesture Swipe          = new SwipeGesture(gesture);
                            Vector       swipeDirection = Swipe.Direction;
                            if (swipeDirection.y > 0)
                            {
                                Debug.Log("Tempo Up");
                                change -= 2f;
                                ch      = 0;
                                if (audio.pitch > 0)
                                {
                                    audio.pitch -= Time.deltaTime * change / timeToDecrease;
                                }
                                else
                                {
                                    Debug.Log("The pitch is below zero. Exitting application.");
                                }
                            }
                        }
                    }
                }
            }
        }
    }
	public static float AngleBetweenFingerTipsHorizontal(HandModel hand, int fingerIndexOne, int fingerIndexTwo){


		float angle = AngleBetweenFingerTips(hand,fingerIndexOne, fingerIndexTwo,hand.GetPalmNormal());

		if (hand.GetLeapHand ().IsRight)
			return -angle;

		return angle;

	}
Exemple #25
0
    /**
      * Destroys a HandModel instance if HandController.destroyHands is true (the default).
      * If you set destroyHands to false, you must destroy the hand instances elsewhere in your code.
      * EDIT BV : put back the hands in the initial position
      */
    protected void DestroyHand(HandModel hand_model)
    {
        if (hand_model.GetLeapHand().IsLeft) {
            lefthand = null;
            //Debug.Log("DestroyHand: minus left hand");
        } else {
            righthand = null;
            //Debug.Log("DestroyHand: minus right hand");
        }

        if (destroyHands)
            Destroy(hand_model.gameObject);
        else
            hand_model.SetLeapHand(null);
        //Debug.Log("DestroyHand " + hand_model);
    }
Exemple #26
0
 /// <summary>
 /// Metodo eseguito all'avvio dello script.
 /// </summary>
 void Start()
 {
     hand_model = GetComponent <HandModel>();
     leap_hand  = hand_model.GetLeapHand();
 }
Exemple #27
0
    /// <summary>
    /// Metodo eseguito ad ogni frame.
    /// </summary>
    void Update()
    {
        // Seleziona la mano destra presente nel frame attuale.
        leap_hand = hand_model.GetLeapHand();
        // Se non viene trovata nessuna mano crea un errore.
        if (leap_hand == null)
        {
            Debug.LogError("No leap_hand founded");
        }

        try
        {
            // Se la mano destra è presente.
            if (handIn)
            {
                // Salva la posizione normalizzata della mano.
                palmNormal = leap_hand.PalmPosition;

                // Se il pollice e l'indice si avvicinano.
                if (leap_hand.IsPinching())
                {
                    // Imposta la mano come "pinchante"
                    pinch = true;
                    // Salva la distanza tra pollice e indice.
                    pinchDistance = leap_hand.PinchDistance;

                    // Disegna
                    Draw();
                    //fill();
                }
                // Se la mano non fa pinch.
                else
                {
                    // Indica che l'indice e il pollice non sono vicini.
                    pinch = false;
                    // Indica che l'indice e il pollice non sono più vicini.
                    prevPinch = false;

                    //if (leftHand.GetComponent<ManageLeft>().IsFaceUp())
                    //{
                    //    if (leap_hand.PalmNormal.y >= 0.85)
                    //    {
                    //        actionController.GetComponent<Rotate>().RotateRight();
                    //    }
                    //    else if (leap_hand.PalmNormal.y <= -0.7)
                    //    {
                    //        actionController.GetComponent<Rotate>().RotateLeft();
                    //    }
                    //    else if (leap_hand.GrabStrength == 1)
                    //    {
                    //        actionController.GetComponent<Rotate>().NormalizeRotation();
                    //    }
                    //}
                }

                // Se la mano è stretta a pugno
                if (leap_hand.GrabStrength == 1)
                {
                    // Indica che la mano è stretta a pugno.
                    grab = true;
                }
                else
                {
                    // Indica che la mano non è stretta a pugno.
                    grab = false;
                }
            }
        }
        // Se la mano non è inserita
        catch (NullReferenceException e)
        {
            // Crea un errore.
            Debug.LogError("Mani non inserite " + e.Message);
        }
    }
	bool IsReadyToCastFireBall(HandModel hand){

		return HandRecog.IsPalmFacingUpwards (hand, palmFacingUpAngle) && HandRecog.IsHandClenchingStrict (hand, clenchingAngle) && hand.GetLeapHand().Confidence >= confidenceLevel;

	}
Exemple #29
0
    void Update()
    {
        HandModel hand_model = GetComponent <HandModel> ();

        if (hand_model == null)
        {
            return;
        }
        Hand leap_hand = hand_model.GetLeapHand();

        if (leap_hand == null)
        {
            return;
        }

        Vector3 handCoordinates = getHandCoordinates(leap_hand);

        // Smooth hand coordinate signal from Leap by averaging with the most recent such coordinates
        Vector3 meanLastFewHandPositions = getPositionsMean(lastFewHandPositions, 4);

        if (lastFewHandPositions.Count > 8 && Vector3.Distance(meanLastFewHandPositions, handCoordinates) > 2.0f)
        {
            // Handle outlier
        }

        // Same smoothing applied to the grab/pinch strengths
        float meanLastFewGrabStrengths   = getMean(lastFewGrabStrengths, 4);
        float meanLastFewPinchStrengthss = getMean(lastFewPinchStrengths, 4);

        if (lastFewGrabStrengths.Count > 6 &&
            (Mathf.Abs(meanLastFewGrabStrengths - (float)leap_hand.GrabStrength) > 0.6f ||
             Mathf.Abs(meanLastFewPinchStrengthss - (float)leap_hand.PinchStrength) > 0.6f))
        {
            // Handle outlier
        }

        lastFewGrabStrengths.Add((float)leap_hand.GrabStrength);
        lastFewPinchStrengths.Add((float)leap_hand.PinchStrength);
        if (lastFewGrabStrengths.Count >= 10)
        {
            lastFewGrabStrengths.RemoveAt(0);
            lastFewPinchStrengths.RemoveAt(0);
        }

        lastFewHandPositions.Add(handCoordinates);
        if (lastFewHandPositions.Count >= 10)
        {
            lastFewHandPositions.RemoveAt(0);
        }
        Vector3 newHandPosition = getPositionsMean(lastFewHandPositions, 3);

        // Ensure that the grab gesture is intentional and the behavior is expected
        if (isGrabbing && lastFewGrabStrengths.Count > 8 && (getMean(lastFewGrabStrengths, 8) > 0.7 || getMean(lastFewPinchStrengths, 8) > 0.7))
        {
            isGrabbing = true;
        }
        else if (!isGrabbing && lastFewGrabStrengths.Count > 8 &&
                 (getMean(lastFewGrabStrengths, 8) > 0.8 || getMean(lastFewPinchStrengths, 8) > 0.8))
        {
            isGrabbing = true;
        }
        else
        {
            isGrabbing = false;
        }

        if (isGrabbing)
        {
            grabbableObj.GetComponent <Animator> ().SetBool("Grounded_b", false);
            grabbableObj.GetComponent <Animator> ().SetFloat("Speed_f", 0.0f);
            Debug.Log(newHandPosition.y);
            grabbableObj.transform.position = newHandPosition;
        }
        else
        {
            grabbableObj.GetComponent <Animator> ().SetBool("Grounded_b", true);
        }
    }
 private void GetHandInfo()
 {
     //Get hand info and if there's no hand in the scene, don't run the rest of the update
     handModel = GetComponent<HandModel>();
     leapHand = handModel.GetLeapHand();
     if (leapHand == null)
         return;
 }
        /**
         * Updates the graphics hands so that they're drawn properly
         * */
        protected new void UpdateHandModels(Dictionary <int, HandModel> all_hands,
                                            HandList leap_hands,
                                            HandModel left_model, HandModel right_model)
        {
            List <int> ids_to_check = new List <int>(all_hands.Keys);

            // Go through all the active hands in the Leap motion frame and update them.
            int num_hands = leap_hands.Count;

            for (int h = 0; h < num_hands; ++h)
            {
                Hand leap_hand = leap_hands[h];

                HandModel model = (mirrorZAxis != leap_hand.IsLeft) ? left_model : right_model;

                // Only create or update if the hand is enabled.
                if (model != null)
                {
                    ids_to_check.Remove(leap_hand.Id);                  //this prevents the hand from being made inactive, since it is active.

                    // Create the hand and initialized it if it doesn't exist yet.
                    if (!all_hands.ContainsKey(leap_hand.Id))
                    {
                        HandModel new_hand = InitializeHand(model);                     //We don't instantiate these ones - they're already on the avatar, so we just init them instead of CreateHand them
                        new_hand.SetLeapHand(leap_hand);
                        new_hand.MirrorZAxis(mirrorZAxis);
                        new_hand.SetController(this);

                        // Set scaling based on reference hand.
                        float hand_scale = MM_TO_M * leap_hand.PalmWidth / new_hand.handModelPalmWidth;
                        new_hand.transform.localScale = hand_scale * transform.lossyScale;

                        new_hand.InitHand();
                        new_hand.UpdateHand();
                        all_hands[leap_hand.Id] = new_hand;

                        if (new_hand.GetLeapHand().IsLeft)
                        {
                            leftActive = true;
                        }
                        else
                        {
                            rightActive = true;
                        }
                        //Debug.Log ("Initialised graphics hand and added to active hands list, now contains " + all_hands.Count + " hands");
                    }
                    else
                    {
                        // Make sure we update the Leap Hand reference.
                        HandModel hand_model = all_hands[leap_hand.Id];
                        hand_model.SetLeapHand(leap_hand);
                        hand_model.MirrorZAxis(mirrorZAxis);

                        // Set scaling based on reference hand.
                        float hand_scale = MM_TO_M * leap_hand.PalmWidth / hand_model.handModelPalmWidth;
                        hand_model.transform.localScale = hand_scale * transform.lossyScale;
                        hand_model.UpdateHand();

                        if (hand_model.GetLeapHand().IsLeft)
                        {
                            leftActive = true;
                        }
                        else
                        {
                            rightActive = true;
                        }
                        //Debug.Log ("Updated graphics hand");
                    }
                }
            }

            // flag hands that are no longer active, so IK knows to turn off
            for (int i = 0; i < ids_to_check.Count; ++i)
            {
                if (all_hands[ids_to_check[i]].GetLeapHand().IsLeft)
                {
                    leftActive = false;
                }
                else
                {
                    rightActive = false;
                }
                all_hands.Remove(ids_to_check[i]);
            }
        }
Exemple #32
0
    protected void StartGrasp()
    {
        // Only grasp if we're hovering over an object.
        if (this.ActiveObject == null)
        {
            return;
        }

        HandModel       handModel = this.GetComponent <HandModel>();
        GraspableObject graspable = this.ActiveObject.GetComponent <GraspableObject>();

        Leap.Utils.IgnoreCollisions(this.gameObject, this.ActiveObject.gameObject, true);

        // Setup initial position and rotation conditions.
        this.PalmRotation = handModel.GetPalmRotation();
        this.GraspOffset  = Vector3.zero;

        // If we don't center the object, find the closest point in the collider for our grab point.
        if (graspable == null || !graspable.CenterGraspedObject)
        {
            Vector3 deltaPosition = ActiveObject.transform.position - this.CurrentGraspCenter;

            Ray        graspRay = new Ray(this.CurrentGraspCenter, deltaPosition);
            RaycastHit graspHit;

            // If the raycast hits the object, we are outside the collider so grab the hit point.
            // If not, we are inside the collider so just use the grasp position.
            if (ActiveObject.Raycast(graspRay, out graspHit, GraspObjectDistance))
            {
                this.GraspOffset = this.ActiveObject.transform.position - graspHit.point;
            }
            else
            {
                this.GraspOffset = this.ActiveObject.transform.position - CurrentGraspCenter;
            }
        }

        this.SmoothedGraspPosition = this.ActiveObject.transform.position - this.GraspOffset;
        this.GraspOffset           = Quaternion.Inverse(this.ActiveObject.transform.rotation) * GraspOffset;
        this.RotationFromPalm      = Quaternion.Inverse(this.PalmRotation) * this.ActiveObject.transform.rotation;

        // If we can rotate the object quickly, increase max angular velocity for now.
        if (graspable == null || graspable.RotateQuickly)
        {
            this.ObjectMaxAngularVelocity = this.ActiveObject.GetComponent <Rigidbody>().maxAngularVelocity;
            this.ActiveObject.GetComponent <Rigidbody>().maxAngularVelocity = Mathf.Infinity;
        }

        if (graspable != null)
        {
            // Notify grabbable object that it was grabbed.
            if (graspable.GetResponseTime() < 0L)
            {
                if (handModel != null)
                {
                    Vector3 normal     = handModel.GetPalmNormal();
                    float   angleLeft  = Vector3.Angle(normal, Vector3.left);
                    float   angleRight = Vector3.Angle(normal, Vector3.right);
                    if (GraspController.Verbose)
                    {
                        Debug.LogWarning("hand normal: " + normal + ", to right: " + angleRight + ", to left: " + angleLeft);
                    }
                    if (angleLeft < angleRight)
                    {
                        graspable.setGraspDirection(GraspableObject.GraspDirection.UPRIGHT);
                    }
                    else
                    {
                        graspable.setGraspDirection(GraspableObject.GraspDirection.ROTATED);
                    }
                }
                else
                {
                    if (GraspController.Verbose)
                    {
                        Debug.LogWarning("no hand found...");
                    }
                }
            }
            graspable.OnGrasp(this.gameObject);

            if (graspable.UseAxisAlignment)
            {
                // If this option is enabled we only want to align the object axis with the palm axis,
                // hence we cancel out any rotation about the aligned axis.
                Vector3 palmVector = graspable.RightHandAxis;
                if (handModel.GetLeapHand().IsLeft)
                {
                    palmVector = Vector3.Scale(palmVector, new Vector3(-1, 1, 1));
                }
                Vector3    axisInPalm     = this.RotationFromPalm * graspable.ObjectAxis;
                Quaternion axisCorrection = Quaternion.FromToRotation(axisInPalm, palmVector);
                if (Vector3.Dot(axisInPalm, palmVector) < 0)
                {
                    axisCorrection = Quaternion.FromToRotation(axisInPalm, -palmVector);
                }
                this.RotationFromPalm = axisCorrection * this.RotationFromPalm;
            }
        }
    }
Exemple #33
0
    /// <summary>
    /// Metodo eseguito ad ogni frame.
    /// </summary>
    void Update()
    {
        // Ad ogni frame cerca la mano sinistra inserita.
        leap_hand = hand_model.GetLeapHand();
        // Se non è presente nessuna mano solleva un errore.
        if (leap_hand == null)
        {
            Debug.LogError("No leap_hand founded");
        }

        try
        {
            // Se la mano sinistra è inserita
            if (handIn)
            {
                // Se la mano è stretta a pugno
                if (leap_hand.GrabStrength == 1)
                {
                    // Salva lo stato della mano stretta a pugno.
                    isGrabbing = true;
                }
                // altrimenti
                else
                {
                    // Salva lo stato della mano che non è distesa.
                    isGrabbing = false;
                }

                // Se l'indice della mano e il pollice della mano sono vicini o si toccano
                if (leap_hand.IsPinching())
                {
                    // Salva lo stato di pinching.
                    isPinching = true;
                }
                // altrimenti
                else
                {
                    // Salva lo stato di pinching.
                    isPinching = false;
                }

                // Se il palmo della mano è rivolto verso l'alto
                if (leap_hand.PalmNormal.y >= 0)
                {
                    // Mostra il menù inventario.
                    menu.SetActive(true);
                    // Salva lo stato del palmo.
                    isFaceUp = true;
                }
                // altrimenti
                else
                {
                    // Disattiva il menì inventario.
                    menu.SetActive(false);
                    // Salva lo stato del palmo.
                    isFaceUp = false;
                }

                // Gestione dello zoom che è stata disattivata.
                //if (isPinching && rightHand.GetComponent<ManageRight>().IsHandPinching())
                //{
                //	double distance = leap_hand.PalmPosition.DistanceTo(rightHand.GetComponent<ManageRight>().GetPalmNormal());
                //	if (distance > 0.38)
                //	{
                //		actionController.GetComponent<Zoom>().ZoomOut();
                //	}
                //	else if (distance < 0.2)
                //	{
                //		actionController.GetComponent<Zoom>().ZoomIn();
                //	}
                //}

                // Se entrambe le mani sono strette a pugno
                if (isGrabbing && rightHand.GetComponent <ManageRight>().IsHandGrabbing())
                {
                    // Notifica lo stato delle mani.
                    Debug.Log("Both hand grab");
                }
            }
        }
        // Se nessuna mano è inserita
        catch (NullReferenceException e)
        {
            // Notifica l'assenza di mani
            Debug.LogError("Mani non inserite " + e.Message);
            // Disattiva il menu inventario attaccato alla mano sinistra.
            menu.SetActive(false);
        }
    }
Exemple #34
0
    void Update()
    {
        bool      trigger_pinch = false;
        HandModel hand_model    = GetComponent <HandModel>();
        Hand      leap_hand     = hand_model.GetLeapHand();

        if (leap_hand == null)
        {
            return;
        }

        // Scale trigger distance by thumb proximal bone length.
        Vector leap_thumb_tip   = leap_hand.Fingers[0].TipPosition;
        float  proximal_length  = leap_hand.Fingers[0].Bone(Bone.BoneType.TYPE_PROXIMAL).Length;
        float  trigger_distance = proximal_length * TRIGGER_DISTANCE_RATIO;

        // Check thumb tip distance to joints on all other fingers.
        // If it's close enough, start pinching.
        for (int i = 1; i < HandModel.NUM_FINGERS && !trigger_pinch; ++i)
        {
            Finger finger = leap_hand.Fingers[i];

            for (int j = 0; j < FingerModel.NUM_BONES && !trigger_pinch; ++j)
            {
                Vector leap_joint_position = finger.Bone((Bone.BoneType)j).NextJoint;
                if (leap_joint_position.DistanceTo(leap_thumb_tip) < trigger_distance)
                {
                    trigger_pinch = true;
                }
            }
        }

        Vector3 pinch_position = 0.5f * (hand_model.fingers[0].GetTipPosition() +
                                         hand_model.fingers[1].GetTipPosition());

        // Only change state if it's different.
        if (leap_hand.Confidence >= minConfidence &&
            leap_hand.PalmVelocity.ToUnityScaled().magnitude <= maxVelocity)
        {
            if (trigger_pinch && !pinching_)
            {
                OnPinch(pinch_position);
            }
            else if (!trigger_pinch && pinching_)
            {
                OnRelease();
            }
        }

        // Accelerate what we are grabbing toward the pinch.
        if (grabbed_ != null)
        {
            Grabbable grabbable = grabbed_.GetComponent <Grabbable>();
            palm_rotation_ = Quaternion.Slerp(palm_rotation_, hand_model.GetPalmRotation(),
                                              1.0f - filtering);
            Vector3 delta_palm_position = hand_model.GetPalmPosition() - palm_position_;
            palm_position_ += (1 - filtering) * delta_palm_position;

            Vector3    target_position = pinch_position;
            Quaternion target_rotation = palm_rotation_ * start_rotation_;

            if (grabbable != null)
            {
                if (grabbable.keepDistanceWhenGrabbed)
                {
                    target_position = palm_position_ + palm_rotation_ * start_position_;
                }

                if (grabbable.preferredOrientation)
                {
                    Quaternion relativeToPalm = Quaternion.FromToRotation(grabbable.objectOrientation,
                                                                          grabbable.palmOrientation);
                    target_rotation = palm_rotation_ * relativeToPalm;
                }
            }

            Vector3 velocity = (target_position - grabbed_.transform.position) / Time.fixedDeltaTime;
            grabbed_.GetComponent <Rigidbody>().velocity = velocity;


            Quaternion delta_rotation = target_rotation *
                                        Quaternion.Inverse(grabbed_.transform.rotation);

            float   angle = 0.0f;
            Vector3 axis  = Vector3.zero;
            delta_rotation.ToAngleAxis(out angle, out axis);

            if (angle >= 180)
            {
                angle = 360 - angle;
                axis  = -axis;
            }
            if (angle != 0)
            {
                grabbed_.GetComponent <Rigidbody>().angularVelocity = angle * axis;
            }
        }
    }
Exemple #35
0
    protected GraspState GetNewGraspState()
    {
        HandModel handModel = this.GetComponent <HandModel>();
        Hand      leapHand  = handModel.GetLeapHand();

        /*
         * Vector leapThumbTip = leapHand.Fingers[0].TipPosition;
         * float closestDistance = Mathf.Infinity;
         *
         * // Check thumb tip distance to joints on all other fingers.
         * // If it's close enough, we are in a grasp position.
         * for (int i = 1; i < HandModel.NUM_FINGERS; i++) {
         * Finger finger = leapHand.Fingers[i];
         *
         * for (int j = 0; j < FingerModel.NUM_BONES; j++) {
         *  Vector leapJointPosition = finger.Bone((Bone.BoneType)j).NextJoint;
         *
         *  float distanceToThumbTip = leapJointPosition.DistanceTo(leapThumbTip);
         *  closestDistance = Mathf.Min(closestDistance, distanceToThumbTip);
         * }
         *
         * //float distanceToThumbTip = leapHand.Fingers[i].TipPosition.DistanceTo(leapThumbTip);
         * //closestDistance = Mathf.Min(closestDistance, distanceToThumbTip);
         * }
         */
        // check destruction
        if (this.CurrentGraspState == GraspState.GRASPED && this.ActiveObject != null)
        {
            // get finger centroid (except thumb)
            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++;
                }
            }

            /*this.invalidCounter++;
             * if (this.invalidCounter >= GraspController.MAX_SUCCESSIVE_GRAB_FLUCTUATIONS)
             * {
             * return GraspState.RELEASED;
             * }*/
            if (stretchedFingers >= 3)
            {
                return(GraspState.RELEASED);
            }
            else
            {
                return(GraspState.GRASPED);
            }
        }

        return(GraspState.RELEASED);
    }
    protected void StartPinch()
    {
        // Only pinch if we're hovering over an object.
        if (active_object_ == null)
        {
            return;
        }



        HandModel hand_model = GetComponent <HandModel>();

        Debug.Log("Start pinch");


        Leap.Utils.IgnoreCollisions(gameObject, active_object_.gameObject, true);
        GrabbableObject grabbable = active_object_.GetComponent <GrabbableObject>();

        // Setup initial position and rotation conditions.
        palm_rotation_       = hand_model.GetPalmRotation();
        object_pinch_offset_ = Vector3.zero;

        // If we don't center the object, find the closest point in the collider for our grab point.
        if (grabbable == null || !grabbable.centerGrabbedObject)
        {
            Vector3 delta_position = active_object_.transform.position - current_pinch_position_;

            Ray        pinch_ray = new Ray(current_pinch_position_, delta_position);
            RaycastHit pinch_hit;

            // If we raycast hits the object, we're outside the collider so grab the hit point.
            // If not, we're inside the collider so just use the pinch position.
            if (active_object_.Raycast(pinch_ray, out pinch_hit, grabObjectDistance))
            {
                object_pinch_offset_ = active_object_.transform.position - pinch_hit.point;
            }
            else
            {
                object_pinch_offset_ = active_object_.transform.position - current_pinch_position_;
            }
        }

        filtered_pinch_position_ = active_object_.transform.position - object_pinch_offset_;
        object_pinch_offset_     = Quaternion.Inverse(active_object_.transform.rotation) *
                                   object_pinch_offset_;
        rotation_from_palm_ = Quaternion.Inverse(palm_rotation_) * active_object_.transform.rotation;

        // If we can rotate the object quickly, increase max angular velocity for now.
        if (grabbable == null || grabbable.rotateQuickly)
        {
            last_max_angular_velocity_ = active_object_.rigidbody.maxAngularVelocity;
            active_object_.rigidbody.maxAngularVelocity = Mathf.Infinity;
        }

        if (grabbable != null)
        {
            // Notify grabbable object that it was grabbed.
            grabbable.OnGrab();

            if (grabbable.useAxisAlignment)
            {
                // If this option is enabled we only want to align the object axis with the palm axis
                // so we'll cancel out any rotation about the aligned axis.
                Vector3 palm_vector = grabbable.rightHandAxis;
                if (hand_model.GetLeapHand().IsLeft)
                {
                    palm_vector = Vector3.Scale(palm_vector, new Vector3(-1, 1, 1));
                }

                Vector3    axis_in_palm    = rotation_from_palm_ * grabbable.objectAxis;
                Quaternion axis_correction = Quaternion.FromToRotation(axis_in_palm, palm_vector);
                if (Vector3.Dot(axis_in_palm, palm_vector) < 0)
                {
                    axis_correction = Quaternion.FromToRotation(axis_in_palm, -palm_vector);
                }

                rotation_from_palm_ = axis_correction * rotation_from_palm_;
            }
        }
    }
Exemple #37
0
    /**
     * Checks whether the hand is pinching and updates the position of the pinched object.
     */
    void Update()
    {
        // 手の情報を取得
        HandModel hand_model = GetComponent <HandModel>();
        Hand      leap_hand  = hand_model.GetLeapHand();

        if (leap_hand == null)
        {
            return;
        }

        // Scale trigger distance by thumb proximal bone length.
        // 親指の指先の位置
        Vector leap_thumb_tip = leap_hand.Fingers[0 /*親指*/].TipPosition;
        // ピンチを判断するためにトリガとなる距離は
        // 親指の付け根から第一関節までの距離を基準に計算する
        float proximal_length  = leap_hand.Fingers[0].Bone(Bone.BoneType.TYPE_PROXIMAL).Length;
        float trigger_distance = proximal_length * TRIGGER_DISTANCE_RATIO;


        // Check thumb tip distance to joints on all other fingers.
        // If it's close enough, start pinching.
        // 親指以外の指の関節位置と親指の指先の位置の距離を調べて、
        // 鳥がとなる値よりも近かったらピンチフラグをONにする
        bool trigger_pinch = false;

        for (int i = 1; i < HandModel.NUM_FINGERS && !trigger_pinch; ++i)
        {
            Finger finger = leap_hand.Fingers[i];

            for (int j = 0; j < FingerModel.NUM_BONES && !trigger_pinch; ++j)
            {
                Vector leap_joint_position = finger.Bone((Bone.BoneType)j).NextJoint;
                if (leap_joint_position.DistanceTo(leap_thumb_tip) < trigger_distance)
                {
                    trigger_pinch = true;
                }
            }
        }

        // ピンチしている位置を吸着点とする
        // この際、計算していた座標を Leap の世界から Unity の世界の座標に変換
        Vector3 pinch_position = hand_model.fingers[0].GetTipPosition();

        // Only change state if it's different.
        // ピンチしている最中でない && ピンチを検知したら OnPinch を発火
        // 逆の場合は OnRelease を発火
        if (trigger_pinch && !pinching_)
        {
            OnPinch(pinch_position);
        }
        else if (!trigger_pinch && pinching_)
        {
            OnRelease();
        }

        // Accelerate what we are grabbing toward the pinch.
        if (grabbed_ != null)
        {
            Vector3 distance = pinch_position - grabbed_.transform.position;
            grabbed_.GetComponent <Rigidbody>().AddForce(forceSpringConstant * distance);
        }
    }