/// @brief updates the skeleton
    ///
    /// This method is responsible for updating the skeleton based on new information.
    /// It is called by an external manager with the correct user which controls this specific skeleton.
    /// @param userID the user id of the user which controls us. @note this is the NI user ID!
    /// @param skeleton the skeleton object to use.
    /// @param centerOffset the offset we should use on the center (when moving the root).
    /// This is usually the starting position (so the skeleton will not "jump" when doing the first update
    public void UpdateSkeleton(int userID, NIUserAndSkeleton skeleton, Vector3 centerOffset)
    {
        if (skeleton.Valid == false)
        {
            return; // no skeleton
        }
        if (skeleton.GetUserCalibrationState(userID) != NIUserAndSkeleton.CalibrationState.calibrated)
        {
            return; // we are not calibrated.
        }
        if (skeleton.Skeleton.IsTracking(userID) == false)
        {
            return; // we are not tracking
        }
        // we use the  torso as root
        SkeletonJointTransformation skelTrans = skeleton.Skeleton.GetSkeletonJoint(userID, SkeletonJoint.Torso);

        UpdateRoot(skelTrans.Position, centerOffset);

        // update each joint with data from OpenNI
        foreach (SkeletonJoint joint in Enum.GetValues(typeof(SkeletonJoint)))
        {
            if (skeleton.Skeleton.IsJointAvailable(joint))
            {
                SkeletonJointTransformation skelTransJoint = skeleton.Skeleton.GetSkeletonJoint(userID, joint);
                UpdateJoint(joint, skelTransJoint, skelTrans.Position);
            }
        }
    }
Beispiel #2
0
        /// @brief initializes the class with a new user.
        /// 
        /// initializes the class with a new user (The class is invalid (m_uniqueID<0) until this is done successfully)
        /// @param usersGenerator the users generator which holds the users.
        /// @param uniqueID the uniqueID of the user we should initialize to.
        /// @return true on success and false on failure (making the object invalid, i.e. m_uniqueID<0).
        public bool InitInternalSkeleton(NIUserAndSkeleton usersGenerator, int uniqueID)
        {
            m_uniqueID = -1; // to make sure we are invalid until we are sure we are valid.
            if (usersGenerator.Valid == false)
                return false; // must have a valid users generator to do anything
            m_userID = usersGenerator.GetNIUserId(uniqueID);
            if (m_userID < 0)
                return false; // means the uniqueID was illegal
            if (usersGenerator.GetUserCalibrationState(m_userID) != NIUserAndSkeleton.CalibrationState.calibrated)
                return false; // we only initialize calibrated users as we need the skeleton.           
            int numJoints = 0; // holds the place to position the next joint.
            foreach (SkeletonJoint joint in Enum.GetValues(typeof(SkeletonJoint)))
            {
                if (usersGenerator.Skeleton.IsJointAvailable(joint)==false)
                    continue; // an irrelevant joint.
                // we need to add the joint but we don't want to create a new one unless we need to.
                if (numJoints < m_jointsIDs.Count)
                {
                    m_jointsIDs[numJoints] = joint;
                }
                else
                {
                    m_jointsIDs.Add(joint);
                }

                // we need to add the joint transformation but we don't want to create a new one unless we need to.
                if(numJoints<m_jointsTrans.Count)
                {
                    m_jointsTrans[numJoints]=usersGenerator.Skeleton.GetSkeletonJoint(m_userID, joint);
                }
                else
                {
                    SkeletonJointTransformation skelTrans = usersGenerator.Skeleton.GetSkeletonJoint(m_userID, joint);
                    m_jointsTrans.Add(skelTrans);
                }
                numJoints++;
            }
            if (numJoints < m_jointsIDs.Count)
            {
                for (; numJoints < m_jointsIDs.Count; numJoints++)
                    m_jointsIDs[numJoints] = SkeletonJoint.Invalid;
            }
            m_uniqueID = uniqueID;
            return true;
        }