- Home /
Mirroring a Quaternion
So I am trying to make a custom animation system, and one thing I want to be able to do for convenience is to mirror a pose around the z and y axis.
I managed to get the positions to change correctly, but no matter what I use as a way to mirror a quaternion , it always looks twisted and contorted.
public void Mirror()
{
//Vector3 temp = transform.localScale;
//temp.x *= -1;
//transform.localScale = temp;
Vector3[] targetPos = new Vector3[16];
Vector3[] targetRot = new Vector3[16]; // I've tried making this a quaternion AND a vector3
for (int i = 0; i < bones.Length; i++)
{
GameObject bone = bones[i];
Vector3 localPos = bone.transform.position - baseTransform.transform.position;
Vector3 localRot = bone.transform.rotation.eulerAngles;
Debug.Log(localPos + " " + localRot);
localPos.x *= -1;
localRot.y *= -1;
//localRot.x *= -1;
//localRot.w *= -1;
Debug.Log(localPos + " " + localRot);
int other = i;
if (i > 3)
{
if (i % 2 == 0)
other++;
else
{
other--;
}
}
targetPos[i] = localPos;
targetRot[i] = localRot;
}
for (int i = 0; i < bones.Length; i++)
{
GameObject bone = bones[i];
bone.transform.position = baseTransform.transform.position + targetPos[i];
bone.transform.eulerAngles = targetRot[i]; //at one point I tried using Quaternion.Euler
}
}
// what the indices in the arrrays represent
public enum HumanoidSkeleton : int
{
hip = 0, chest, neck, head, upperArmL, upperArmR, lowerArmL, lowerArmR, handL, handR, upperLegL, upperLegR, lowerLegL, lowerLegR, footL, footR
}
Answer by MrNegaBlox · Jun 25, 2017 at 02:44 PM
Ok, I'm an idiot. The problem wasn't mirroring the quaternion. It was because I was misassigning changes. I had the "other" variable to make sure that the right joint get changed, but I never actually put it into use
targetPos[other] = localPos;// I was using "i" here instead of other
targetRot[other] = localRot;// I was using "i" here instead of other
Also, I ended up switching back to using Quaternions and multiplying the x and w components by -1 to mirror it.
Quaternion localRot = bone.transform.rotation;
localRot.x *= -1;
localRot.w *= -1;
That ended up making it work
Answer by aldonaletto · Jun 25, 2017 at 04:54 AM
Reversing the Euler angles doesn't produce the expected results (except in some very special cases): the Euler angles are applied successively in the order Z, X and Y, what may give weird results in many cases. A Quaternion represents a rotation of a given angle about an arbitrary axis, and is coded in a mathematically convenient way. You can get the axis and the angle with ToAngleAxis(), revert the angle or the axis and set it back to the rotation with AngleAxis() - like this:
...
float angle;
Vector3 axis;
bone.transform.rotation.ToAngleAxis(out angle, out axis); // get angle and axis
axis.x *= -1; // mirror the axis about the plane YZ
bone.transform.rotation = Quaternion.AngleAxis(angle, axis); // assign it back
...
Two things:
You defined angle as a float, but you use angle.x which won't work.
Second you don't need to initialize angle and axis since out parameters ensure that they are initialized within the method.
Also I'm not sure what the op meant by "mirroring an animation". What kind of model do we talk about? For example a humanoid animation can't be simply mirrored by mirroring the angles. That would make the left arm look to the right. Also the bone animations are animated in local space so flipping it in world space may give you strange results
Judging from the comments, I think he meant to do axis.x
Also, for your last point, That is what all those if statements are for. Its kinda wierd to explain but basically, that mirrors positions correctly, but not rotations.
Thanks for pointing out those errors: I actually wanted to mean axis.x, and just don't know why I've been thinking for years that out parameters should be initialized (maybe due to my prejudice against C#, which I'm always expecting to complain about anything...)
Your answer
![](https://koobas.hobune.stream/wayback/20220612124826im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can I make animations snap to a frame? 1 Answer
Quaternion issue: how can I fix this? 0 Answers
Rotating Armature During Animation 1 Answer
Ellipse Animation by code 2 Answers
Quaternion to matrix conv. error 0 Answers