Newton Body center of mass [SOLVED]

Report any bugs here and we'll post fixes

Moderators: Sascha Willems, Thomas

Newton Body center of mass [SOLVED]

Postby misho » Thu Mar 07, 2019 8:29 pm

Hi,

How exactly does Newton calculate center of mass (COM) when the body has compound collisions?

I am getting unexplained center of mass offsets, even though my compound collisions are perfectly axial. What I have are rocket parts, basically a bunch of cylinders and cones all stacked on top of each other, that are perfectly aligned axially. However, when I check body's COM:

Code: Select all
NewtonBodyGetCentreOfMass(body, &com[0]);


I get small offsets that shouldn't be there. They are small, but when I fire an axially mounted rocket engine, the small COM offset gives a small torque on lateral axis and the rocket part starts to spin.

I fixed this by forcing COM to zero after I assemble my rocket body from compounds:

Code: Select all
   dVector origin = dVector(0.0f, 0.0f, 0.0f);
   NewtonBodySetCentreOfMass(newEntity->nBody, &origin[0]);


however - if my compounds are symmetrical, exact and aligned, I shouldn't have to do that, correct? Also - I am sure I will one day need to construct a rocket part that's asymmetrical, and I'd like to rely on Newton to compute the exact COM...

Thanks
Misho
Last edited by misho on Fri Mar 08, 2019 2:50 pm, edited 1 time in total.
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 552
Joined: Tue May 04, 2010 10:13 am

Re: Newton Body center of mass

Postby Julio Jerez » Fri Mar 08, 2019 11:48 am

com is calculate use the stand definition
is assumes a uniform density. so mass = density * volume
by definition the com is given by; com = sum (mass(i) * com(i)) / sum (mass(i))
substituting mass with density * volume you get.
Code: Select all
com = sum (density * volume(i) * com(I)) / sum (density * volume(i))
com = density *  sum (volume(i) * com(I)) / (density * sum (volume(i)))

com = sum (volume(i) * com(I)) / sum (volume(i))

this is done on this function
dgVector dgCollisionCompound::CalculateVolumeIntegral (const dgMatrix& globalMatrix, const dgVector& plane, const dgCollisionInstance& parentScale) const


the complex part is where the volume of each shape is calculated, this use a diverge theorem
which calculate the flow if a field over a close area.
since the shapes are convex, them the some area are positive and some are negative the difference is the volume, area or length depend of the field.

the only problem is that the implicit primitives has to be polygonised, so the volume is not the exact value of teh shape but is a very good approximation.

I doudt that's the source of the error, but if you need that level of acuracy, you can just calculate yourself using the above formula, and using axact volumes them explicitly set the com of the body.

what is the magnitude of the error you are getting?
Julio Jerez
Moderator
Moderator
 
Posts: 10993
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Body center of mass

Postby misho » Fri Mar 08, 2019 12:54 pm

Hi Julio,

On an object that is around 13.3 meters long, and is comprised of 9 axially centered cone compound collision objects, I am getting 3 cm in z (up-down) direction, y-axis being longitudinal, and 2 mm in x-direction (lateral axis). These 3 cm are causing a pitch when rocket engine is active.

I should point out that this is the ONLY object that is giving me this error. I have 10 or so other objects that all calculate their COM with x and z near zero (order of magnitude e^-9).

I checked and re-checked, and all compound collision sizes and positions are correct. They might overlap by a very small amount.

I have the ability to selectively load the compounds, so I will go in and check and try to determine at what point I'm getting an error.
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 552
Joined: Tue May 04, 2010 10:13 am

Re: Newton Body center of mass

Postby Julio Jerez » Fri Mar 08, 2019 1:41 pm

3cm on 13 m is too large of a error to be attribute to the polygonazation of the shapes,
my guess is that the model with that error the shape are either overlapping of have gaps.


on the shape with the error, you can run the formula for com I gave you above and check if it produce the same com, each shape sub shape can get the volume and the position.
Julio Jerez
Moderator
Moderator
 
Posts: 10993
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Body center of mass

Postby Julio Jerez » Fri Mar 08, 2019 2:26 pm

umm you may be right, there is a bug coming from the change I made to make the dgVector class a 4d only. It is here on this function
Code: Select all
void dgCollisionConvex::MassProperties ()
{
   m_centerOfMass.m_w = dgCollisionConvex::CalculateMassProperties (dgGetIdentityMatrix(), m_inertia, m_crossInertia, m_centerOfMass);
   if (m_centerOfMass.m_w < DG_MAX_MIN_VOLUME) {
      m_centerOfMass.m_w = DG_MAX_MIN_VOLUME;
   }
   dgFloat32 invVolume = dgFloat32 (1.0f) / m_centerOfMass.m_w;
   m_inertia = m_inertia.Scale (invVolume);
   m_crossInertia = m_crossInertia.Scale (invVolume);
   m_centerOfMass = m_centerOfMass.Scale (invVolume);

   // complete the calculation
   dgCollision::MassProperties ();
}


if you look at line m_centerOfMass = m_centerOfMass.Scale (invVolume);

the value w in the shape volume, but I am scaling by the inv of the volume so the become, so some shapes will be wrong and some will be right.
This is a major bug.

Let me re check again before I made the fix. but I am very confident that's the problem and a big one.
Julio Jerez
Moderator
Moderator
 
Posts: 10993
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Body center of mass

Postby misho » Fri Mar 08, 2019 2:50 pm

Ok - I am relieved to find that this was my error! I was missing a comma in my configuration file, so the properties read in were in incorrect order! I can't believe this, I was triple-checking the values, but failed to notice comma was missing :roll:

Thanks!
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 552
Joined: Tue May 04, 2010 10:13 am

Re: Newton Body center of mass [SOLVED]

Postby Julio Jerez » Fri Mar 08, 2019 3:09 pm

glad you found it, but there was also a serious bug in the calculation of the volume of convex shapes.
you may now be getting the correct values, but that only because your model has lot of symmetry.

I just committed the fix, and now the buoyancy that use that functionality a lot work as expected. before only boxes and shape were right because they used the close form formulae for calculation the volume.
your shape use cylinder which are polygonised because it does use the equation for cross inertia.
The general converge and divergence theorem function calculate all mass properties:
volume, center of mass, inertia and cross inertia.
this is why is used for all shapes other than boxes and spheres.

Please make a backup and sync so that you get the fix, this is a serious problem that can affect many things.
Julio Jerez
Moderator
Moderator
 
Posts: 10993
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Newton Body center of mass [SOLVED]

Postby misho » Fri Mar 08, 2019 3:16 pm

Wow - great! I'm glad that my "fake" bug prompted the discovery and the fix of the "real" one! :mrgreen:

Thanks Julio!
Misho Katulic
CTO, FSX SpacePort
TerraBuilder
www.terrabuilder.com
misho
 
Posts: 552
Joined: Tue May 04, 2010 10:13 am


Return to Bugs and Fixes

Who is online

Users browsing this forum: No registered users and 9 guests