// Use this for initialization
    void Start()
    {
        // Creates the App Finger objects for tracking fingers
        m_appFingers = new AppFinger[10];
        for (int i = 0; i < m_appFingers.Length; i++)
        {
            m_appFingers[i] = new AppFinger();
            m_appFingers[i].M_FingerObject = m_FingerObject;
            m_appFingers[i].initialize();
        }

        // Creates the app Hand objects for tracking hands
        m_appHands = new AppHand[2];
        for (int i = 0; i < m_appHands.Length; i++)
        {
            // Instantiates the object to represent the palms
            GameObject palmObject = (GameObject)Instantiate(m_PalmObject, Vector3.zero, Quaternion.identity);
            palmObject.transform.parent        = transform;
            palmObject.transform.localPosition = Vector3.zero;

            m_appHands[i] = new AppHand();
            m_appHands[i].M_HandPalmObject = palmObject;
            m_appHands[i].initialize();
        }
    }
    private AppFinger findAppFinger(Finger finger)
    {
        AppFinger result = null;

        for (int i = 0; i < NUM_FINGERS; i++)
        {
            if (mFingers[i].M_id == finger.Id)
            {
                return(mFingers[i]);
            }
        }

        return(result);
    }
    public override void initializeInputMethod()
    {
        base.initializeInputMethod();

        // Creates an array for storing data for the fingers
        m_Fingers = new AppFinger[NUM_FINGERS];

        // Initialize left finger
        m_Fingers[LEFT] = new AppFinger();
        m_Fingers[LEFT].M_FingerObject = mLeftFinger;
        m_Fingers[LEFT].initialize();


        // Initialize right finger
        m_Fingers[RIGHT] = new AppFinger();
        m_Fingers[RIGHT].M_FingerObject = mRightFinger;
        m_Fingers[RIGHT].initialize();
    }
Exemple #4
0
    /// <summary>
    /// Initializes the input method.
    /// </summary>
    public override void initializeInputMethod()
    {
        base.initializeInputMethod();

        mScreenWidth  = UnityEngine.Screen.width;
        mScreenHeight = UnityEngine.Screen.height;

        // Calculates a scale movement factor based on the screen resolution
        // to have a sensitive movement across all resolutions and screen sizes
        mScaleFactorX = Mathf.Min(mScreenWidth / 200, 32);
        mScaleFactorY = Mathf.Min(mScreenHeight / 160, 28);

        // Initialize right finger
        m_ActiveFinger = new AppFinger();
        m_ActiveFinger.M_FingerObject = fingerReferenceObject;
        m_ActiveFinger.initialize();

        Debug.Log("Initialize called");
    }
    /// <summary>
    /// Initializes the input method.
    /// </summary>
    public override void initializeInputMethod()
    {
        base.initializeInputMethod();

        mScreenWidth  = UnityEngine.Screen.width;
        mScreenHeight = UnityEngine.Screen.height;

        // Calculates a scale movement factor based on the screen resolution
        // to have a sensitive movement across all resolutions and screen sizes
        mScaleFactorX = Mathf.Min(mScreenWidth / 100, 16);
        mScaleFactorY = Mathf.Min(mScreenHeight / 80, 14);

        // Initialize right finger
        mFingers = new AppFinger[NUM_FINGERS];

        for (int i = 0; i < NUM_FINGERS; i++)
        {
            mFingers[i] = new AppFinger();
            mFingers[i].M_FingerObject = fingerObjects[i];
            mFingers[i].initialize();
        }
    }
    /// <summary>
    /// Updates the finger.
    /// </summary>
    /// <param name='finger'>
    /// Finger.
    /// </param>
    public void updateFingerPhysicsBased(AppFinger finger)
    {
        // checks if the finger has no RigidBody attached
        if (finger.M_FingerObject.GetComponentInChildren <Rigidbody>() == null)
        {
            // It adds a new RigidBody Object
            finger.M_FingerObject.AddComponent <Rigidbody>();
            finger.M_FingerObject.GetComponent <Rigidbody>().useGravity             = false;
            finger.M_FingerObject.GetComponent <Rigidbody>().mass                   = 5;
            finger.M_FingerObject.GetComponent <Rigidbody>().angularDrag            = 0;
            finger.M_FingerObject.GetComponent <Rigidbody>().collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
            finger.M_FingerObject.GetComponent <Rigidbody>().interpolation          = RigidbodyInterpolation.Interpolate;
        }


        // Kill any other motion from previous frame
        finger.M_FingerObject.GetComponentInChildren <Rigidbody>().angularVelocity = new Vector3(0, 0, 0);
        finger.M_FingerObject.GetComponentInChildren <Rigidbody>().velocity        = new Vector3(0, 0, 0);
        finger.M_FingerObject.transform.localRotation = Quaternion.identity;

        Vector3 impulseVector;

        // Gets the impulse vector
        impulseVector = ((finger.M_SmoothedPosition * finger.M_DataWeight - finger.M_FingerObject.transform.localPosition) * movementForce) * finger.M_DataWeight;

        // Applies a vertical smoothness Factor
        impulseVector.y *= verticalSmoothFactor;

        // Adds the impulse vector which actually moves the fingers
        finger.M_FingerObject.GetComponent <Rigidbody>().AddRelativeForce(impulseVector, ForceMode.Impulse);

        // Gets the fingers direction
        Vector3 vFingerDir = new Vector3(-finger.M_TipDirection.x, -finger.M_TipDirection.y, finger.M_TipDirection.z);

        // Applies the fingers direction to the finger object
        finger.M_FingerObject.transform.localRotation = Quaternion.FromToRotation(Vector3.back, vFingerDir);
    }
Exemple #7
0
    /// <summary>
    /// Processes the grab ungrabb logic.
    /// </summary>
    /// <param name='activeFinger'>
    /// Active finger.
    /// </param>
    private void processGrabUngrabb(AppFinger activeFinger)
    {
        Ray ray = new Ray(activeFinger.M_FingerObject.transform.position, transform.forward);

        if (Physics.Raycast(ray, out mHit, 10000))
        {
            if (mHit.collider.CompareTag("DRAGABLE") && activeFinger.M_TouchingObject == null && activeFinger.M_TipPosition.z < mGrabbingZ)
            {
                activeFinger.M_TouchingObject = mHit.collider.gameObject;
                activeFinger.M_TouchingObject.GetComponent <MeshRenderer>().renderer.material.color = Color.green;
            }
        }

        if (activeFinger.M_TipPosition.z > mUngrabbingZ && activeFinger.M_TouchingObject != null)
        {
            activeFinger.M_TouchingObject.GetComponent <MeshRenderer>().renderer.material.color = Color.white;
            activeFinger.M_TouchingObject = null;
        }

        if (activeFinger.M_TouchingObject != null)
        {
            activeFinger.M_TouchingObject.transform.position = Vector3.Lerp(activeFinger.M_TouchingObject.transform.position, new Vector3(activeFinger.M_FingerObject.transform.position.x, activeFinger.M_FingerObject.transform.position.y, activeFinger.M_TouchingObject.transform.position.z), Time.deltaTime * 10);
        }
    }
Exemple #8
0
 /// <summary>
 /// Updates the finger.
 /// </summary>
 /// <param name='activeFinger'>
 /// Active finger.
 /// </param>
 private void updateFinger(AppFinger activeFinger)
 {
     activeFinger.M_FingerObject.transform.localPosition = new Vector3(activeFinger.M_TipPosition.x * mScaleFactorX, (activeFinger.M_TipPosition.y - 150) * mScaleFactorY, 0);
 }
    public override void processLeapInput()
    {
        // Gets current frame Data
        Frame currentFrame = LeapInput.Frame;

        if (currentFrame.IsValid)
        {
            // Enforce left/right division
            Finger leftFinger  = findFinger(m_Fingers[LEFT].M_id, currentFrame);
            Finger rightFinger = findFinger(m_Fingers[RIGHT].M_id, currentFrame);

            if (leftFinger != null && rightFinger != null && leftFinger.TipPosition.x > rightFinger.TipPosition.x)
            {
                // Swap finger ids
                int tmp = m_Fingers[LEFT].M_id;
                m_Fingers[LEFT].M_id  = m_Fingers[RIGHT].M_id;
                m_Fingers[RIGHT].M_id = tmp;
            }

            // Reads input data from the leap device
            for (int fingerIndex = 0; fingerIndex < NUM_FINGERS; fingerIndex++)
            {
                AppFinger finger = m_Fingers[fingerIndex];

                // Attemps to find the current finger in the frame
                Finger currentLeapFinger = findFinger(finger.M_id, currentFrame);

                // If the finger was not found it tries to find a new one
                if (currentLeapFinger == null)
                {
                    // Decrements the data certainty
                    finger.M_DataWeight = Mathf.Max(finger.M_DataWeight - cDataRampSpeed * Time.deltaTime, 0.0f);

                    FingerList fingers = currentFrame.Fingers;

                    for (int j = 0; j < fingers.Count; j++)
                    {
                        if (fingers[j].Id != m_Fingers[(fingerIndex + 1) % NUM_FINGERS].M_id && fingers[j].IsValid)
                        {
                            currentLeapFinger = fingers[j];
                            j = fingers.Count;
                        }
                    }
                }


                // Updates our data if we found a finger
                if (currentLeapFinger != null)
                {
                    // Increments the data certainty
                    finger.M_DataWeight = Mathf.Min(finger.M_DataWeight + cDataRampSpeed * Time.deltaTime, 1.0f);

                    Vector v = currentLeapFinger.TipPosition;

                    finger.M_TipDirection = new Vector3(currentLeapFinger.Direction.x, currentLeapFinger.Direction.y, currentLeapFinger.Direction.z);

                    finger.M_TipPosition = new Vector3((float)v.x, (float)v.y + cLeapInputYOffset, -((float)v.z + cLeapInputZOffset) * 2) * cWorldScale * cLeapInputScale;

                    finger.M_id = currentLeapFinger.Id;
                }

                // Updates finger color
                Renderer[] rendererInFingers = finger.M_FingerObject.GetComponentsInChildren <Renderer>();

                foreach (Renderer render in rendererInFingers)
                {
                    render.material.color = Color.Lerp(Color.red, Color.green, finger.M_DataWeight);
                }
            }

            // Updates finger smoothed position
            for (int fingerIndex = 0; fingerIndex < NUM_FINGERS; fingerIndex++)
            {
                m_Fingers[fingerIndex].M_SmoothedPosition = m_Fingers[fingerIndex].M_SmoothedPosition +
                                                            kFingerSmoothing * (m_Fingers[fingerIndex].M_TipPosition - m_Fingers[fingerIndex].M_SmoothedPosition);
            }
        }
    }
    /// <summary>
    /// Processes the leap input.
    /// </summary>
    public override void processLeapInput()
    {
        // Gets the current frame leap data
        Frame m_currentFrame = LeapInput.Frame;

        // Checks that the current frame is valid
        if (!m_currentFrame.IsValid)
        {
            return;
        }

        // Gets a list of fingers
        FingerList m_fingersList = m_currentFrame.Fingers;

        // Initialize the fingers found state in the frame
        for (int i = 0; i < NUM_FINGERS; i++)
        {
            mFingers[i].M_Tracked = false;
        }

        // iterates all the fingers on the frame
        for (int i = 0; i < m_fingersList.Count; i++)
        {
            // Searches if the finger of the frame is already being tracked
            AppFinger finger = findAppFinger(m_fingersList[i]);

            // If it's a new finger
            if (finger == null)
            {
                // Gets the first free finger
                for (int j = 0; j < NUM_FINGERS; j++)
                {
                    if (mFingers[j].M_DataWeight == 0 && !mFingers[j].M_Tracked)
                    {
                        mFingers[j].M_id           = m_fingersList[i].Id;
                        mFingers[j].M_TipDirection = Vector3.zero;
                        mFingers[j].M_TipPosition  = Vector3.zero;
                        mFingers[j].M_TipVelocity  = Vector3.zero;

                        mFingers[j].M_SmoothedVelocity  = mFingers[j].M_SmoothedVelocity + smoothFactor * (mFingers[j].M_TipVelocity - mFingers[j].M_SmoothedVelocity);
                        mFingers[j].M_SmoothedDirection = mFingers[j].M_SmoothedDirection + smoothFactor * (mFingers[j].M_TipDirection - mFingers[j].M_SmoothedDirection);
                        mFingers[j].M_SmoothedPosition  = mFingers[j].M_SmoothedPosition + smoothFactor * (mFingers[j].M_TipPosition - mFingers[j].M_SmoothedPosition);

                        mFingers[j].M_Tracked = true;
                        j = NUM_FINGERS;
                    }
                }

                // If the finger was already being tracked
            }
            else
            {
                for (int j = 0; j < NUM_FINGERS; j++)
                {
                    // Gets the tracked finger
                    if (mFingers[j].M_id == finger.M_id)
                    {
                        mFingers[j].M_id           = m_fingersList[i].Id;
                        mFingers[j].M_TipDirection = new Vector3(m_fingersList[i].Direction.x, m_fingersList[i].Direction.y, m_fingersList[i].Direction.z);
                        mFingers[j].M_TipPosition  = new Vector3(m_fingersList[i].TipPosition.x, m_fingersList[i].TipPosition.y, m_fingersList[i].TipPosition.z);
                        mFingers[j].M_TipVelocity  = new Vector3(m_fingersList[i].TipVelocity.x, m_fingersList[i].TipVelocity.y, m_fingersList[i].TipVelocity.z);

                        mFingers[j].M_SmoothedVelocity  = mFingers[j].M_SmoothedVelocity + smoothFactor * (mFingers[j].M_TipVelocity - mFingers[j].M_SmoothedVelocity);
                        mFingers[j].M_SmoothedDirection = mFingers[j].M_SmoothedDirection + smoothFactor * (mFingers[j].M_TipDirection - mFingers[j].M_SmoothedDirection);
                        mFingers[j].M_SmoothedPosition  = mFingers[j].M_SmoothedPosition + smoothFactor * (mFingers[j].M_TipPosition - mFingers[j].M_SmoothedPosition);

                        mFingers[j].M_Tracked = true;
                        j = NUM_FINGERS;
                    }
                }
            }
        }

        // Iterates all the app fingers
        for (int i = 0; i < NUM_FINGERS; i++)
        {
            // If the finger has been found it increments the data weight
            if (mFingers[i].M_Tracked)
            {
                mFingers[i].M_DataWeight = (Mathf.Min(mFingers[i].M_DataWeight + smoothFactor * Time.deltaTime, 1.0f));
            }
            else
            {
                // If not it decrements the data weight
                mFingers[i].M_DataWeight        = (Mathf.Max(mFingers[i].M_DataWeight - smoothFactor * Time.deltaTime, 0.0f));
                mFingers[i].M_SmoothedPosition -= ((mFingers[i].M_SmoothedPosition - mFingers[i].M_SmoothedPosition / 1.2f) / 100.0f);
            }
        }
    }