amirf8
amirf8

Reputation: 5

kinect sdk 2.0 joint angles and tracking

How to check whether the joints you are accessing have a tracking state of Tracked. I am finding the angles of 8 of the joints and I can't seem to get the result to be displayed on my screen,

public double AngleBetweenTwoVectors(Vector3D vectorA, Vector3D vectorB)
    {
        double dotProduct = 0.0;
        vectorA.Normalize();
        vectorB.Normalize();


        dotProduct = Vector3D.DotProduct(vectorA, vectorB);

        return (double)Math.Acos(dotProduct) / Math.PI * 180;
    }

    public double[] GetVector(Body skeleton)
    {

        Vector3D Shoulder = new Vector3D(skeleton.Joints[JointType.SpineShoulder].Position.X, skeleton.Joints[JointType.SpineShoulder].Position.Y, skeleton.Joints[JointType.SpineShoulder].Position.Z);
        Vector3D RShoulder = new Vector3D(skeleton.Joints[JointType.ShoulderRight].Position.X, skeleton.Joints[JointType.ShoulderRight].Position.Y, skeleton.Joints[JointType.ShoulderRight].Position.Z);
        Vector3D LShoulder = new Vector3D(skeleton.Joints[JointType.ShoulderLeft].Position.X, skeleton.Joints[JointType.ShoulderLeft].Position.Y, skeleton.Joints[JointType.ShoulderLeft].Position.Z);
        Vector3D RElbow = new Vector3D(skeleton.Joints[JointType.ElbowRight].Position.X, skeleton.Joints[JointType.ElbowRight].Position.Y, skeleton.Joints[JointType.ElbowRight].Position.Z);
        Vector3D LElbow = new Vector3D(skeleton.Joints[JointType.ElbowLeft].Position.X, skeleton.Joints[JointType.ElbowLeft].Position.Y, skeleton.Joints[JointType.ElbowLeft].Position.Z);
        Vector3D RWrist = new Vector3D(skeleton.Joints[JointType.WristRight].Position.X, skeleton.Joints[JointType.WristRight].Position.Y, skeleton.Joints[JointType.WristRight].Position.Z);
        Vector3D LWrist = new Vector3D(skeleton.Joints[JointType.WristLeft].Position.X, skeleton.Joints[JointType.WristLeft].Position.Y, skeleton.Joints[JointType.WristLeft].Position.Z);
        Vector3D RKnee = new Vector3D(skeleton.Joints[JointType.KneeRight].Position.X, skeleton.Joints[JointType.KneeRight].Position.Y, skeleton.Joints[JointType.KneeRight].Position.Z);
        Vector3D LKnee = new Vector3D(skeleton.Joints[JointType.KneeLeft].Position.X, skeleton.Joints[JointType.KneeLeft].Position.Y, skeleton.Joints[JointType.KneeLeft].Position.Z);
        Vector3D RAnkle = new Vector3D(skeleton.Joints[JointType.AnkleRight].Position.X, skeleton.Joints[JointType.AnkleRight].Position.Y, skeleton.Joints[JointType.AnkleRight].Position.Z);
        Vector3D LAnkle = new Vector3D(skeleton.Joints[JointType.AnkleLeft].Position.X, skeleton.Joints[JointType.AnkleLeft].Position.Y, skeleton.Joints[JointType.AnkleLeft].Position.Z);
        Vector3D Hip = new Vector3D(skeleton.Joints[JointType.SpineBase].Position.X, skeleton.Joints[JointType.SpineBase].Position.Y, skeleton.Joints[JointType.SpineBase].Position.Z);
        Vector3D RHip = new Vector3D(skeleton.Joints[JointType.HipRight].Position.X, skeleton.Joints[JointType.HipRight].Position.Y, skeleton.Joints[JointType.HipRight].Position.Z);
        Vector3D LHip = new Vector3D(skeleton.Joints[JointType.HipLeft].Position.X, skeleton.Joints[JointType.HipLeft].Position.Y, skeleton.Joints[JointType.HipLeft].Position.Z);


        double AngleRElbow = AngleBetweenTwoVectors(RElbow - RShoulder, RElbow - RWrist);
        double AngleRShoulder = AngleBetweenTwoVectors(RShoulder - Shoulder, RShoulder - RElbow);
        double AngleLElbow = AngleBetweenTwoVectors(LElbow - LShoulder, LElbow - LWrist);
        double AngleLShoulder = AngleBetweenTwoVectors(LShoulder - Shoulder, LShoulder - LElbow);
        double AngleRKnee = AngleBetweenTwoVectors(RKnee - RHip, RKnee - RAnkle);
        double AngleLKnee = AngleBetweenTwoVectors(LKnee - LHip, LKnee - LAnkle);
        double AngleRHip = AngleBetweenTwoVectors(RHip - Hip, RHip - RKnee);
        double AngleLHip = AngleBetweenTwoVectors(LHip - Hip, LHip - LKnee);


        Results.Add(AngleLShoulder);
        Results.Add(AngleLElbow);
        Results.Add(AngleLKnee);
        Results.Add(AngleLHip);
        Results.Add(AngleRShoulder);
        Results.Add(AngleRElbow);
        Results.Add(AngleRKnee);
        Results.Add(AngleRHip);

        double[] resultsArray = Results.ToArray();

        return resultsArray;

    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        result1.Text = Results[4].ToString();
        result2.Text = Results[5].ToString();
        result3.Text = Results[6].ToString();
        result4.Text = Results[7].ToString();
        result5.Text = Results[0].ToString();
        result6.Text = Results[1].ToString();
        result7.Text = Results[2].ToString();
        result8.Text = Results[3].ToString();
    }

There is live feed coming from the kinect to the program, but when the button is pressed, there is a null pointer exception thrown for some reason. I am thinking it is because i am not sure if the joints are tracked. How do I go about doing that?

Also is my code to display the angles correct? I am using textbox for each result.

Upvotes: 0

Views: 3195

Answers (1)

jubueche
jubueche

Reputation: 793

foreach (Joint joint in skeleton.Joints)
            {
                if (joint.TrackingState == JointTrackingState.Tracked)
                {
                    //Do something                    
                }
                else if (joint.TrackingState == JointTrackingState.Inferred)
                {
                    //Do something else                   
                }
            }

This loops through the Joints array and checks for every Joint if the JointTrackingState is Tracked, Inferred or NotTracked, see here. The program to calculate the angles is perfectly fine.

If you have more questions on how to calculate the angle of Joints see here.

If you want a working programm, I'll send it to you per e-mail. Or see here. Hope this helps :)

Upvotes: 1

Related Questions