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
0
Question by joncoughlingames · Jan 16, 2018 at 08:59 PM · rigidbodycollidermassdensity

Can I control mass density of colliders in a rigidbody independent of their size?

I am building a 3D rigidbody physics simulation with modular attachable components. I have a single rigidbody on a parent object, and as I attach or remove modules I add to or subtract from the parent rigidbody mass. I also have a collider associated with each component, and I would like to use the rigidbody physics to automatically calculate the inertia tensor and center of mass of the full system of colliders.

The Unity rigidbody component seems to assume uniform mass density among children colliders. Is there a way for me to adjust that assumption that is not based on collider size? I would like to be able to assign a small, attachable module a large mass without making its collider larger than its physical size.

Comment
Add comment · Show 1
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 joncoughlingames · Jan 16, 2018 at 09:01 PM 0
Share

If I can't adjust collider density, I will have to manually calculate the mass properties of my system of objects. I have started coding this already, and while center of mass is easy to find, I am struggling to calculated the inertia tensor of asymmetric bodies.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Captain_Pineapple · Feb 27, 2018 at 10:08 PM

This piece of code should do the trick: Warning: This is only accurate for cube Sized Modules as children. If you want to use other things use a different approach for inertia of simple geometry forms. ->wikipedia or something. (for cubes its mass*sidelength/6)

 private void recalculateMassAndInertia()
         {
             Vector3 newCenterOfMass = Vector3.zero;
             Vector3 newInertiaVector = Vector3.zero;
             Vector3 distance = Vector3.zero;
             YourModuleScript m;
             float sumOfMass = 0;
             foreach (Transform t in allConnectedModules) {
                 
                 if (t.gameObject.GetActive ()) {
                     m = t.GetComponent<YourModuleScript> ();
                     
                     newCenterOfMass += (t.localPosition * m.mass);
                     
                     sumOfMass += m.mass;
                 }
             }
     
             newCenterOfMass = newCenterOfMass /sumOfMass;
     
             Debug.Log (newCenterOfMass);
             foreach (Transform t in allConnectedModules) {
                 if (t.gameObject.GetActive ()) {
                     m = t.GetComponent<YourModuleScript> ();
                     distance = new Vector3 (Mathf.Pow (t.localPosition.y - newCenterOfMass.y, 2.0f) + Mathf.Pow (t.localPosition.z - newCenterOfMass.z, 2.0f), Mathf.Pow (t.localPosition.x - newCenterOfMass.x, 2.0f) + Mathf.Pow (t.localPosition.z - newCenterOfMass.z, 2.0f), Mathf.Pow (t.localPosition.x - newCenterOfMass.x, 2.0f) + Mathf.Pow (t.localPosition.y - newCenterOfMass.y, 2.0f));
                     newInertiaVector += ((Vector3.one * m.mass *m.sidelength/ 6.0f) + m.mass* distance);
                     // if your parent object is in the list: detect it and use this line for this module:
                         //newInertiaVector += (Vector3.one * m.mass *m.sidelength/ 6.0f);
                 }
             }
             GetComponent<Rigidbody> ().centerOfMass = newCenterOfMass;
             GetComponent<Rigidbody> ().inertiaTensor = newInertiaVector;
     
             GetComponent<Rigidbody> ().mass = sumOfMass;
             Debug.Log (sumOfMass);
         }



You have to add your own script to keep track of the modules mass. This should go wherever you find "yourModuleScript" and it should have a public float property named "mass". Also this script assumes that you have all your modules set in a List "allConnectedModules".

Note: im not sure in wich order to set the mass/inertia/center of mass to prevent unity from automatically recalculating on its own. Please let me know if you have any idea about this.

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 joncoughlingames · Feb 28, 2018 at 02:49 PM 0
Share

Thanks for the suggestion. The inertia tensor is the part I have had the most trouble with. Do you know if your code works for children that have been rotated?

If I understand it correctly, the inertia tensor that Unity stores is the moments of inertia around the principal axes of rotation. The rotation vector rotates from object local frame to the frame aligned with those principal axes. I know how to calculate these things with straight matrix math, but it involves solving for eigenvalues and stuff, and I haven't done much straight matrix math inside Unity, so I know I'd have to learn a bit to make my functions.

It would be super helpful, since Unity is already doing all of this under the hood, if they would add the option to set density of individual colliders in a system underneath a single rigidbody. That would allow their rigidbody system, while it's calculating all of this stuff, to assign proper mass to each collider ins$$anonymous$$d of dividing it up proportionally.

avatar image Captain_Pineapple joncoughlingames · Mar 09, 2018 at 01:30 PM 0
Share

yeah i'm totally with you. A mass setting for 3-D Colliders Would be dope.. But i guess they have kind of a similar problem as you. As far as my Script is concerned: It assumes that your blocks are somewhat aligned to each other. So rotations of +-k*90° is fine but anything else will not be precise. The transformation of the main axis to a different system is indeed a difficult matter... Question is: Do you actually need this precision? And what kind of Colliders are you using? Is it acceptable to just approximate the bodys you are using? Especially since i guess that the extra precision you gain will be lost due to the massive calculationtime and unitys sometimes not so precise physics-calculations.

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

109 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

Related Questions

Rigidbodies won't collide if mass difference is too high 0 Answers

How to give dynamically add mass to a gameobject from volume(collider.bounds.size) and density 1 Answer

Rigidbody mass not updating in inspector after calling SetDensity 2 Answers

Players slips on slopes for no reason. 0 Answers

make 2 object not collide? 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