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
1
Question by elder0010 · Feb 20, 2016 at 12:41 PM · gyroscopequaternionsrotations

Invert quaternion rotation

Hi, i am having issues with quaternions rotations.

I am getting a quaternion from my Arduino board and trying to rotate an object, in this way:

     Quaternion q = arduino.getQuaternion ();
     transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * arduino.getEasingValue());

But the rotation is not coherent with the current gyro position (the axis are swapped). I can't change the physical orientation of the chip, but i tried to realign the axis using a Vector3, like that:

 Quaternion q = arduino.getQuaternion ();
 transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(q.eulerAngles.y, -q.eulerAngles.z, -q.eulerAngles.x), Time.deltaTime * arduino.getEasingValue());

Now the rotation is coherent, but i have gimbal lock issues. Is there a way to obtain the same results without using EulerAngles? I need to rotate on x by using the y value, on x using the -z, on z using the -x. I guess i have to use Quaternion.AngleAxis, but i am not getting how to do it properly. Thank you!

Comment
Add comment · Show 4
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 Owen-Reynolds · Feb 20, 2016 at 03:44 PM 0
Share

$$anonymous$$aybe try turning the Quaternion into a Vector3, fixing that for the wrong axis and converting back to a Quat? That would lose information about local spin, but I don't think arduino gives you that anyway -- it's probably just giving you a local UP (or whatever) as a quaternion.

Or, this sounds like a solved problem somewhere in a math forum. Changing axis has got to be common.

avatar image elder0010 · Feb 20, 2016 at 05:32 PM 0
Share

that's what i have tried to do as you can see in the code, but i experience gymbal lock! And yes, my arduino gives me a correct quaternion (not a converted vector3!)

avatar image Bunny83 elder0010 · Feb 20, 2016 at 07:21 PM 0
Share

No, he means a quaternion can be expressed as two direction vectors, usually "forward" and "up". If you multiply Vector3.forward with your quaternion you get the rotated forward vector. Doing the same with Vector3.up will give you the up vector.

Now you should fix the vector representation probably by rotating and / or mirroring the vectors at certain axis. Finally you just have to use Quaternion.LookRotation to convert the forward and up vector back to a quaternion.

avatar image elder0010 Bunny83 · Feb 20, 2016 at 11:22 PM 0
Share

and this approach will not be affected by gymbal lock?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Eno-Khaon · Feb 20, 2016 at 07:42 PM

I would suggest utilizing an extra empty object to convert your rotations accurately.

Namely, use your first example to rotate an empty object, to get proper orientation to prepare to convert to a new set of axes.

 Quaternion q = arduino.getQuaternion ();
 emptyObject.rotation = Quaternion.Slerp(emptyObject.rotation, q, Time.deltaTime * arduino.getEasingValue());

Then, you can break down each axis and convert them for use in your visible object. However, doing this requires special attention paid to the order of operations. According to Unity's documentation on Euler Angles,

The x, y, and z angles represent a rotation z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in that order).

Applying this in your case, then, can be done like this:

 Vector3 v = emptyObject.eulerAngles;
 transform.rotation = Quaternion.AngleAxis(v.y, Vector3.right) * Quaternion.AngleAxis(-v.x, Vector3.forward) * Quaternion.AngleAxis(-v.z, Vector3.up);

The angles are applied matching the order of Unity's application of axes, in the correct order, but are then applied to the relative axes to which they now correlate.

Comment
Add comment · Show 9 · 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 elder0010 · Feb 20, 2016 at 09:12 PM 1
Share

The idea works, but it's still suffering from gymbal lock issues, due to the Vector3 data!

avatar image Eno-Khaon elder0010 · Feb 20, 2016 at 10:11 PM 0
Share

Hmm... what direction(s) is it facing when this happens? Or, rather, what is the orientation of the Arduino input vs. how it is reflected in Unity in this case?

avatar image Eno-Khaon Eno-Khaon · Feb 21, 2016 at 07:34 PM 1
Share

Ohh, wait... When you said roll and pitch were inverted, did you mean I had them swapped with each other, then?

If so, that changes it from

 transform.rotation = Quaternion.AngleAxis(v.y, Vector3.right) * Quaternion.AngleAxis(-v.x, Vector3.forward) * Quaternion.AngleAxis(-v.z, Vector3.up);

to

 transform.rotation = Quaternion.AngleAxis(v.y, Vector3.forward) * Quaternion.AngleAxis(-v.x, Vector3.right) * Quaternion.AngleAxis(-v.z, Vector3.up);

ins$$anonymous$$d.

At any rate, the rotation axes (forward/right/up) can all be modified freely to try different results. The main thing to keep consistent are the inputs into the final rotation (v.y, v.x, v.z).

Show more comments
avatar image elder0010 · Feb 23, 2016 at 08:44 AM 0
Share

it is working finally :) thank you!!

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

6 People are following this question.

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

Related Questions

How to use quaternions to apply an offset to a rotation to calibrate a controller 1 Answer

Dividing rotation into segments of 45 degrees 2 Answers

using difference in 2 quaternions to cancel out starting position of gyro 0 Answers

Add or substract two quaternions/rotations 2 Answers

How to use gyrostabilizer to rotate the camera 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