コード例 #1
0
    // Update the information of every body joint for a given body
    private void RefreshBodyObject(Kinect.Body body, GameObject bodyObject)
    {
        BodyAnalysis.SetMapper(bodyReader.GetMapper());

        // Get the body joints and convert them to a 2D representation using the scale of the
        // current object. For AR purposes
        Vector2[] bodyJoints2D = BodyAnalysis.convertToUnity2DPosition(body.Joints, scale, position);
        // Get the body joint orientation and convert them to a Unity Quaternion type
        Quaternion[] bodyOrientation = BodyAnalysis.convertToUnityOrientation(body.JointOrientations);

        int i = 0;

        foreach (Transform child in bodyObject.transform)
        {
            // If the current joint is not being tracked propperly (i.e. infinity as position),
            // ignore it
            if ((bodyJoints2D[i].x == Mathf.Infinity || bodyJoints2D[i].x == Mathf.NegativeInfinity) ||
                (bodyJoints2D[i].y == Mathf.Infinity || bodyJoints2D[i].y == Mathf.NegativeInfinity))
            {
                i++;
                if (i >= bodyJoints2D.Length)
                {
                    break;
                }
                continue;
            }
            // Update the current joint position and rotation.
            child.transform.position      = new Vector3(bodyJoints2D[i].x, bodyJoints2D[i].y, position.z);
            child.transform.localRotation = bodyOrientation[i];

            i++;
            if (i >= bodyJoints2D.Length)
            {
                break;
            }
        }

        Transform spineBox = bodyObject.transform.Find("Spine angles");

        spineBox.position = new Vector3(bodyJoints2D[(int)Kinect.JointType.Head].x, bodyJoints2D[(int)Kinect.JointType.Head].y, position.z - 0.5f);
        int spineFlex1 = (int)BodyAnalysis.FlexionSpineAngle(body, true);  // coronal
        int spineFlex2 = (int)BodyAnalysis.FlexionSpineAngle(body, false); // sagital

        spineBox.GetChild(0).GetComponent <TextMesh>().text = "Coronal: " + spineFlex1.ToString() + "\nSagital:" + spineFlex2.ToString();

        Transform rightShoulderBox = bodyObject.transform.Find("Right shoulder angles");

        rightShoulderBox.position = new Vector3(bodyJoints2D[(int)Kinect.JointType.ShoulderRight].x, bodyJoints2D[(int)Kinect.JointType.ShoulderRight].y, position.z - 0.5f);
        int rShoulderAngle1 = (int)BodyAnalysis.FlexionShoulderAngle(body, true);
        int rShoulderAngle2 = (int)BodyAnalysis.AbductionShoulderAngle(body, true);

        rightShoulderBox.GetChild(0).GetComponent <TextMesh>().text = "Flexion: " + rShoulderAngle1.ToString() + "\nAbduction: " + rShoulderAngle2.ToString();
    }
コード例 #2
0
    // Update the information of every body joint for a given body
    private void RefreshBodyObject(Kinect.Body body, GameObject bodyObject)
    {
        BodyAnalysis.SetMapper(bodyReader.GetMapper());

        // Get the body joints and convert them to a 2D representation using the scale of the
        // current object. For AR purposes
        Vector2[] bodyJoints2D = BodyAnalysis.convertToUnity2DPosition(body.Joints, scale, position);
        // Get the body joint orientation and convert them to a Unity Quaternion type
        Quaternion[] bodyOrientation = BodyAnalysis.convertToUnityOrientation(body.JointOrientations);

        int i = 0;

        foreach (Transform child in bodyObject.transform)
        {
            // If the current joint is not being tracked propperly (i.e. infinity as position),
            // ignore it
            if ((bodyJoints2D[i].x == Mathf.Infinity || bodyJoints2D[i].x == Mathf.NegativeInfinity) ||
                (bodyJoints2D[i].y == Mathf.Infinity || bodyJoints2D[i].y == Mathf.NegativeInfinity))
            {
                i++;
                if (i >= bodyJoints2D.Length)
                {
                    break;
                }
                continue;
            }
            // Update the current joint position and rotation.
            child.transform.position      = new Vector3(bodyJoints2D[i].x, bodyJoints2D[i].y, position.z);
            child.transform.localRotation = bodyOrientation[i];

            i++;
            if (i >= bodyJoints2D.Length)
            {
                break;
            }
        }
    }
コード例 #3
0
    // Update the information regarding one body
    private void RefreshBodyObject(Kinect.Body body, GameObject bodyObject)
    {
        BodyAnalysis.SetMapper(bodyReader.GetMapper());

        Vector2[]    bodyJoints2D    = BodyAnalysis.convertToUnity2DPosition(body.Joints, scale, position);
        Quaternion[] bodyOrientation = BodyAnalysis.convertToUnityOrientation(body.JointOrientations);

        // ---------------------------------------------------------------------------------
        // Show/hide the marker for each hand. These two lines are added to this function
        // only for testing (debugging) puposes. Remove for deployment
        bodyObject.transform.GetChild(0).gameObject.SetActive(trackRightHand);
        bodyObject.transform.GetChild(1).gameObject.SetActive(trackLeftHand);
        // ---------------------------------------------------------------------------------

        // HAND TRACKING
        if (trackRightHand)
        {
            Vector2 rightHandPos = bodyJoints2D[(int)Kinect.JointType.HandRight];
            if ((rightHandPos.x != Mathf.Infinity && rightHandPos.x != Mathf.NegativeInfinity) &&
                (rightHandPos.y != Mathf.Infinity && rightHandPos.y != Mathf.NegativeInfinity))
            {
                Transform hand = bodyObject.transform.GetChild(0).transform;
                hand.position = new Vector3(rightHandPos.x, rightHandPos.y, position.z);
            }
        }
        if (trackLeftHand)
        {
            Vector2 leftHandPos = bodyJoints2D[(int)Kinect.JointType.HandLeft];
            if ((leftHandPos.x != Mathf.Infinity && leftHandPos.x != Mathf.NegativeInfinity) &&
                (leftHandPos.y != Mathf.Infinity && leftHandPos.y != Mathf.NegativeInfinity))
            {
                Transform hand = bodyObject.transform.GetChild(1).transform;
                hand.position = new Vector3(leftHandPos.x, leftHandPos.y, position.z);
            }
        }
    }
コード例 #4
0
    // Update the information of every body joint for a given body
    private void RefreshBodyObject(Kinect.Body body, GameObject bodyObject)
    {
        BodyAnalysis.SetMapper(bodyReader.GetMapper());

        // Get the body joints and convert them to a 2D representation using the scale of the
        // current object. For AR purposes
        Vector2[] bodyJoints2D = BodyAnalysis.convertToUnity2DPosition(body.Joints, scale, position);

        // Get the Abduction/Adduction angle of the shoulders.
        Quaternion[] jointOrientation   = BodyAnalysis.convertToUnityOrientation(body.JointOrientations);
        float        leftShoulderAngle  = BodyAnalysis.AbductionShoulderAngle(body, false);
        float        rightShoulderAngle = BodyAnalysis.AbductionShoulderAngle(body, true);

        // Get the flexion of the elbows. The shoulder Abduction/Adduction should be performed
        // without bending the arm.
        float       leftElbowAngle  = BodyAnalysis.FlexionElbowAngle(body, false) * 180.0f / Mathf.PI;
        float       rightElbowAngle = BodyAnalysis.FlexionElbowAngle(body, true) * 180.0f / Mathf.PI;
        const float minAngle        = 30;

        // For each desired joint, find its position in 2D space, check if that position
        // is valid (not infinity), and update the position of the corresponding marker.
        // The marker on the elbows is only visible when the angle is higher than a
        // threshold (minAngle)

        // ---> LEFT
        // Left shoulder
        Vector2   pos   = bodyJoints2D[(int)Kinect.JointType.ShoulderLeft];
        Transform joint = bodyObject.transform.Find("Left shoulder");

        if (pos.x != Mathf.Infinity && pos.x != Mathf.NegativeInfinity &&
            pos.y != Mathf.Infinity && pos.y != Mathf.NegativeInfinity)
        {
            joint.position = new Vector3(pos.x, pos.y, position.z - 0.5f);
        }

        // Left text box
        Transform box = bodyObject.transform.Find("Left text");

        box.transform.position = new Vector3(pos.x - 2, pos.y, position.x - 0.5f);
        box.GetChild(0).GetComponent <TextMesh>().text = "Angle: " + leftShoulderAngle.ToString();

        // Left elbow
        pos   = bodyJoints2D[(int)Kinect.JointType.ElbowLeft];
        joint = bodyObject.transform.Find("Left elbow");
        if (pos.x != Mathf.Infinity && pos.x != Mathf.NegativeInfinity &&
            pos.y != Mathf.Infinity && pos.y != Mathf.NegativeInfinity)
        {
            joint.gameObject.SetActive(leftElbowAngle > minAngle);
            joint.position = new Vector3(pos.x, pos.y, position.z - 0.5f);
        }

        // Left hand
        pos   = bodyJoints2D[(int)Kinect.JointType.HandLeft];
        joint = bodyObject.transform.Find("Left hand");
        if (pos.x != Mathf.Infinity && pos.x != Mathf.NegativeInfinity &&
            pos.y != Mathf.Infinity && pos.y != Mathf.NegativeInfinity)
        {
            joint.position = new Vector3(pos.x, pos.y, position.z - 0.5f);
        }

        // ---> RIGHT
        // Right shoulder
        pos   = bodyJoints2D[(int)Kinect.JointType.ShoulderRight];
        joint = bodyObject.transform.Find("Right shoulder");
        if (pos.x != Mathf.Infinity && pos.x != Mathf.NegativeInfinity &&
            pos.y != Mathf.Infinity && pos.y != Mathf.NegativeInfinity)
        {
            joint.position = new Vector3(pos.x, pos.y, position.z - 0.5f);
        }

        // Right text box
        box = bodyObject.transform.Find("Right text");
        box.transform.position = new Vector3(pos.x + 2, pos.y, position.x - 0.5f);
        box.GetChild(0).GetComponent <TextMesh>().text = "Angle: " + rightShoulderAngle.ToString();

        // Right elbow
        pos   = bodyJoints2D[(int)Kinect.JointType.ElbowRight];
        joint = bodyObject.transform.Find("Right elbow");
        if (pos.x != Mathf.Infinity && pos.x != Mathf.NegativeInfinity &&
            pos.y != Mathf.Infinity && pos.y != Mathf.NegativeInfinity)
        {
            joint.gameObject.SetActive(rightElbowAngle > minAngle);
            joint.position = new Vector3(pos.x, pos.y, position.z - 0.5f);
        }

        // Right hand
        pos   = bodyJoints2D[(int)Kinect.JointType.HandRight];
        joint = bodyObject.transform.Find("Right hand");
        if (pos.x != Mathf.Infinity && pos.x != Mathf.NegativeInfinity &&
            pos.y != Mathf.Infinity && pos.y != Mathf.NegativeInfinity)
        {
            joint.position = new Vector3(pos.x, pos.y, position.z - 0.5f);
        }
    }