1

I have written a function to swap the rotations that are applied to mecanim bones; e.g. left and right hands:

private void swapLimbsRots(Quaternion[] rotationsArray)
{
    Quaternion bone1Rot = rotationsArray [(int)XsAnimationSegment.LeftHand];    // 16
    Quaternion bone2Rot = rotationsArray [(int)XsAnimationSegment.RightHand];   // 20

    Quaternion temp = bone1Rot;
    bone1Rot = bone2Rot;
    bone2Rot = temp;

    Debug.Log("I am called");
}

I only swap rotations, because I thought that would be sufficient. Is that right, or should I also swap positions?

Below, is my updating method, which I believe swaps the rotations for left and right hands, and then uses the new array of rotations to update my unity character:

void Update()
{
    Vector3[] latestPositions;
    Quaternion[] latestOrientations;

    if (mvnActors.getLatestPose(actorID, out latestPositions, out latestOrientations))
            {

    if (swapLimbs)
    {
        swapLimbsRots(latestOrientations);
    }

    updateMvnActor(currentPose, latestPositions, latestOrientations);
    updateModel(currentPose, targetModel);
}

Of course, my class variables contain the necessary declarations, as follows. This is why 16 and 20 are mentioned in the function:

public bool swapLimbs = false;              

private Dictionary<XsAnimationSegment, HumanBodyBones> mecanimBones;

        /// The 23 segments numbers for the animation. The first part of the dictionary pairs.
        public enum XsAnimationSegment
        {
            Pelvis = 0, // Hips

            RightUpperLeg = 1,
            RightLowerLeg = 2,
            RightFoot = 3,
            RightToe = 4,

            LeftUpperLeg = 5,
            LeftLowerLeg = 6,
            LeftFoot = 7,
            LeftToe = 8,

            L5 = 9,     // not used
            L3 = 10,    // Spine
            T12 = 11,   // not used
            T8 = 12,    // Chest

            LeftShoulder = 13,
            LeftUpperArm = 14,
            LeftLowerArm = 15,
            LeftHand = 16,

            RightShoulder = 17,
            RightUpperArm = 18,
            RightLowerArm = 19,
            RightHand = 20,

            Neck = 21,
            Head = 22
        }


        /// The segments order.
        int[] segmentOrder =
        {
                    (int)XsAnimationSegment.Pelvis,

                    (int)XsAnimationSegment.RightUpperLeg,
                    (int)XsAnimationSegment.RightLowerLeg,
                    (int)XsAnimationSegment.RightFoot,
                    (int)XsAnimationSegment.RightToe,

                    (int)XsAnimationSegment.LeftUpperLeg,
                    (int)XsAnimationSegment.LeftLowerLeg,
                    (int)XsAnimationSegment.LeftFoot,
                    (int)XsAnimationSegment.LeftToe,

                    (int)XsAnimationSegment.L5,
                    (int)XsAnimationSegment.L3,
                    (int)XsAnimationSegment.T12,
                    (int)XsAnimationSegment.T8,

                    (int)XsAnimationSegment.LeftShoulder,
                    (int)XsAnimationSegment.LeftUpperArm,
                    (int)XsAnimationSegment.LeftLowerArm,
                    (int)XsAnimationSegment.LeftHand,

                    (int)XsAnimationSegment.RightShoulder,
                    (int)XsAnimationSegment.RightUpperArm,
                    (int)XsAnimationSegment.RightLowerArm,
                    (int)XsAnimationSegment.RightHand,

                    (int)XsAnimationSegment.Neck,
                    (int)XsAnimationSegment.Head
        };


        /// Map the mecanim bones to xsens segments. 23 bones/segments
        protected void mapMecanimBones()
        {
            mecanimBones = new Dictionary<XsAnimationSegment, HumanBodyBones>();

            mecanimBones.Add(XsAnimationSegment.Pelvis,         HumanBodyBones.Hips);
            mecanimBones.Add(XsAnimationSegment.LeftUpperLeg,   HumanBodyBones.LeftUpperLeg);
            mecanimBones.Add(XsAnimationSegment.LeftLowerLeg,   HumanBodyBones.LeftLowerLeg);
            mecanimBones.Add(XsAnimationSegment.LeftFoot,       HumanBodyBones.LeftFoot);
            mecanimBones.Add(XsAnimationSegment.LeftToe,        HumanBodyBones.LeftToes);
            mecanimBones.Add(XsAnimationSegment.RightUpperLeg,  HumanBodyBones.RightUpperLeg);
            mecanimBones.Add(XsAnimationSegment.RightLowerLeg,  HumanBodyBones.RightLowerLeg);
            mecanimBones.Add(XsAnimationSegment.RightFoot,      HumanBodyBones.RightFoot);
            mecanimBones.Add(XsAnimationSegment.RightToe,       HumanBodyBones.RightToes);
            mecanimBones.Add(XsAnimationSegment.L5,             HumanBodyBones.LastBone);
            mecanimBones.Add(XsAnimationSegment.L3,             HumanBodyBones.Spine);
            mecanimBones.Add(XsAnimationSegment.T12,            HumanBodyBones.LastBone);
            mecanimBones.Add(XsAnimationSegment.T8,             HumanBodyBones.Chest);
            mecanimBones.Add(XsAnimationSegment.LeftShoulder,   HumanBodyBones.LeftShoulder);
            mecanimBones.Add(XsAnimationSegment.LeftUpperArm,   HumanBodyBones.LeftUpperArm);
            mecanimBones.Add(XsAnimationSegment.LeftLowerArm,   HumanBodyBones.LeftLowerArm);
            mecanimBones.Add(XsAnimationSegment.LeftHand,       HumanBodyBones.LeftHand);
            mecanimBones.Add(XsAnimationSegment.RightShoulder,  HumanBodyBones.RightShoulder);
            mecanimBones.Add(XsAnimationSegment.RightUpperArm,  HumanBodyBones.RightUpperArm);
            mecanimBones.Add(XsAnimationSegment.RightLowerArm,  HumanBodyBones.RightLowerArm);
            mecanimBones.Add(XsAnimationSegment.RightHand,      HumanBodyBones.RightHand);
            mecanimBones.Add(XsAnimationSegment.Neck,           HumanBodyBones.Neck);
            mecanimBones.Add(XsAnimationSegment.Head,           HumanBodyBones.Head);
        }

However, the left and right hands are animated in a way that is obvious the swapping does not take place. What am I missing or doing wrong?

1 Answers1

0

Animating does it's thing in update, if you want to change anything about it you should do it in a LateUpdate()

Just changing your Update() to a LateUpdate() should fix it;

HoloLady
  • 1,041
  • 1
  • 12
  • 28
  • Thank you. You are right, but in `LateUpdate()` I will not have `latestOrientations` which is the only parameter of my `swapLimbsRots` method... Are you suggesting I should call `mvnActors.getLatestPose(actorID, out latestPositions, out latestOrientations)` again in `LateUpdate`? –  Nov 25 '15 at 09:30
  • 1
    I'm suggesting you change Update() to LateUpdate(), if that doesn't work you could just make `latestOrientations` and `latestPositions` global in your class, and then just have `updateMvnActor(currentPose, latestPositions, latestOrientations); updateModel(currentPose, targetModel);` in `LateUpdate()` and leave the rest in update – HoloLady Nov 25 '15 at 09:46
  • Thank you. I made the 2 variables global. –  Nov 26 '15 at 09:17
  • Aren't calculation results of `Update` supposed to be available in `LateUpdate` now that I am dealing with global variables? My `latestOrientations` variable (which is an array of quaternions) receives data in `Update` but when I use `Debug.Log` to print just one element of it in `LateUpdate` I get the `NullReferenceException: Object reference not set to an instance of an object` error message. I am confused now! –  Nov 26 '15 at 09:51
  • What line triggers the NRE? – HoloLady Nov 26 '15 at 10:43
  • The `Debug.Log` line! –  Nov 26 '15 at 11:26