Compound Shapes / Center of Mass / Inertia Questions

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Compound Shapes / Center of Mass / Inertia Questions

Postby Bill » Fri Jun 26, 2015 2:33 pm

Hello,

I've been using Newton a little bit for a few months now and I really love it. I'm making some progress, but I still have some issues that I was hoping I might get some guidance on. My general questions relate to compound shapes formed from convex hulls of meshes, center of mass, and mass moment of inertia values. I thought I might approach my questions by way of a simple example...

On the documentation page for NewtonCreateCompoundCollision (http://newtondynamics.com/wiki/index.php5?title=NewtonCreateCompoundCollision), the example is given of using a compound collision object to create a snowman from three spheres. I'd like to do that, but I'd like to use three meshes instead of three sphere primitives, just as an exercise to prepare me for using meshes with more complicated shapes down the road.

I'll start with something basic, just to make sure I'm somewhat on the right track...

I'd like the origin of the local coordinate system for my snowman sphere meshes to be at the base of the snowman where it would touch the ground, with +Z pointing up (out of the ground). So, for example, let's say my snowman will be three 1 meter diameter spheres. The bottom sphere mesh (sphere1) will be centered at (0,0,0.5), the middle sphere mesh (sphere2) will be centered at (0,0,1.5), and the top sphere mesh (sphere3) will be centered at (0,0,2.5).

In this scenario, where is the center of mass of the snowman body, assuming I don't explicitly set it myself? Is it at (0,0,0), or is it at (0,0,1.5)?

A related question: if I specify mass moment of inertia values for the body, how should they be specified? Should they be about / with respect to the center of mass location that I set, or should they be about / with respect to (0,0,0)?

Another related question: is there any negative to having my meshes located as described above, or should I just not do this and instead always strive to put my mesh's local coordinate system origin at the compound object's center of mass?

I apologize if this has been covered somewhere before...I searched everywhere and came up empty! If anyone has some guidance on this, or can point me to some example, it would be greatly appreciated.

Thanks a lot,
Bill
Bill
 
Posts: 22
Joined: Mon Oct 27, 2014 9:40 am

Re: Compound Shapes / Center of Mass / Inertia Questions

Postby JoeJ » Fri Jun 26, 2015 5:24 pm

Bill wrote:In this scenario, where is the center of mass of the snowman body, assuming I don't explicitly set it myself? Is it at (0,0,0), or is it at (0,0,1.5)?


This a simple mass weighted average of the bodies center of mass.
E.g. you have 2 bodies:
compoundCom = b1.mass * b1.com + b2.mass * b2.com / (b1.mass + b2.mass);

Bill wrote:A related question: if I specify mass moment of inertia values for the body, how should they be specified?


Less simple...
To calculate inertia i have a code snippet that does this for 2 bodies (U & L).
The compound center of mass is already known (com),
also each bodies uniform inertia is known (sphere or cube has uniform inertia).
SqL means squared length.
axis = direction unit vector at that you want to know inertia. Usually you call this for each local axis of the body to get the 3 inertia values

Code: Select all
float GetInertia (sVec3 axis, sVec3 comU, sVec3 comL, sVec3 com, float massU, float massL, float iU, float iL)
   {
      sVec3 d;
      float inertia = 0;

      d = comL - com;   
      inertia += massL * sVec3(axis * axis.Dot (d) - d).SqL(); // effect of point mass from body L
   
            d = comU - com;   
      inertia += massU * sVec3(axis * axis.Dot (d) - d).SqL();

      inertia += iU + iL; // add the inertia of both bodies as well

      return inertia;
   }


:?: I don't know how to do this for nonuniform bodies - that would be very interesting...

However, usually you don't need to bother with this - Newton does it automatically for you.


Bill wrote:Another related question: is there any negative to having my meshes located as described above, or should I just not do this and instead always strive to put my mesh's local coordinate system origin at the compound object's center of mass?


You can do like you want, but because physics are harder than graphics, i prefer to keep object center at com. I's easier to deal with a graphical offset than with a physical lever that you need to take into account when applying forces.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Compound Shapes / Center of Mass / Inertia Questions

Postby JoeJ » Sat Jun 27, 2015 2:36 am

JoeJ wrote::?: I don't know how to do this for nonuniform bodies - that would be very interesting...


I think it should be very easy:

// for each body do...
vec localAxis = subBody.matrix.Unrotate(axis); // transform axis of interest (from code above) to local body space
localAxis.x *= subBody.iXX; // multiply each component by body inertia
localAxis.y *= subBody.iYY;
localAxis.z *= subBody.iZZ;
inertia += localAxis.length(); // sum up inerta at axis of interest

... but i have not tested this.


EDIT:

I was wrong, the correct code to calc inertia about a local axis should be:

localAxis[0] /= Ixx;
localAxis[1] /= Iyy;
localAxis[2] /= Izz;
inertia = 1.0f / localAxis.Length();
Last edited by JoeJ on Sat Aug 08, 2015 8:15 am, edited 1 time in total.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Compound Shapes / Center of Mass / Inertia Questions

Postby Bill » Sat Jun 27, 2015 9:46 am

Hi JoeJ,

Thanks for the replies and info. I think I actually wasn't quite specific enough with my mass moment of inertia question...I'll try again with some more detail.

In this case, I know the mass moment of inertia values for the compound snowman object because I modeled it in a CAD program and it reports to me the mass moments of inertia. It gives them to me two ways: with respect to the snowman's CG (0,0,1.5) and with respect to the snowman's local coordinate system origin (0,0,0).

When I set the body's mass moments of inertia in Newton, which set of mass moment of inertia values do I need to use in order to model it correctly?

This is obviously a simple example, but I'm just trying to understand exactly what Newton requires so I can do this correctly in the future for more complicated objects (such as an airplane or car or something like that).

Thanks a lot,
Bill
Bill
 
Posts: 22
Joined: Mon Oct 27, 2014 9:40 am

Re: Compound Shapes / Center of Mass / Inertia Questions

Postby JoeJ » Sat Jun 27, 2015 10:39 am

Ah, ok - so you need the values from CG.
But to be sure you should compare your values from CAD with the values Newton automatically calculates either by

NewtonBodySetMassProperties (body, mass, shape); // calculate
float mass, Ixx, Iyy, Izz;
NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz); // read computed values

or

NewtonConvexCollisionCalculateInertialMatrix (shape, &inertia[0], &origin[0]);
NewtonBodySetMassMatrix (body, mass, mass * inertia[0], mass * inertia[1], mass * inertia[2]);

If they don't match try a simple sphere first.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Compound Shapes / Center of Mass / Inertia Questions

Postby Julio Jerez » Sat Jun 27, 2015 10:52 am

In newton when you create a collision shape you can call function
void NewtonBodySetMassProperties (const NewtonBody* const bodyPtr, dFloat mass, const NewtonCollision* const collisionPtr)
the function calculate the full inert matrix, regarless of the origin and codenames system
the low lever function is this, you can asset a brak point
Code: Select all
void dgBody::SetMassProperties (dgFloat32 mass, const dgCollisionInstance* const collision)
{
   // using general central theorem, to extract the Inertia relative to the center of mass
   //IIorigin = IImatrix - unitmass * [(displacemnet % displacemnet) * identityMatrix - transpose(displacement) * displacement)];
   dgMatrix inertia (collision->CalculateInertia());

   dgVector origin (inertia.m_posit);
   dgFloat32 mag = origin % origin;
   for (dgInt32 i = 0; i < 3; i ++) {
      inertia[i][i] = (inertia[i][i] - (mag - origin[i] * origin[i])) * mass;
      for (dgInt32 j = i + 1; j < 3; j ++) {
         dgFloat32 crossIJ = origin[i] * origin[j];
         inertia[i][j] = (inertia[i][j] + crossIJ) * mass;
         inertia[j][i] = (inertia[j][i] + crossIJ) * mass;
      }
   }

   // although the engine fully supports asymmetric inertia, I will ignore cross inertia for now
   SetMassMatrix(mass, inertia[0][0], inertia[1][1], inertia[2][2]);
   SetCentreOfMass(origin);
}


there is a compromise that I make, since almost 98% of the cases, object are build aligned to some principal axis of inertia, this means that the cross product are usually zero or much smaller that the diagonal, so I disregard them.

If you want the to be part of the simulation you will have to rotate and translate your shape to match the centre of inertia and the principal axis before you build it. this way the cross product will be negligible.

like Joe say, if you want to verify how close the the actually inertia the afte you set using the above function, yo can do this:
1-set the body to align to an identity matrix
2 call NewtonBodyGetInertiaMatrix(const NewtonBody* const body, dFloat* const inertiaMatrix);

this will give you the full matrix of inertia and you can compare that the one you get form you auto cad.
The reason I neglected the full matrix of inertia in the this require almost tripling the calculation plus another extra matrix to store for the principal alignment matrix

basically each calculation will require a similar transformation of the type: inertial = R * I * Rt
but since in almost 98% of the carses R is identity or near identity, it does not justify the extra storage and extra calculation went the app can simple align the shape R.

I think you can probably ask autocad to rotate the body so that the shape is aligned to the principal axis of inertia. if you can do that the Newton and autocad should agree 100%
if not the are function in the math libraru that you can use for that, if you do not have them already.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Compound Shapes / Center of Mass / Inertia Questions

Postby Julio Jerez » Sat Jun 27, 2015 11:20 am

Are you doing some precise simulation that you required full inertia tensor?
These is very eassy to support, I can add a define like

#define DF_USEF_FULL_INERTIA
the I can add a function like
NetwonSetFullInertiaAndCom (body, inertia, com)

then when the define is off it will call the normal one, else the full matrix
all other function will use the full matrix no jus the diagonal

by I can no make the permanent because of the memory and calculation.
but it will be very good for robotic stuff.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Compound Shapes / Center of Mass / Inertia Questions

Postby Bill » Sat Jun 27, 2015 5:14 pm

Hi Julio,

Thanks for your help and thanks for writing such a great physics library.

Having the option of putting Newton into a mode that would let me supply the full mass moment of inertia matrix would be great. If you added that feature, I would certainly use it. I have some more complicated stuff I'd like to do down the road from now and this feature would be handy.

But first, I was hoping we could step back to my "trivial" snowman example for a minute, just to make sure I'm understanding everything correctly:

My snowman mesh occupy the space from -0.5 < X < 0.5, -0.5 < Y < 0.5, 0 < Z < 3. In this case, its principal mass moment of inertia axes are exactly aligned with X, Y, and Z. So, in its local coordinate system, the off-diagonal terms of the mass moment of inertia matrix are zero.

In Newton, I'm setting the center of mass where I want it to be (for example, I'm setting it to (0,0,1.5), because each of the three spheres are identical).

Then, I'm setting Ixx, Iyy, and Izz to values that represent the principal mass moments of inertia about the center of gravity. In other words, I think what you guys are telling me is that it would be incorrect to set them to mass moment of inertia values about the origin (0,0,0), as Newton expects to receive mass moment of inertia values about the center of gravity.

Is all of that correct so far?

Assuming that is correct so far, do I need to do anything else to get physically-realistic behavior with respect to things like collisions and gravity forces? Based on my experiments so far, it seems like I don't need to do anything else, but I just wanted to check. The reason I ask is because I was concerned about a remark on the following Wiki page:

http://newtondynamics.com/wiki/index.ph ... ntreOfMass

Specifically, I didn't quite understand the section that starts with "Care must be taken when offsetting the centre of mass of a body...". Is that irrelevant to my specific use case, where my only forces and torques are from gravity and collisions?

I really appreciate the help and support you guys have given me on this. I know this must seem pointless...I could just use sphere primitives, let Newton calculate all the mass properties, and be done with it! But, what I'm headed towards is using Newton to simulate objects in which I physically measure their mass properties experimentally and then apply them to the Newton models. That is, their actual mass moments and center of mass won't be exactly what you'd get by calculating them from the meshes directly (due to material property variation, internal voids, etc.). I'm just trying to make sure I understand exactly how Newton works so I don't make an error down the road from now when I'm working with these more complex cases.

Thanks again,
Bill
Bill
 
Posts: 22
Joined: Mon Oct 27, 2014 9:40 am

Re: Compound Shapes / Center of Mass / Inertia Questions

Postby Julio Jerez » Sat Jun 27, 2015 6:17 pm

if you use this function
Code: Select all
NewtonBodySetMassProperties (const NewtonBody* const bodyPtr, dFloat mass, const NewtonCollision* const collisionPtr)


it will set everything correctly, Inertia and center of mass, the only thing it will neglect is the cross product of inertia, for the reasons I explained before.

With next commit 3.14 I will add a compiled version for use full inertia.
I will also move all the configurations to a configuration header because ether are about half a dozen already
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Compound Shapes / Center of Mass / Inertia Questions

Postby Julio Jerez » Sat Jun 27, 2015 10:47 pm

Ok since this is so simple I just added the compile time define
//#define DG_USE_FULL_INERTIA_MATRIX

by uncomment that and now can simple use
NewtonBodySetMassProperties (const NewtonBody* const bodyPtr, dFloat mass, const NewtonCollision* const collisionPtr)

and the engine will use the full intertia tensor of th shape.

alrernatvalle you can also use new function
void NewtonBodySetFullMassMatrix (const NewtonBody* const body, dFloat mass, const dFloat* const inertiaMatrix);

where you place the inertia matrix on a 4 x 4 matrix. you also have to set the COG so that

and that will use the inertia matrix the you get from you cad program. with the engine now support general full inertia.

The option is disabled you need to uncomment and compile the project, this most applications do not required that level of precision. and even without the option being clever it can done it, but you are right it is not a physic engine of it can not handle full inertia matrix and we can no have that, can :D we.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Compound Shapes / Center of Mass / Inertia Questions

Postby zak » Sun Jun 28, 2015 6:19 am

I think it's time to create a simple doc with all #define can be commented/uncommented and their file location
( #define for Windows, iOS or Android, for double/float, for single thread, for SSE, for full inertia matrix ecc. ecc.)
zak
 
Posts: 87
Joined: Mon Dec 06, 2004 9:30 am

Re: Compound Shapes / Center of Mass / Inertia Questions

Postby Bill » Sun Jun 28, 2015 10:28 am

Hi Julio,

Thanks a lot for adding the full mass moment of inertia tensor option...that will definitely come in handy! I think I understand when I need to use this and when I don't, but I'd like to just double-check to be safe...

The way I'm understanding what you've said is that if all of my objects are oriented such that the off-diagonal elements of their mass moment of inertia tensors are zero, then enabling the full tensor option will have no impact on my simulations. In other words, this new option you've added really only comes into play if and when I've modeled my objects in an orientation that gives them non-zero off-diagonal moment of inertia tensor components. Is that correct?

So, here are the functions I'm calling right now in my sample code...does this look OK for situations where I want to set the CG and mass moments to my own values?

Code: Select all
// Call this to set the mass
NewtonBodySetMassProperties

// Call this to set the center of mass to wherever I want it to be
NewtonBodySetCentreOfMass

// Call this to set Ixx, Iyy, and Izz
NewtonBodySetMassMatrix


Thanks again,
Bill
Bill
 
Posts: 22
Joined: Mon Oct 27, 2014 9:40 am

Re: Compound Shapes / Center of Mass / Inertia Questions

Postby Julio Jerez » Sun Jun 28, 2015 10:38 am

yes that is correct.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 3 guests