Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
13
Question by Jean-Fabre · Sep 07, 2010 at 07:29 AM · quaternionangle

how to get the signed angle between two quaternion?

Hi,

Quaternion.Angle returns the absolute angle between two transform.rotation. Is there a technic to get it signed, I am currently using cross products and vector comparison to check. It seems to me very cumbersome.

Is there a better technic or something built in actually?

Thanks for your help,

Jean

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

1 Reply

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

Answer by duck · Sep 07, 2010 at 10:30 AM

To get the unsigned angle between two quaternions, you can use the Quaternion.Angle function:

var angle = Quaternion.Angle( rotationA, rotationB );

However to get a signed angle doesn't really make a lot of sense in 3D space, because the rotation could be in any 3d direction (not just in one of two 'flat' directions).

Unity does however provide the function "Mathf.DeltaAngle" to get the signed difference between two angles (not rotations), so perhaps what you want to do is convert your 3D rotations into 2D angles relative to a certain direction on a certain plane (using Atan2). For example, to compare the angle of rotationA with rotationB, projected on the X-Z plane, you could do this:

// get a "forward vector" for each rotation var forwardA = rotationA * Vector3.forward; var forwardB = rotationB * Vector3.forward;

// get a numeric angle for each vector, on the X-Z plane (relative to world forward) var angleA = Mathf.Atan2(forwardA.x, forwardA.z) Mathf.Rad2Deg; var angleB = Mathf.Atan2(forwardB.x, forwardB.z) Mathf.Rad2Deg;

// get the signed difference in these angles var angleDiff = Mathf.DeltaAngle( angleA, angleB );

Angle differences on the X-Z plane would be suitable for "top down" angle differences - eg, determining the directional differences between cars on a terrain. For other situations, you might need to use a different plane such as X-Y or Y-Z.

Hope this solves your problem!

Comment
Add comment · Show 8 · 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 Jean-Fabre · Sep 07, 2010 at 12:05 PM 0
Share

Thanks Duck, yes, signed angle make sense only against a plane or an axis, that's what I am am after. The code is as involving as is $$anonymous$$e currently.

avatar image Saturnix · Dec 30, 2012 at 09:07 PM 0
Share

I love you. Seriously, thanks a lot!

avatar image infinitypbr · Jun 22, 2013 at 01:54 AM 0
Share

Super old answer, but it was the solution to getting AngularSpeed into $$anonymous$$ecanim so that a Nav$$anonymous$$esh can control the movement, $$anonymous$$ecanim can still show the proper animations. (This is because the "S$$anonymous$$lth" tutorial 4.3 is not out yet, so I had to figure it out myself)

avatar image Karwoch · Jul 14, 2015 at 01:03 PM 0
Share

$$anonymous$$arvellous!

avatar image david.struverson · Mar 01, 2016 at 09:26 PM 0
Share

Hey, Duck, sorry to bother you, but for this example, is it the Z rotation? And are you assu$$anonymous$$g that the rotation is in eulerAngles?

avatar image david.struverson david.struverson · Mar 01, 2016 at 09:54 PM 0
Share

Adding onto this, my code doesn't seem to be working: using UnityEngine; using System.Collections;

 public class positionmang3 : $$anonymous$$onoBehaviour {
     private Quaternion transRotTemp;
     private Quaternion gyroRotTemp;
     private Vector3 transRotVect;
     private Vector3 gyroRotVect;
     private float gyroAngleTemp;
     private float transAngleTemp;
     void Start () {
         Input.gyro.enabled = true;
 
     }
     void Update () {
         /*gyroDiff = remote.localRotation.eulerAngles - transform.localRotation.eulerAngles;
         transform.rotation *= Quaternion.AngleAxis (gyroDiff.x, transform.right);
         transform.rotation *= Quaternion.AngleAxis (gyroDiff.y, transform.up);
         transform.rotation *= Quaternion.AngleAxis (gyroDiff.z, transform.forward);*/
         transform.rotation *= Quaternion.AngleAxis (rotDiff (transform.rotation, Input.gyro.attitude, "x"), transform.right);
         Debug.Log(rotDiff(transform.rotation, Input.gyro.attitude, "x"));
 
     }
     float rotDiff (Quaternion transRot, Quaternion gyroRot, string axis) {
         //TE$$anonymous$$P
         transRotTemp = transRot;
         gyroRotTemp = gyroRot;
         if (axis == "x") {
             transRotVect = transRotTemp.eulerAngles;
             gyroRotVect = gyroRotTemp.eulerAngles;
             transRotVect = Vector3.Scale(transRotTemp.eulerAngles, transform.forward);
             gyroRotVect = Vector3.Scale(gyroRotTemp.eulerAngles, transform.forward);
             gyroAngleTemp = $$anonymous$$athf.Atan2 (gyroRotVect.y, gyroRotVect.z) * $$anonymous$$athf.Rad2Deg;
             transAngleTemp = $$anonymous$$athf.Atan2 (gyroRotVect.y, gyroRotVect.z) * $$anonymous$$athf.Rad2Deg;
             return $$anonymous$$athf.DeltaAngle (gyroAngleTemp, transAngleTemp);
 
         } else {
             Debug.LogError ("ERROR: Invalid axis in rotDiff() function");
             return 0;
         }
     }
 
 }
 
Show more comments

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

7 People are following this question.

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

Related Questions

Calculate Angle: From & To 3 Answers

how do I limit the angle that a bone can move using quaternions? 2 Answers

Coroutine with Quaternion.Angle 1 Answer

Rotate vector around vector? 2 Answers

How can I draw a line at a specific angle? 1 Answer


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