コード例 #1
0
    private void CheckForceBinary()
    {
        var intHands = manager.interactionControllers
                       .Query()
                       .Select(controller => controller.intHand)
                       .Where(intHand => intHand != null);

        //.Select(intHand => intHand.leapHand);

        foreach (var intHand in intHands)
        {
            var hand = intHand.leapHand;

            if (intHand.isTracked) // If hand is tracked, check for overlaping colliders
            {
                foreach (var finger in hand.Fingers)
                {
                    var fingertipPosition = finger.TipPosition.ToVector3();
                    var buffer            = Physics.OverlapSphere(fingertipPosition, CONTACT_POINT_SIZE);
                    //Debug.DrawLine(fingertipPosition, Vector3.forward, Color.red, 1.0f);

                    var colliders = new List <Collider>();
                    foreach (var col in buffer)
                    {
                        if (col.gameObject.layer != controllerLayer)
                        {
                            colliders.Add(col);
                        }
                        if (col.gameObject.layer == groundLayer)
                        {
                            AutoHeight.OnHandContact();
                        }
                    }

                    float force = 0;
                    if (colliders.Count > 0)
                    {
                        force = 100;
                    }
                    UpdateFinger(finger.Type, hand.IsLeft, force);
                }
            }
            else // If hand is not tracked, set all forces to 0
            {
                if (hand.Fingers[3].Type == Finger.FingerType.TYPE_RING) // If hand was at least once on the scene and fingers are properly initialized
                {
                    foreach (var finger in hand.Fingers)
                    {
                        UpdateFinger(finger.Type, hand.IsLeft, 0);
                    }
                }
            }
        }
    }
コード例 #2
0
    //--------------------------------------------------------------------------------------------------------------------------
    //--------------------------------------------------------------------------------------------------------------------------


    private void CheckForce()
    {
        var intHands = manager.interactionControllers
                       .Query()
                       .Select(controller => controller.intHand)
                       .Where(intHand => intHand != null);

        foreach (var intHand in intHands)
        {
            var hand = intHand.leapHand;

            if (intHand.isTracked)
            {
                foreach (var finger in hand.Fingers)
                {
                    var fingertipPosition = finger.TipPosition.ToVector3();
                    var buffer            = Physics.OverlapSphere(fingertipPosition, CONTACT_POINT_SIZE);
                    //Debug.DrawLine(fingertipPosition, Vector3.forward, Color.red, 1.0f);

                    var colliders = new List <Collider>();
                    foreach (var col in buffer)
                    {
                        if (col.gameObject.layer != controllerLayer)
                        {
                            colliders.Add(col);
                        }
                        if (col.gameObject.layer == groundLayer)
                        {
                            AutoHeight.OnHandContact();
                        }
                    }

                    foreach (var col in colliders)
                    {
                        RaycastHit[] hits;
                        Vector3      origin      = fingertipPosition;
                        Vector3      destination = col.bounds.center;
                        hits = Physics.RaycastAll(origin, destination - origin, Mathf.Infinity);

                        int numHits = 0;
                        foreach (RaycastHit hitInfo in hits)
                        {
                            if (hitInfo.collider == col)
                            {
                                numHits++;
                            }
                        }

                        if (numHits == 0) // If object's collider wasn't hit (which means finger is inside of the object)
                        {
                            float distance = 0;
                            if (col is MeshCollider)
                            {
                                distance = (Physics.ClosestPoint(fingertipPosition,
                                                                 col,
                                                                 col.attachedRigidbody.position,
                                                                 col.attachedRigidbody.rotation)
                                            - fingertipPosition).magnitude;
                            }
                            else
                            {
                                distance = (col.transform.TransformPoint(
                                                col.ClosestPointOnSurface(
                                                    col.transform.InverseTransformPoint(fingertipPosition)))
                                            - fingertipPosition).magnitude;
                            }

                            UpdateFinger(finger.Type, hand.IsLeft, distance * 1500);//(600 / Vector3.Magnitude(transform.lossyScale)));
                        }
                    }
                }
            }
            else // If hand is not tracked, set all forces to 0
            {
                if (hand.Fingers[3].Type == Finger.FingerType.TYPE_RING) // If hand was at least once on the scene and fingers are properly initialized
                {
                    foreach (var finger in hand.Fingers)
                    {
                        UpdateFinger(finger.Type, hand.IsLeft, 0);
                    }
                }
            }
        }
    }