Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What is inertia Tensor for Dummies?

Discussion in 'Scripting' started by psyhova, Mar 23, 2014.

  1. psyhova

    psyhova

    Joined:
    Oct 3, 2013
    Posts:
    32
  2. diegzumillo

    diegzumillo

    Joined:
    Jul 26, 2010
    Posts:
    418
    I don't know of any source with a simple explanation. Inertia tensor is a slightly advanced physics concept. But I'll see what I can do :p

    Do you remember basic physics? imagine there's a point mass in space and you want to change its velocity. You do that by applying a force. But how much force? well, that depends on the object, every object responds differently and mathematically we express this by: F = m a. The acceleration (change in velocity) is what we want by applying F, but every object has a specific m.

    ok, now for rotational mechanics things get a bit more complicated but the main idea is that there is an analogy with the previous case. If you want to spin an object (increase its angular velocity) around some axis you have to apply a torque. So we are looking at something like T = I W. Where T is torque, W is angular velocity and I is called moment of inertia and is playing the same role as mass on the previous paragraph, a thing that is particular to that object. But here's the catch: what if you apply the torque in the x axis but the object is spinning in the y axis? or some other combination. To account to every combination possible, and considering we live in a 3D space, I needs to be a 3X3 matrix (also known as tensor).

    Did that help in anything?
     
    Last edited: Mar 23, 2014
    Gulooo, Oranger97, DSivtsov and 17 others like this.
  3. SuperUltraHyper

    SuperUltraHyper

    Joined:
    Sep 7, 2015
    Posts:
    112
    So, practically speaking, what happens if you increase or decrease the inertia tensor?
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    You can go check out this video from unity specifically. If you go to like the 27 or 28 minute mark he talks about the tensor.


    But effectively speaking...

    The inertia tensor is the rotational equivalent of mass in a way.

    If you think about mass as the difficulty it is to accelerate something linearly. Well the moment of inertia, or inertia tensor, is how difficult it is to rotate that object.

    In the case of the 'inertiaTensor' vector, it's the moment of inertia per axis. The x value is how hard it is to rotate around the x-axis, and so on. So larger values mean more force required to rotate it around that axis. Small values mean less force to rotate around that specific axis.
     
    Bunny83, Oyedoyin1, giboulot and 3 others like this.
  5. jeffk1945

    jeffk1945

    Joined:
    Dec 14, 2016
    Posts:
    3
    Old thread but i'll just contribute my 2 cents.

    Intuitively speaking mass is sort of the 'resistance' to being accelerated according to F=ma. A larger mass will accelerate at a lower magnitude for a constant force. This is called inertial mass. Now this is for linear motion.
    Imagine trying to spin a wheel, you have a bicycle wheel and a wheel from a big truck. Torque is basically the force time the moment arm. But most importantly torque is what makes objects rotate or spin, or travel in an angular fashion. so for the same torque applied to the bicycle wheel and the big truck wheel, the bicycle wheel will be easier. That's because it's got less Moment of Inertial (MoI). The Moment of Inertia is the rotational equivalent to mass. Larger MoI, the more difficult it will be to give it angular acceleration.

    In my example i didn't state it but assumed the wheel spins along it's axis. However there are an infinite possibilities to spin this thing. I could spin it along it's spokes, or off to the side, doesn't have to be always through the center. So this Moment of Inertia actually depends on which axis you spin an object. The value of the moment of inertia depends on the geometry, mass, and the axis in which you want to spin something.

    Now we come to the inertial Tensor. I'ts a 3x3 matrix essentially and you know that its x,y,z. Each value say the xx position just represents an axis that the object can spin around. XX position in the tensor being along the x-axis, yy, being along the y-axis, xy being along the diagonal of the xy-plane, etc. I said there are infinite number of axis, but we only choose 6 (not 9) because the xy and yx are the same for example. So lets say you have an inertia tensor of an object. and you want to know what it's moment of inertia would be if you spun it along the z-axis.. just look at the zz position, etc.

    So why does Unitys Inertial Tensor only a Vector3? Well. If you actually take an object and spin it randomly and into the air. it will actually only settle into a rotation only along certain axis. Take a pencil. throw it into the air by spinning it like a plane propeller with axis horizontal, it's stable. Now spin it along the length of the pencil, it's stable. and then finally spin it along it's axis but axis is vertical now, so like helicopter blades. and you'll see it's stable. If you try to spin it any other way, such as like a cone, you can see the rotation changing because it's not stable. There are only three axis it will be stable in and those are called the prinipal axis and those three moments of inertias along those axis are called principal moment of inertias. Since there are only three, that's why Unity simplied it down to a vector3. In the physics books you will still see a 3x3 matrix but the three principle moments of inertia only along the diagonal of the matrix.

    If you do want to learn more. Don't jump into the inertia tensor. First learn moments of inertia, then parallel axis theorem and how to calculate MoI of different shapes along different axis. But yeah it's a bit mathematical you'll have to dig deep into your linear algebra and learn about eigenvectors and eigenvalues and what a matrix really is geometrically, etc.
     
  6. nmw01223

    nmw01223

    Joined:
    May 21, 2020
    Posts:
    6
    Just read your very helpful 2 cents. Don't know if this is too old for you to reply to but:

    From what I understand you to say above, the three fields in the inertial tensor represent MoI round the three axes, usually - in Unity speak - X (across), Y (up), Z (away from you).

    I'm trying to model a quadcopter, so X is the pitch axis (up/down), Z the roll axis (left/right) and Y the yaw axis (turn) - CofG is in the middle. So if I know/guess/calculate/approximate the MoI of the craft around those three axes, I can set the interial tensor fields from those values.

    Then providing I can break down any torque into torque round the X, Y and Z axes, I can apply them to the rigid body (local frame of reference) as torques, and the physics model should handle it, at least to a first approximation.

    Am I right?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,769
    Bunny83 likes this.
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,533
    Yes, that's pretty much right.

    See, the moment of inertia directly follows the linear inertia for point masses. When you have a point mass with a distance of x from the rotation point / axis, when you want to accelerate the point mass around that rotation point, you need a certain torque that is equal to the linear acceleration at that point. The problem we are facing is, the closer a point mass is to the rotation point, the less resistance we get. Since actual objects do not consists of point masses but have a distribution of mass across the 3d space, the moment of inertia is in essence the integral of all the point masses in relation to their distance from the rotation axis.

    Unity uses the eigendecomposition of the 3x3 intertia tensor. What way the tensor can be specified by just providing a rotation matrix (or quaternion which unity uses) and the 3 diagonal values which Unity specifies as a Vector3. Calculating the accurate moment of inertia is quite complex as it requires tensor algebra. For a complex object that is composed of several simpler objects, it's possible to calculate the resulting tensor from the moments of inertia of the individual parts. Here's a list of the most common primitive shapes and their moments of inertia around the principal axes. To combine several objects into one, you would use the parallel axis theorem. Though the eigenvalue / eigenvector decomposition is also not that trivial ^^.

    All in all it's a quite complex topic. Though since we are in a game engine which does not even apply the real world physics correctly, it doesn't really matter how accurate the behaviour is. It just need to feel right.

    For example some time ago someone asked how to simulate a skateboard doing a kickflip in Unity. The simple answer was: you can't. That's because PhysX does not preserve angular momentum (as the real world does) but it simply preserves angular velocity. You may think there's no difference, but there is. This small difference gives us the intermediate axis theorem and thw tumbling effect explained at the end of this video. This will never happen in Unity because again, Unity does not preserve the angular momentum but the angular velocity. So an object where no forces act upon it will continue to rotate around a stable axis at all time. Simulating real world physics gets way too complicated way too fast. So a simply approximation is usually enough for games. Such physics engines (PhysX for 3d Physics and Box2D for Physics2d) can not really be used for scientifically accurate simulations. They contains tons of simplifications.
     
    ALTINEL and nmw01223 like this.
  9. nmw01223

    nmw01223

    Joined:
    May 21, 2020
    Posts:
    6
    Not sure how I have hijacked anything? jeffk2021 made a very helpful post in 2/2021. I simply wanted to ask him a question about his post. Creating a new post would probably not achieve that.
     
    Jirdus likes this.
  10. nmw01223

    nmw01223

    Joined:
    May 21, 2020
    Posts:
    6
    Thanks very much, I need to read that and the links carefully. I don't need anything more than a very basic approximation.

    The craft body is simply represented by a single rectangular block. The mass and X/Y/Z dimensions are set reasonably OK, but the angular momentum wasn't set at all. I was simply taking the thrusts I have calculated round each axis, the distance from the CofG (ie the torque) and turning those into angular acceleration (accel=torque/ang.mom.) with an estimate of the angular momentum for each axis made from the key components and their locations, nothing complicated. The accelerations are then applied to the rigidbody, not the torques, but it does not respond as expected.

    I wondered if this was because although I am applying angular accelerations so it should just do what I say, the body does have an angular momentum, apparently calculated by rigidbody and the collider. Since that is almost certainly wrong, and might be screwing things up. I wanted therefore to try to set the itertial tensor to represent my calculated momentums, and actually set the torques instead.

    (I am not a mechanical engineer, sorry about the terminology).
     
  11. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,533
    It's not really clear what you want to do. However you usually don't need to mess with the inertiatensor at all. Be warned when you do set the inertia tensor manually you essentially disconnect the moment of inertia from the mass of the object. As we explained above the moment of inertia already contains the mass. Unity / PhysX usually calculates the inertia tensor and its rotation automatically based on the compound collider and the mass of the rigidbody. When you set it manually it does no longer has any relation to the colliders or the mass of the rigidbody. You can call ResetInertiaTensor to revert any manual changes so Unity again calculates the moment of inertial automatically from the mass and the collider(s) of the rigidbody.
     
  12. nmw01223

    nmw01223

    Joined:
    May 21, 2020
    Posts:
    6
    I think - disconnecting them is what I want to do (?).

    The body of the craft is represented very simply by a 'flattened cube', with a cube collider of the similar size (a bit bigger in the X/Z dimensions to allow for propeller swing). The body being typically about 120mm width (X) and depth (Z), and 20mm height (Y). It isn't like that in reality of course - but it is pretty symmetrical, CofG at the middle.

    Now I know the craft mass. Say 210g. I know the maximum thrust of the (four) motors - one at each corner mounted vertically. So, in terms of upthrust, it is the sum of the motor thrusts, and I can just apply that as a force up the Y axis (local frame of reference), and with pull of gravity downwards (world frame) it should accelerate appropriately.

    When it comes to rotation, that is different. Rolling right torque (around Z axis) should be caused by the sum of the two LH motor thrusts, minus the sum of the two RH motor thrusts, times the distance of motors to CofG perpendicular to that axis. Similarly pitching forward (around X axis) is two rear motors minus two front motors, and yawing right (around Y axis) is going to be caused by one diagonal pair minus the other though in this case the distance is root 2 bigger.

    But, that is the torque, and it needs to be applied to something of the correct MofI along each axis to produce the right angular accelerations. I did not set the MofI at all - left it as default, but I did estimate the MofI along each axis - it is dominated by the motors at the corners which are the heavy bits (iron magnets). So, I estimated the MofI for pitch or roll to be 4 times distance of motor to axis squared times motor mass (4 times - 4 motors). Then bumped up by 30-50% to allow for all the other 'non motor' bits. Yaw was the same but root 2 times bigger because the distance is root 2 times bigger. Like I said - very approximate.

    So, I did not apply torques to the rigidbody, I applied angular accelerations having worked them out myself from my MofIs. But it occurs to me, the rigibbody is also dealing with other forces, collisions etc, and presumably it is using its own calculated MofIs for that. Since the body is a cube, it will think the mass is distributed evenly, whereas, like I said, it is concentrated in the motors, so I would guess my calculated MofI will be different.

    So, to cut a long story short, what I want to try is setting the MofIs myself from my calculated values using the rigidbody inertial tensor. Hence my original worries about what is an inertial tensor and how does it work - and what are the units.

    I am assuming from all the above that the units are SI (everything else rigidbodywise seems to deal in mtrs, Kgs and secs) and I just therefore set my MofIs in a vector3 (X and Z for pitch and roll, Y for yaw), and save them to the rigidbody in the usual way.

    I hope ....

    I suppose an alternative would be to apply four forces at the motor locations (local frame of reference) and let the rigid body work it all out, but again, that requires setting the MofI for each axis correctly.

    UPDATE:

    Didn't do what I expected at all. On a test 1m sided cube (sitting in mid air) with a rigidbody but no collider, drag of 0 and gravity off, I set the mass and interial tensor from my calculated values. Applied a torque to one axis, and it rotated, applied torque to a different axis and that rotated. So far so good.

    The rotation rate was about 7 (rad/sec). From my calculations it should have been about 100. Bumped the torque by a factor of 10. Still 7 rad/sec, divided it by 10, same. Reversed it, still 7 rad/sec but the opposite rotation. Changed the inertial tensor values, no change. Changed the mass, no change. Added a collider, no change. Very odd.

    Then left the inertial tensor at default values. Now the rotation rate went up and down OK.

    Seems to me therefore that if one sets an explicit MofI using the inertial tensor, the physics engine ignores both that and the applied torque. You just get a fixed rotation rate. No idea why.
     
    Last edited: Mar 25, 2022