Converted the comment into an answer.
@Nimred
Yes, it seems you’re right ^^. I just copied his comment into the question but hadn’t a closer look at what he has done there. Your code looks right to me.
Vector3.Scale just does a component wise multiplication. So Vector3.Scale(A,B) == new Vector3(A.xB.x, A.yB.y, A.z*B.z); So one vector scales the other component wise.
Unfortunately there’s no component wise reciprocal operator or method. However we could simply write one ^^:
public static class Vector3Extension
{
public static Vector3 Reciprocal(this Vector3 v)
{
return new Vector3(1f/v.x, 1f/v.y, 1f/v.z);
}
}
With that helper method I’ve created this method which seems to be correct:
public static Vector3 ApplyTensor(Rigidbody rb, Vector3 worldAcceleration)
{
var q = rb.transform.rotation * rb.inertiaTensorRotation;
var qi = Quaternion.Inverse(q);
var acc = qi * worldAcceleration;
acc = Vector3.Scale(acc, rb.inertiaTensor.Reciprocal());
return q * acc;
}
I’ve created a test scene where i added two identical rigidbodies. In FixedUpdate i used
R1.AddTorque(torque,ForceMode.Force);
acc = ApplyTensor(R2, torque);
R2.AddTorque(acc, ForceMode.Acceleration);
And the result is identical. Even when i select both objects, the inspector shows all rotation values as identical. I also tested with a rotated tensor.
edit
The following is actually wrong ^^. I was under the impression that the inertia tensor just contains the “mass distribution” for a given collider setup and the mass would be somehow included in the actual calculation when a force is “converted” to the angular acceleration. However it turns out that it’s not possible to linearly “scale” the inertia tensor by the actual mass.
So what Unity / Physx does is to calculate the inertia tensor based on the given mass and collider setup. The tensor already contains the mass. So the above method is actually correct.
It seems that the “acceleration” mode for torque is internally also dividing the acceleration by the rigidbodies mass. That actually must be a bug in Unity. See ForceMode. The acceleration is not dependent on the mass, only forces are. So usually to get the real “acceleration” you would need to divide by the mass as well.
The same thing seems to apply for “VelocityChange”. VelocityChange and Acceleration both ignore the tensor but still divide by the mass.