Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by narf0708 · Apr 28, 2016 at 01:06 PM · vector3quaternioneuleranglestorqueeuler angles

Avoiding Gimbal Lock with Vector3 EulerAngles and Applying Torque

I'm trying to make a controller which will let a user apply torque to a spaceship around all three axes. When user input ceases, a damping torque will be applied to slow the rotational speed down to zero.

However, I'm getting a gimbal lock problem where the behaviour of the damping force becomes unpredictable at any rotation where the y and/or z axis rotations shift by 180 degrees instantaneously. All the proposed solutions I've come across so far use a direct manipulation of transform.rotate, which I wish to avoid the use of in favor of RigidBody's addTorque.
My current understanding of the problem is that this is being caused by the fact that Unity uses quaternions instead of EulerAngles to track rotation, but I lack the knowledge of how to solve it.

My current code:

 Rigidbody shipRB;
 void Update () {
     getInput();
 }

 Vector3 lastRotationPos;
 void getInput() {

     //rotations
     if (Input.GetKey("q")) {
         shipRB.AddTorque(transform.right);
     } else if (Input.GetKey("e")) {
         shipRB.AddTorque(-transform.right);
     } else {
         Vector3 currentRotation = transform.rotation.eulerAngles;
         Vector3 rotationDelta = lastRotationPos - currentRotation;
         float xAngle = Mathf.DeltaAngle(lastRotationPos.x, currentRotation.x);
         float yAngle = Mathf.DeltaAngle(lastRotationPos.y, currentRotation.y);
         float zAngle = Mathf.DeltaAngle(lastRotationPos.z, currentRotation.z);
         if (transform.eulerAngles.y >= 90 && transform.eulerAngles.z >= 90) {
             xAngle = -xAngle;
         }
         rotationDelta = new Vector3(-xAngle, -yAngle, -zAngle);
         
         //limiting correction thrust
         rotationDelta = new Vector3(limit(rotationDelta.x, 5), limit(rotationDelta.y, 5), limit(rotationDelta.z, 5));

         //apply correction thrust
         shipRB.AddTorque(rotationDelta);
     }
     if (Input.GetKey("i")) {
         shipRB.AddTorque(transform.forward);
     } else if (Input.GetKey("k")) {
         shipRB.AddTorque(-transform.forward);
     }
     if (Input.GetKey("j")) {
         shipRB.AddTorque(transform.up);
     } else if (Input.GetKey("l")) {
         shipRB.AddTorque(-transform.up);
     }
     lastRotationPos = transform.rotation.eulerAngles;
 }


 float limit(float num, float limit) {
     if (num > limit) {
         num = limit;
     } else if (num < -limit) {
         num = -limit;
     }
     return num;
 }
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 Scribe · Apr 28, 2016 at 01:56 PM 0
Share

Why not just increase Angular drag component of the rigidbody?

avatar image Scribe Scribe · Apr 28, 2016 at 01:58 PM 0
Share

Or use apply torque as shipRB.AddTorque(-shipRB.angularVelocity) if you really want to do it yourself. You seem to want to make things hard for yourself.

avatar image narf0708 Scribe · Apr 28, 2016 at 03:04 PM 1
Share

Thanks, increasing angular drag works perfectly. I'm not very familiar with the Unity libraries yet, and also have a habit of inadvertently making things hard for myself.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

61 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

Related Questions

Quaternions to EulerAngles weird x-axis interaction 1 Answer

Quaternion.Angle() unexpected output. Compare to Vector3.Angle() which produces correct values. 0 Answers

Why isn't my steering wheel working? 0 Answers

Rotate object towards Vector 0 Answers

Rotate LOCAL y axis to match Joystick input 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