Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by Jon-Huhn · Jan 01, 2015 at 06:52 PM · axisanglerotationsbetween

How to find axis and angle between two rotations.

I know Quaternion.Angle() finds the angle between two rotations, but how do I find the axis about which that angle can be applied?

Comment
Add comment · Show 5
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 NoseKills · Jan 01, 2015 at 07:58 PM 1
Share

While I'm getting believeable looking results with this test script (that pretty much uses the logic described by @Glurth aslo)...

(Empty scene, attach to camera)

 using UnityEngine;
 using System.Collections;
 
 public class AngleTest : $$anonymous$$onoBehaviour {
     public Transform a;
     public Transform b;
     public LineRenderer l;
 
     // Use this for initialization
     void Start () 
     {
         l = gameObject.AddComponent<LineRenderer>();
 
         a = GameObject.CreatePrimitive(PrimitiveType.Cube).transform;
         b = GameObject.CreatePrimitive(PrimitiveType.Cylinder).transform;
 
 //        a.rotation = Quaternion.AngleAxis(90, Vector3.up);
 //        b.rotation = Quaternion.AngleAxis(90, Vector3.forward);
 
         Vector3 va = a.TransformDirection(Vector3.right);
         Vector3 vb = b.TransformDirection(Vector3.right);
         Debug.Log(Vector3.Cross(va, vb));
         Debug.Log(Quaternion.Angle(a.rotation, b.rotation));
     
     }
 
     void Update () 
     {
         l.SetVertexCount(2);
         Vector3 va = a.TransformDirection(Vector3.right);
         Vector3 vb = b.TransformDirection(Vector3.right);
         l.SetPosition(1, Vector3.Cross(va, vb) * 10f); 
         Debug.Log(Quaternion.Angle(a.rotation, b.rotation));
     }
 
 }
 

what bothers me is that if you un-comment the commented lines, Quaternion.Angle(a.rotation, b.rotation) returns 120, when the angle see$$anonymous$$gly is 90 degrees (alternate selecting both objects in hierarchy and look at the red rotation arrows in scene view).

So since Quaternions and 3D rotations are a bit more complex than meets the eye, you'll probably have lots more trouble using/applying that axis value even if it was correct :P.

avatar image Glurth · Jan 01, 2015 at 08:38 PM 0
Share

Quaternion.Angle(a.rotation, b.rotation) returns 120, when the angle see$$anonymous$$gly is 90 degrees

I'd think Quaternion angle somehow CO$$anonymous$$BINES both the angle between the axis, and the angles AROUND those axis. Notice that if you change the "90" in those remarked out lines, you will get different angle in your debug output, not 120. I ran your script, the axis it drew is indeed perpendicular to both axis of the rotations. That being said, your tests may very well show that this is NOT what the OP is actually looking for.

avatar image NoseKills · Jan 01, 2015 at 08:47 PM 0
Share

Yeah I'm definitely not saying Quaternion.Angle(); is bugged or anything. Just that I don't know the math behind Quaternions well enough :).

Depending on what the OP needs and how he plans to use the result, this might be another way to get the axis.

 Quaternion differenceFromAtoB = a.rotation * Quaternion.Inverse(b.rotation);
 
         Vector3 axis;
         float angle;
         differenceFromAtoB.ToAngleAxis(out angle, out axis);

Getting the "difference" quaternion and getting the angle and axis from it (based on this)

avatar image MakeCodeNow · Jan 03, 2015 at 06:33 AM 0
Share

Quaternions are 4D (i.e. points on a hypersphere) which means that often your intuition for how to visualize them is wrong, or at least misleading, until you've worked with them a lot and/or really dug into the math.

avatar image Jon-Huhn · Jan 03, 2015 at 11:51 AM 0
Share

Thanks everyone for your replies. I've had to spend some time reading up on how quats work in order to wrap my head around this. I think Glurth's idea is probably the simplest (for some reason I'm not seeing any option to upvote... guess I don't have any $$anonymous$$arma yet:)

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Glurth · Jan 01, 2015 at 07:09 PM

If I understand what your looking for, this axis will be perpendicular to both Quaternions' "LookAt" Vector3 (I think you'd use ToAngleAxis() axis output for that). If you get both those vectors, you can use the cross-product ( Vector3.Cross(V3,V3) ) of them to get a Vector3 perpendicular to both. I doubt this is the most efficient way though.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

how can i find angle from object of world axis ?? 1 Answer

New position in 3D world based on angles and axis input 0 Answers

How do I project a quaternion to the yz-plane 0 Answers

Player transform should always be zero 1 Answer

How to find direction Vector for a collision surface? 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