Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MrNegaBlox · Jun 24, 2017 at 07:05 PM · animationquaternion

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
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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
 ...


Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jun 25, 2017 at 07:55 AM 1
Share

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

avatar image MrNegaBlox Bunny83 · Jun 25, 2017 at 02:29 PM 0
Share

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.

avatar image aldonaletto Bunny83 · Jun 26, 2017 at 11:12 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

163 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges