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 fat_birb · Jan 21, 2020 at 05:35 AM · rotationphysicsrigidbody

What is the most accurate method of calculating total rotations of a rigidbody?

I want to calculate a rigidbody's total rotations in the most accurate manner possible in order to simulate robot encoders (encoders calculate the total amount of rotations of an axle without resetting once the axle makes a full rotation, e.g. the encoder gives an output of 0, 360 degrees, 720 degrees, and so on.) Luckily, I only wish to calculate rotations along one axis. However, I cannot simply read from transform.eulerAngles, as this number resets to zero or inverts itself after reaching a certain threshold. This also means I cannot reliably use the change in rotation each frame and add it to a total, as I need to know the direction which the rigidbody rotated as well. I'm also unsure about using the angular velocity of the rigidbody and multiplying it by changes in time, as I do not know if this mirrors the calculations by Unity's physics engine and I want the calculation to be as accurate as possible. I also want the calculations to stay accurate at high RPMs, and it seems unlikely to me that an angular velocity calculation would be accurate if the rigidbody encounters an object. Does anyone have a accurate method of getting a rigidbody's total rotation, keeping in mind high rotational speeds and collisions? Thanks.

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
2
Best Answer

Answer by lgarczyn · Jan 21, 2020 at 10:52 PM

The easiest way is this (for the Y axis)

 FixedUpdate(){
     totalRot += rigidbody.angularVelocity.y * Time.fixedDeltaTime;
 }

The possible errors will be minuscule, and caused by the fact that the rotation of a rigidbody may be stopped mid-physics-frame by a collision, which means that every frame where there is a collision you may have an error of 50% of the delta velocity average.


This is the other option, more precise, but can fail if the angular velocity gets ludicrously high.

 float yRot = rigidbody.rotation.eulerAngles.y;
 float angle = Mathf.Angle(yRot, yRotPrev);
 if (yRot > yRotPrev)
     totalRot += angle;
 else
     totalRot -= angle;
 yRotPrev = yRot;

Or this shorter but untested code

 totalRot += Mathf.repeat(yRot - prevRot, 360f) % 180f;


Both solutions could be put together and work in absurd scenarios, but it's really unnecessary.

Comment
Add comment · Show 2 · 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 fat_birb · Jan 22, 2020 at 03:21 AM 0
Share

Thanks so much for answering and for the advantages/disadvantages list!

avatar image lgarczyn fat_birb · Jan 22, 2020 at 10:29 PM 0
Share

Always glad to help

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

239 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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 make an object(cylinder) always at the center of an object(Terrain)? 0 Answers

Rolling a 3D triangle 0 Answers

Rigidbody.MoveRotation() Towards an object? 1 Answer

how to shot a soccer ball in curve path according to the swipe type 2 Answers

Balancing in a bike game 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