- Home /
Problem with Rigidbody.CenterOfMass
Rigidbody.CenterOfMass seems to return wrong values for me.
here a simpified scene: 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?
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!
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.
Your answer
Follow this Question
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