Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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
0
Question by JustAnotherRobot · Jun 29, 2021 at 03:09 PM · quaternioneuleranglestransform.rotation

Calculate sum over all movements and rotations of object

Hello everybody,

I'd like to do a statistic for my application in which the user can move and rotate an object. In the end I would like to display how far the object has been moved overall and how much the user has rotated the objects.

For the change in position I have the straight forward approach of always adding the change in the objects position to one Vector3 holding the distance in the 3 axis overall. I do this in the Update() method:

 if (transform.hasChanged){
     overallMovedDist  += vector3.Distance(oldPosition, transform.position);
     old.position = transform.position;
     transform.hasChanged = false;
 }

Now for the rotation I wonder, how one would best approach such a statistic? For users it would probably be best to display the values in Eulerangles, I suppose. So after doing two full rotations around for instance the x-axis one would get something like (4*pi, 0, 0) or (2*360, 0,0) as a result: With the same approach as for the position I first tried to calculate this by always directly comparing the change in eulerangles that I got from the quaternions. However if an object is rotated around one axis too far, you get a jump in the values for the other axis as well as some point. If I am correct, this comes from the fact that Unity uses Quaternions internally and the conversion. I tried some stuff with Quaternions already which you can see beneath.

 Quaternion relativeChange = Quaternion.Inverse(oldRotation) * transform.rotation; // get rotation between old and new transform.rotation
 overallRotation = overallRotation * relativeChange; //add the two Quaternions
 oldRotation = transform.rotation;
 Debug.Log(overallRotation.ToEulerAngles()); 

However when I test this in my code, slowly rotate the object around an axis and watch the Log I always get the same thing printed, which is (0.0, 0.0, 0.0). But obviously this is not correct, as I have rotated the object.

Can anybody assist me with how to approach this correctly? Thank you in advance!

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
0

Answer by DenisIsDenis · Jun 30, 2021 at 03:11 AM

Why don't you use Vector3 to store the rotation already at the Euler angles. The algorithm is similar to measuring the coordinate change:

 Vector3 overallRotation, oldRotation;
     void Start()
     {
         oldRotation = transform.rotation.eulerAngles;
     }
 
     void Update()
     {
         if (oldRotation != transform.rotation.eulerAngles)
         {
             overallRotation += transform.rotation.eulerAngles - oldRotation;
             oldRotation = transform.rotation.eulerAngles;
             print(overallRotation);
         }
     }

The angle size will be between 0 and 360 (because transform.rotation.eulerAngles has such borders).

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 JustAnotherRobot · Jul 02, 2021 at 10:21 AM 0
Share

Thank you for your response. I tried to share the issue I have with this approach in the original question, however I probably did not do a good job describing it.

However if an object is rotated around one axis too far, you get a jump in the values for the other axis as well as some point

If I use the eulerangle code, and continuously rotate the object around the same axis (like ai$$anonymous$$g to turn it 360 degrees), this is the output for the overallRotation I get at first is along the lines of:

(1.0, 0.0, 0.0) (2.0, 0.0, 0.0) (3.0, 0.0, 0.0)

But when getting for instance close to 90 degree rotation we got the following switch:

(88.0, 0.0, 0.0) (89.0, 0.0, 0.0) (90.0, 0.0, 0.0) (88.0, 180.0, 180) (87.0, 180.0, 180.0)

avatar image DenisIsDenis JustAnotherRobot · Jul 02, 2021 at 12:00 PM 0
Share

You can approach the issue from the other side. After all, you somehow implement object rotation for users. Then why not use this: for sure you implement rotation for each axis separately. Then you can write something like this:

 // for x-axis
 transform.Rotate(xAngle, 0, 0);
 overallRotation.x += xAngle; // overallRotation is Vector3
avatar image DenisIsDenis · Jul 02, 2021 at 11:05 AM 0
Share

For your method with Quaternion to work, you need to initialize the overallRotation (because simply declaring it, it contains null, and when multiplied by null, we get null):

  overallRotation = Quaternion.Euler (0, 0, 0);

But, unfortunately, even with the use of Quaternions, you will get the problem you indicated (I also have it).

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

121 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

Related Questions

How to use a negative value when MathF.Clamping a eulerAngle? 1 Answer

How do I clamp the Z-Axis Rotation for this code? 1 Answer

Increment Car Rotation along with steering Wheel 0 Answers

How to check for NaN 4 Answers

Incrementing Rotation Produces Strange Transform Values 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