What is the correct way to make a character turn his head and spine via code?
I use code like this to set a new transformation matrix on a bone:
bonePose.DefaultTransform = twist * Matrix.CreateTranslation(bonePose.DefaultTransform.Translation);
I only want to affect rotation about one axis (yaw). But the problem with this code is that it only preserves the translation of the bone. It overwrites any pitch (head up/down) rotation.
To compensate, I try to re-create the original pitch rotation:
Vector3 rollAxis = new Vector3(0, 1, 0);//positive is dip right wing
Vector3 yawAxis = new Vector3(1, 0, 0);//positive is bearing left
Vector3 pitchAxis = new Vector3(0, 0, 1);//positive is dive
twist = Matrix.Identity;
twist *= Matrix.CreateFromAxisAngle(yawAxis, boneAngle);
twist *= Matrix.CreateFromAxisAngle(pitchAxis, pitchForward);
But this distorts the character spine somewhat, because my approximation is not close enough to the original model pose.
Is there a better way?