[SOLVED]eulerRotations

I have a problem with euler rotations. I have some mocap equipment that is sending local euler angles for each joint of a skeleton.

I’m applying these angles to my rig using (C#):

leftShoulder.transform.localEulerAngles = new Vector3(MocapDataArray[12], mocapDataArray[14]*-1, mocapDataArray[13]);

As you can see, some slight manipulation to my incoming data must be applied (channel order and *-1).

At first this appeared to work but have noticed certain angles produce incorrect results, especially as an axis gets above 180 degrees (this is only true when using data from all XYZ channels simultaneously, if I only detect data from one channel (MocapDataArray[12],0,0) I can push above to 360 without any probs and get the right result). I’m, sure it’s the way that the three axes of orientation interact.

I have noticed in the inspector that if X is being manipulated then Y and Z will change automatically when x is between 90 270 degrees, maybe this is causing problems when I’m trying to set these variables to match the mocapDataArray on every frame?

Maybe I can go down the route of Quaternions but is this possible if the data is coming in as eulers? Can I apply a quat to a GameObject to set rot?

Many thanks, Jake.

I’m not an expert, but sounds like gimbal lock and, if so, you will want to use quaternions. And, as far as quaternions go, Unity uses them as the primary method of manipulation of rotation, not euler angles. As far as converting eulers to quaternions, I’ll leave that to someone who knows. :wink: I asked my father to explain quaternions to me once, and my brain melted after about a minute.

Yeah I think your right, it seems to be immitating the same gimble lock type effect i’ve seen when using 3DS Max. When moving my arm through a large, perfect circle I instead get an 8-like shape effect from my character’s arm in Unity.

I have tried using an ‘empty’ that is the parent of the arm. I can then rotate this enabling me to effectively change the rotation order the mocap data is applied, this seemed to help a bit but the problem is still there.

Looking through the API it seemed that the only way to directly set the rotation of a GameObject was to use EulerAngles or localEulerAngles (transform.rotate would effectively act like the speed of rotation but this is no good for me!). Can I set a GameObject to a particular Quat?

BTW I discovered this video that does a good job in explaining Quaternions in lehmans terms:

http://www.guerrillacg.org/home/3d-rigging/the-rotation-problem

You can access Quaternion values the same way you can Vectors (even though they recommend you don’t unless you know what you are doing).

Hey. Thanks for the link explaining quaternions. That made it much easier for me to understand. My father’s an aerospace engineer. I don’t think he has the ability to “dumb” down his language enough to my level to have explained it to me as easily.

Ok I solved the problem. Applying identical euler angles in different combinations (XYZ, YZX) results in a different result. It was a matter of making sure that the euler angles were applied in the correct order. To do this I would have to create an empty at the root of the joint, rotate this, create another empty that would apply the channels in the correct order (this took some exprimentation until the result was correct), then fix the original offset I gave the first empty:

//Gimbal lock corrections - We must apply channels in a specific order so Unity is not effected by gimbal lock.
    //GimbalFix Empty: Used to change the orientation of the parent of the current bone.
    //OrientFix Empty: Used to re-orientate the bone to its default rest position since GimbalFix has changed this orientation.
    public GameObject gimbalFix_Chest;
    public GameObject orientFix_Chest;

//CHEST
            gimbalFix_Chest.transform.localEulerAngles = new Vector3(0, 90, 90);
            orientFix_Chest.transform.localEulerAngles = new Vector3(floatArray[3], floatArray[4], floatArray[5]);
            spine.transform.localEulerAngles = new Vector3(-90, -90, 0);

I believe using Quats could have also solved this but I couldn’t work out how to convert and then apply.

A quick question though. I couldn’t work out how to create an empty at the exact same position/orientation at the root of each bone. I would thus right-click on a bone and select ‘Duplicate’. I’m sure this is wrong and not actually creating an empty but it works correctly. So, how do I create an empty at the root of a bone?

Thanks.