• 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 Sunrunner · Aug 20, 2017 at 12:17 AM · rotationrigidbody3dvelocityangularvelocity

Calculate Angular Velocity to Match Remote Position

I have a rigid body that I need to sync with a remote game object's rotational position & angular velocity. Unfortunately, this object has a massive amount of children causing direct modifications to transform.rotation to have severe lag. instead, I am directly setting angular velocity to get fluid movements. This is my code:

         private Vector3 GetAngularVelocity(Quaternion remoteRotation, Vector3 remoteAngularVelocity, GameObject gameObject)
         {
             Vector3 difference = new Vector3(Mathf.DeltaAngle(gameObject.transform.rotation.eulerAngles.x, remoteRotation.eulerAngles.x),
                                              Mathf.DeltaAngle(gameObject.transform.rotation.eulerAngles.y, remoteRotation.eulerAngles.y),
                                              Mathf.DeltaAngle(gameObject.transform.rotation.eulerAngles.z, remoteRotation.eulerAngles.z));
 
             if (difference.magnitude < 0.1 && remoteAngularVelocity == Vector3.zero) //overcorrections can cause jitter when standing still. 
             {
                 return Vector3.zero;
             }
 
             Vector3 eulersPerSecondToMakeUpDifference = difference / PlayerMovement.BROADCAST_INTERVAL; //BROADCAST_INTERVAL = 0.05 seconds
 
             Vector3 radiansPerSecondToMakeUpDifference = new Vector3(eulersPerSecondToMakeUpDifference.x * Mathf.Deg2Rad,
                                                                      eulersPerSecondToMakeUpDifference.y * Mathf.Deg2Rad,
                                                                      eulersPerSecondToMakeUpDifference.z * Mathf.Deg2Rad);
             
             float maxAdjustment = 0.5f; // to prevent jerkiness and correct error over time
             Vector3 limitedVelocityChange = MathUtil.ClampMagnitude(radiansPerSecondToMakeUpDifference, maxAdjustment, maxAdjustment * -1);
 
             Console.WriteLine("Remote Rotation: " + precise(remoteRotation.eulerAngles) + " " +
                               "Current Rotation: " + precise(gameObject.transform.rotation.eulerAngles) + " " +
                               "Difference: " + precise(difference) + " " +
                               "eulersPerSecondToMakeUpDifference: " + precise(eulersPerSecondToMakeUpDifference) + " " +
                               "radiansPerSecondToMakeUpDifference: " + precise(radiansPerSecondToMakeUpDifference) + " " +
                               "remoteAngularVelocity: " + precise(remoteAngularVelocity) + " " +
                               "limitedVelocityChange: " + precise(limitedVelocityChange) + " ");
 
             return remoteAngularVelocity + limitedVelocityChange;
         }


         rigidbody.angularVelocity = GetVehicleAngularVelocity(remoteRotation, remoteAngularVelocity, gameObject);

This causes behavior that is close but not quite right. After moving the remote object from one position to another, the local object tends to spin without ever landing on the desired target. Could this be Gimbal lock?

Example output:

 Remote Rotation: (2.629201, 208.2598, 358.6983) Current Rotation: (15.38525, 167.0688, 260.5146) Difference: (-12.75604, 41.19099, 98.18378) eulersPerSecondToMakeUpDifference: (-255.1208, 823.8199, 1963.676) radiansPerSecondToMakeUpDifference: (-4.452699, 14.37837, 34.2726) remoteAngularVelocity: (0, 0, 0) limitedVelocityChange: (-0.05947673, 0.1920584, 0.4577948) 
 Remote Rotation: (2.629201, 208.2598, 358.6983) Current Rotation: (15.33852, 167.1382, 260.2452) Difference: (-12.70932, 41.12169, 98.45313) eulersPerSecondToMakeUpDifference: (-254.1864, 822.4338, 1969.063) radiansPerSecondToMakeUpDifference: (-4.436389, 14.35418, 34.36662) remoteAngularVelocity: (0, 0, 0) limitedVelocityChange: (-0.05914053, 0.1913524, 0.4581339) 
 Remote Rotation: (2.629201, 208.2598, 358.6983) Current Rotation: (15.20987, 167.3288, 260.1854) Difference: (-12.58066, 40.93106, 98.51297) eulersPerSecondToMakeUpDifference: (-251.6132, 818.6212, 1970.259) radiansPerSecondToMakeUpDifference: (-4.391478, 14.28764, 34.38751) remoteAngularVelocity: (0, 0, 0) limitedVelocityChange: (-0.05855986, 0.190524, 0.4585535) 
 Remote Rotation: (2.629201, 208.2598, 358.6983) Current Rotation: (15.20987, 167.3288, 260.1854) Difference: (-12.58066, 40.93106, 98.51297) eulersPerSecondToMakeUpDifference: (-251.6132, 818.6212, 1970.259) radiansPerSecondToMakeUpDifference: (-4.391478, 14.28764, 34.38751) remoteAngularVelocity: (0, 0, 0) limitedVelocityChange: (-0.05855986, 0.190524, 0.4585535)
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

113 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

Related Questions

Stop velocity on rotation change 2 Answers

How to make a player rotate in a direction with a slope of the ground. 0 Answers

Rotate Rigidbody Towards Velocity? [3d] 0 Answers

Rolling a 3D triangle 0 Answers

i want to stop a freely moving game object (Using Rigid body) at any instance and in any direction but latter on after some time i want that Game Object to move in same Direction and Same Speed 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