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 /
avatar image
0
Question by NeilM0 · Sep 29, 2011 at 08:39 PM · physicsquaternionforcetorqueaddtorque

Figuring out the correct amount of torque to apply?

How do I figure out how much torque is needed to my object to get it to match the rotation of another object in the scene?

I'm trying to use torque to get the position of one Rigid body to match the position of another object elsewhere in the scene.

.MoveRotation technically worked, but it jitters around a lot and it doesn't seem to play well with gravity. I don't mind if my object doesn't have the exact rotation of the other one, I just want it generally in the same spot, which is why I'm trying to use torque.

I've got a script on one object which has the transform of a second object.

 public Transform match;

 void FixedUpdate()
 {
    Vector3 t = ? // How do I get from transform.rotation to match.rotation?
    transform.rigidbody.AddTorque(t, ForceMode.Impulse);
 }

Update:

This is getting closer, but it's still not quite right. It starts rotating in the direction, but I expected it to overshoot and come back and sort of wobble, but it didn't do that. It just keeps spinning.

 using UnityEngine;
 using System.Collections;
 
 public class RotateOnClick : MonoBehaviour {
     
     public Transform match;
     
     void FixedUpdate() 
     {
         if (Input.GetMouseButtonUp(0))
         {
             Vector3 to = match.localRotation.eulerAngles.normalized;
             Vector3 fr = (transform.rotation.eulerAngles == Vector3.zero) ? Vector3.up : rigidbody.rotation.eulerAngles.normalized;
             
             Quaternion deltaQuat = Quaternion.FromToRotation(fr, to);
             
             Debug.Log("From: " + fr + " to: " + to + "  delta: " + deltaQuat);
             
             Vector3 x = Vector3.Cross(fr, to);
             float theta = Mathf.Asin(x.magnitude);
             Vector3 w = x.normalized * theta / Time.fixedDeltaTime;
             Quaternion q = transform.rotation * rigidbody.inertiaTensorRotation;
             Vector3 T = q * Vector3.Scale(rigidbody.inertiaTensor, (Quaternion.Inverse(q) * w));
             
             rigidbody.AddTorque(T, ForceMode.Impulse);
         }
     }
 }
Comment
Add comment · Show 2
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 SirGive · Sep 29, 2011 at 09:22 PM 0
Share

Why not just measure the amount of torque from one and apply it to the other and just play with a scale until they act similar enough?

avatar image NeilM0 · Sep 30, 2011 at 02:45 AM 0
Share

The other object doesn't have torque applied to it. It's just at a particular rotation. If I copied it over, it would probably just be zero.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Sep 29, 2011 at 09:31 PM

AddTorque is like AddForce: it applies an acceleration to the rigidbody, which increases the velocity over time. It's much easier to copy its angularVelocity:

void FixedUpdate(){
  rigidbody.angularVelocity = match.rigidbody.angularVelocity;
}
But this only copy the physics effects: if you rotate match using Rotate or other non-physics method, nothing happens. If you need to make the object follow any match rotation, just copy the transform.rotation directly:

void FixedUpdate(){
  transform.rotation = match.rotation;
}
You can even add some inertia using Lerp:

void FixedUpdate(){
  transform.rotation = Quaternion.Lerp(transform.rotation, match.rotation, 5*Time.deltaTime);
}
Comment
Add comment · Show 5 · 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 NeilM0 · Sep 30, 2011 at 02:48 AM 0
Share

I wasn't totally clear in the first post. The match object isn't a physics object. It's just a standard one that I'm trying to match. It won't have a rigidbody to copy off of. If it did it would have to be kinemetic and I don't think it would have an angularVelocity.

I want to copy it's rotation using forces so that it can be affected by other forces, but generally keep the same shape most of the time.

avatar image aldonaletto · Sep 30, 2011 at 03:00 AM 0
Share

Is the match object rotating at a constant rotation and around a fixed axis? Or it can rotate to any direction?

avatar image NeilM0 · Sep 30, 2011 at 01:23 PM 0
Share

The match object doesn't have to move, but can rotate in any direction.

avatar image AndersWR · Oct 09, 2013 at 04:17 PM 0
Share

hello, did you ever figure this out?

avatar image NeilM0 · Oct 09, 2013 at 05:18 PM 0
Share

No. However, I would still like to know the answer.

avatar image
0

Answer by Tehelee · Jun 05, 2018 at 10:39 AM

I'm sure I'm not the only one to end up here recently; here's my springy rotation lock component. It functions similarly to the Spring Joint but for rotation:

 [RequireComponent(typeof(Rigidbody))]
 public class SpringRotation : MonoBehaviour
 {
     public float force = 10f;
 
     public Rigidbody target;
     private new Rigidbody rigidbody;
 
     private Vector3 torque;
 
     private void Awake()
     {
         this.rigidbody = this.GetComponent<Rigidbody>();
     }
 
     private void FixedUpdate()
     {
         if ((null == target) || !target) return;
        
         // Determine Quaternion 'difference'
         // The conversion to euler demands we check each axis
         Vector3 torqueF = OrientTorque(Quaternion.FromToRotation(this.transform.forward, this.target.transform.forward).eulerAngles);
         Vector3 torqueR = OrientTorque(Quaternion.FromToRotation(this.transform.right, this.target.transform.right).eulerAngles);
         Vector3 torqueU = OrientTorque(Quaternion.FromToRotation(this.transform.up, this.target.transform.up).eulerAngles);
 
         float magF = torqueF.magnitude;
         float magR = torqueR.magnitude;
         float magU = torqueU.magnitude;
 
         // Here we pick the axis with the least amount of rotation to use as our torque.
         this.torque = magF < magR ? (magF < magU ? torqueF : torqueU) : (magR < magU ? torqueR : torqueU);
            
         this.rigidbody.AddTorque(this.torque * Time.fixedDeltaTime * force);
     }
 
     private Vector3 OrientTorque(Vector3 torque)
     {
         // Quaternion's Euler conversion results in (0-360)
         // For torque, we need -180 to 180.
 
         return new Vector3
         (
             torque.x > 180f ? 180f - torque.x : torque.x,
             torque.y > 180f ? 180f - torque.y : torque.y,
             torque.z > 180f ? 180f - torque.z : torque.z
         );
     }
 }
Comment
Add comment · Show 1 · 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 davidcoenfish · Apr 20, 2020 at 08:51 AM 0
Share

perhaps that was ment to be the following for the OrientTorque

     return new UnityEngine.Vector3(
         180.0f < torque.x ? torque.x - 360.0f : torque.x,
         180.0f < torque.y ? torque.y - 360.0f : torque.y,
         180.0f < torque.z ? torque.z - 360.0f : torque.z
     );

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AddTorque does not use ForceMode as expected (Different than AddForce) 1 Answer

AddTorque application is too slow 1 Answer

Direction of rotation and ApplyForce 1 Answer

AddRelativeTorque not rotating 1 Answer

Calculate force for torque by specifying a required velocity 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