Search code examples
kinectkinect-sdk

Exception understood on update Kinect Joint positions


I am doing a very simple stuff, my goal is to move one skeleton based on the position of the other skeleton, for this i am based myself on a HipCenter position. (This algoritm could be wrong, this question is about a exception ocurring in the foreach loop)

Here is my actual code:

public static Skeleton MoveTo(this Skeleton skOrigin, Skeleton skDestiny)
{
     Skeleton skReturn = skOrigin; // just making a copy

        // find the factor to move, based on the HipCenter.
        float whatToMultiplyX = skOrigin.Joints[JointType.HipCenter].Position.X / skDestiny.Joints[JointType.HipCenter].Position.X;
        float whatToMultiplyY = skOrigin.Joints[JointType.HipCenter].Position.Y / skDestiny.Joints[JointType.HipCenter].Position.Y;
        float whatToMultiplyZ = skOrigin.Joints[JointType.HipCenter].Position.Z / skDestiny.Joints[JointType.HipCenter].Position.Z;


        SkeletonPoint movedPosition = new SkeletonPoint();
        Joint movedJoint = new Joint();
        foreach (JointType item in Enum.GetValues(typeof(JointType)))
        {
            // Updating the position
            movedPosition.X = skOrigin.Joints[item].Position.X * whatToMultiplyX;
            movedPosition.Y = skOrigin.Joints[item].Position.Y * whatToMultiplyY;
            movedPosition.Z = skOrigin.Joints[item].Position.Z * whatToMultiplyZ;

            // Setting the updated position to the skeleton that will be returned.
            movedJoint.Position = movedPosition;
            skReturn.Joints[item] = movedJoint;
        }

        return skReturn;
    }

Using F10 to debug everything works fine ultin the second pass in te foreach loop. When i am passing for the second time in the foreach i get a exception on this line

skReturn.Joints[item] = movedJoint;

The exception says:

JointType index value must match Joint.JointType 

But the value is actualy the Spine.

Whats wrong?


Solution

  • Solved, here is the solution

     Joint newJoint = new Joint(); // declare a new Joint
    
    // Iterate in the 20 Joints
    foreach (JointType item in Enum.GetValues(typeof(JointType)))
    {
        newJoint = skToBeMoved.Joints[item];
    
                // applying the new values to the joint
                SkeletonPoint pos = new SkeletonPoint()
                {
                    X = (float)(newJoint.Position.X + (someNumber)),
                    Y = (float)(newJoint.Position.Y + (someNumber)),
                    Z = (float)(newJoint.Position.Z + (someNumber))
                };
    
                newJoint.Position = pos;
                skToBeChanged.Joints[item] = newJoint;
            }
    

    This will work.