# Calculation of Inertia Tensors

Bit of a complex and lengthy question that, I'll admit, I don't quite understand very well yet so I will try and explain as best as I can.

Short Version: Is there a general c++/physx formula out there to calculate inertia tensors based off an objects shape?

Long Version: For our physics, we need to specify x, y and z inertia tensors. Currently the way we do it is pretty much just a ratio based off of mass. So if an object is long on the X axis and thin on Y and Z, and the mass is 10000, we will set Z and Y to 7000 and X to 3000. (This isn't exact, but just to give an idea)

This works relatively well but our biggest problem is when there is joint instability somewhere, we have to keep guessing at tensors until we figure out what works best. This can become very time consuming if we have a very big physics simulation and one out of 20+ joints is causing all the others to lose stability.

What I am working on is a function that will take the bounding box of an object and hopefully calculate relatively accurate tensors. I have taken some of the math from http://en.wikipedia.org/wiki/List_of_moment_of_inertia_tensors and made a function that basically works like the following for similar rotations below.

Or if the rotation is on an end, like this:

So, this seems to give me results that are similar to the way we have been doing it, but I dont want to switch to this way without making sure that it will work for general use. Below is the code for my function based on the first image with a cube and center pivot.

NxVec3 CalculateInertiaTensor( VisBoundingBox_cl boundingBox, float m )
{
float width = boundingBox.GetSizeX();
float height = boundingBox.GetSizeZ();
float depth = boundingBox.GetSizeY();

float xTensor = 0.083f * m*(height*height + depth*depth);
float yTensor = 0.083f * m*(width*width + depth*depth);
float zTensor = 0.083f * m*(width*width + height*height);

return NxVec3(xTensor, yTensor, zTensor);
}


I can't guarantee that this is the right way to do it (as the most accurate way is to use the actual shape instead of a bounding box) and i am not very familiar with inertia tensors and the math but it seems to return numbers fairly similar to what we were using. Anyone here happen to know if there is a better way to do this?

• If you can decompose your object into tetrahedra then you ought to be able to use the linearity of the tensor along with the basic formula for the moment of inertia of a tetrahedron (you can find this with Wolfram Alpha, for instance) to compute an exact tensor. My concern with the bounding box method would be that it really depends on how much of your BB the object fills; imagine the difference between a fat ellipsoid and a slender helical spring, for instance. – Steven Stadnicki Apr 24 '12 at 21:21
• Thanks for the input. And you are correct, my main issue comes up when there is, say, an 'A' shaped object, the BB will make the tensors come back incorrectly. I'll check your info, thanks! – Mungoid Apr 24 '12 at 21:23
• You're welcome - if you'd like me to flesh this out in more detail, I ought to be able to build a proper answer out of it, but that should be enough to get you started. – Steven Stadnicki Apr 24 '12 at 21:25
• If you would be willing to, that would be awesome! I have been trying to figure this out for a while but I am still a bit of a fledgling in this area so I end up getting myself more and more confused =-) – Mungoid Apr 24 '12 at 21:30