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
2
Question by Hengo- · Oct 05, 2016 at 08:02 PM · physicsrigidbodycenterofmass

Problem with Rigidbody.CenterOfMass

Rigidbody.CenterOfMass seems to return wrong values for me.

here a simpified scene: picture what you see here: Black ray: Center of the box collider (should be center off mass)
Red ray: what Rigidbody.CenterOfMass returns
Cyan ray: center of the gameObject
the box is imported from blender (that it is off center is intentional)

and when im already here, how do i get the center of mass from multiple rigidbodys connected with joints from unity?

ea0ksc0.png (42.8 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Bunny83 · Oct 06, 2016 at 02:29 AM

Does the single box has a rigidbody? Or are the two boxes part of a single rigidbody? If they are childs of a rigidbody they count as compound colliders and the center of mass would be calculated accordingly.

Two rigidbodies don't have a common center of mass. Each rigidbody is calulated on it's own. Joints just apply forces between the two bodies.

We don't have enough information to give a clear answer. It's not clear what belongs to the same object and what's a seperate object.

Keep in mind that rigidbodies in rigidbody physics are considered "rigid", so they don't change shape or size dynamically. All physical properties of the rigidbody stays constant.

Also we don't know how you visualized the center of mass. Rigidbody.centerOfMass is a local vector but it isn't defined in localspace as it isn't affected by scale. To get the worldspace position of the center of mass you have to do:

 var c = transform.position + transform.rotation*rb.centerOfMass;

or

 var c = transform.position + transform.TransformDirection(rb.centerOfMass);

TransformPoint will not give the right position as it applies the scale of the object as well.

The documentation reads as follows:

Note: centerOfMass is relative to the transform's position and rotation, but will not reflect the transform's scale!

Comment
Add comment · 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
0

Answer by Jindrich-Regal · Mar 16, 2017 at 08:27 PM

@Hengo For more rigidbody connected with joints you can use something like that:

 public class CenterOfMass : MonoBehaviour {
 
     Vector3 centerOfMass;
     Rigidbody[] parts;
     
     void Start () {
     
     }
 
     private void OnEnable()
     {
         parts = GetComponentsInChildren<Rigidbody>();
     }
     
     public Vector3 getCenterOfMass () {
         centerOfMass = Vector3.zero;
         float totalMass = 0;
         for (int i = 0; i < parts.Length; i++)
         {
             centerOfMass += parts[i].worldCenterOfMass*parts[i].mass;
             totalMass+= parts[i].mass;
         }
         centerOfMass /= totalMass;
         return centerOfMass;
     }
 
     private void OnDrawGizmos()
     {
         getCenterOfMass ();
         Gizmos.color = Color.red;
         Gizmos.DrawWireSphere(centerOfMass, 0.2f);
     }
 }

You can cache result for frame or longer time for better performance.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

AddRelativeTorque results in incorrect angular acceleration (Maybe centrifugal force affects) 0 Answers

Changing a rigidbody's center of mass, to get a natural rotation while it moves through the air 4 Answers

Is there a way to draw center of mass on the screen? 1 Answer

How can I rotate around with a Rigidbody while respecting physics? 0 Answers

AddRelativeTorque() Applied Incorrectly When Using a Custom Centre of Gravity 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