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 JonathanBolten · Dec 15, 2012 at 06:39 AM · physicsrigidbodyquaterniontorqueangularvelocity

Aligning 2 Quaternions Using Torque

Hi, I'm working on improving my math skills, especially with quaternions. A weird behavior came up that I'm curious about...

I'm trying to rotate one object to be 'near' identical rotation of a target object using Unity's torque physics. (I know these aren't real world accurate or anything... I'd just like it to get it behaving better.) The code I wrote below works fine if I comment out:

 myRigid.AddRelativeTorque (newAxis);

and use this line instead (uncomment):

 // myRigid.MoveRotation (Quaternion.AngleAxis (newAngle , newAxis));

but obviously that's not using the torque feature of physics.

Currently the cube is rotating correctly to the camera's rotation, and then stopping, like it should, but when I change the camera's rotation from 0.0, 0.0, 0.0 to ANYTHING else the cube is rocking back and forth and BARELY MOVING AT ALL! (It doesn't decisively go anywhere.)

How do I fix this? Here is the code I wrote:

     private void UsingQuaternions ()
     {
         Quaternion changeRotation = Quaternion.RotateTowards (myRigid.rotation , Camera.main.transform.rotation , 0.0f); // I've played around with the last value (delta) here, I don't think this is causing my problem.
         
         Vector3 newAxis;
         float newAngle;
         changeRotation.ToAngleAxis (out newAngle , out newAxis);
         newAxis.Normalize ();
 
         float angleBetween = Quaternion.Angle (myRigid.rotation , Camera.main.transform.rotation);

         if (angleBetween < 3.0f)
         {
 // This makes the rotation 'gravitate' towards the target area when it gets close.
             myRigid.angularVelocity *= 0.6f;
         }
         else
         {
             myRigid.AddRelativeTorque (newAxis);
             // myRigid.MoveRotation (Quaternion.AngleAxis (newAngle , newAxis));
         }
     }

UsingQuaternions () is called once every FixedUpdate (), just to clarify. :)

I'd appreciate any help with this, 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

Answer by whydoidoit · Dec 15, 2012 at 07:02 AM

So relativetorque is applying a force around the axes of the rigidbody - given that you apply a force with a maximum magnitude of 1 then I'm not surprised it isn't moving! Also newAxis as well as being normalized (before you made your call to do so, as well as after...) is the axis that the rotation should occur around - not the direction which movement should occur in. Added to that you need to understand what the rotation of the object is to apply the force.

What you are trying to do is very difficult, effectively you would need to work out how much force to apply around each axis of the actual representation of the object - I'm guessing that's Vector3.up for AddTorque.

You should check out this answer by @aldonaletto.

My best guess (without trying it) of getting the right kind of thing to apply would be to use:

 var requiredRotation = Quaternion.FromToRotation(myRigid.forward, Camera.main.transform.forward);
 myRigid.AddTorque(requiredRotation.eulerAngles * someKindOfScalingFactor);

Given the speed of movement with AddForce depends on the mass of the object - I've no idea what that scaling factor might be...

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 JonathanBolten · Dec 15, 2012 at 10:21 AM 0
Share

On the note of newAxis being the rotation that is rotated around and not the direction... I thought RotateTowards would flip the direction to where I want to be going, I guess not though. Why is that?

Thanks for your input! It has been very helpful. $$anonymous$$aking me think about things differently now. :)

avatar image whydoidoit · Dec 15, 2012 at 10:49 AM 0
Share

So Quaternion.AngleAxis returns a rotation around some axis. So the axis will be pointing perpendicular to the angular rotation that would be made to adjust the object.

RotateTowards is adjusting one angle towards another - you don't really need that - you need to apply a force around either the local up of the object (using AddRelativeTorque) or around Vector3.up that in some way causes the object to move towards the target rotation.

If you want to just have it point there - then just update 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

11 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

Related Questions

transform.Rotate and Rigidbody.MovePosition 1 Answer

Gravity on a rotating platform 1 Answer

Understanding how torque is applied 2 Answers

Using Torque to kill Angular Velocity 2 Answers

Problems with rotating an object using angular velocity. 2 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