no inertia in Editor

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

no inertia in Editor

Postby blackbird_dream » Wed Dec 06, 2017 5:06 am

Arghhh
There is no field for inertia in the Newton Body Editor. Only Mass and CoM.
Is it possible to add Ixx,Iyy,Izz,Ixy,Ixz and Iyz ?
Or I must create a new childclass in a script, to set these values?
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: no inertia in Editor

Postby blackbird_dream » Wed Dec 06, 2017 6:10 am

I'm having a look at dNewtonBody.cpp
I guess I should begin with substituting NewtonBodysetMassMatrix for NewtonBodysetMassProperties.
This leads me to the pointer to the colllision object as argument of NewtonBodysetMassProperties.why is it needed ?
Last edited by blackbird_dream on Wed Dec 06, 2017 11:44 am, edited 1 time in total.
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: no inertia in Editor

Postby blackbird_dream » Wed Dec 06, 2017 10:20 am

Well, I changed all the occurences of NewtonBodySetMassProperties(m_body, mass, NewtonBodyGetCollision(m_body)); with NewtonBodySetMassMatrix(m_body, mass, Ixx,Iyy,Izz); inside dNewtonBody.cpp

I am wondering now how to add the editboxes for Ixx,Iyy and Izz in the NewtonBody (script) Editor.
I suspect I can't as all the stuffs are in the precompiled NewtonPluginEditor.dll. Am I correct ?
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: no inertia in Editor

Postby Sweenie » Wed Dec 06, 2017 11:24 am

Well, both the NewtonPlugin and NewtonPluginEditor source can be modified.

We haven't created a custom editor for the NewtonBody class yet so mass and com are just public properties of the NewtonBody class(which can be edited in the Unity Editor as well).
You can add a custom inertia property by editing NewtonBody.cs inside the NewtonPlugin folder.

A custom editor class just allows you to create more advanced editor controls.

If you modify the Newtonwrapper(the c++ part) you will have to close Unity first before compiling, as Unity won't hot reload this dll.
But if you only change the NewtonPlugin or NewtonEditorPlugin Unity will automatically detect this and hot reload those dlls.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: no inertia in Editor

Postby blackbird_dream » Wed Dec 06, 2017 11:43 am

oh yes thks.
What about the pointer to the collision in NewtonBodySetMassProperties. Do you know the reason for this argument in the function ?
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: no inertia in Editor

Postby Julio Jerez » Wed Dec 06, 2017 12:34 pm

the pointer to the collision shape is for calculating the inerta distrbution and the center of mass. it does not have to be the same as the collison on the rigid body, in fact in many cases is not.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: no inertia in Editor

Postby blackbird_dream » Wed Dec 06, 2017 1:14 pm

Ok that's what I supposed too .
so may be in the Editor I should let the possibiliy for calculating the inertia from the collision box if desired.
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: no inertia in Editor

Postby blackbird_dream » Thu Dec 07, 2017 5:20 am

What I did :
changed line 63 of dNewtonBody.h :
Code: Select all
   void SetCenterOfMass(float com_x, float com_y, float com_z, float Ixx, float Iyy, float Izz);

changed dNewtonBody.cpp
Code: Select all
void dNewtonBody::SetCenterOfMass(float com_x, float com_y, float com_z, float Ixx, float Iyy, float Izz)
{
   dVector com;
   //dFloat Ixx;
   //dFloat Iyy;
   //dFloat Izz;
   dFloat mass;

   NewtonBodyGetMass(m_body, &mass, &Ixx, &Iyy, &Izz);
   NewtonCollision* const collision = NewtonBodyGetCollision(m_body);
//   NewtonBodySetMassProperties(m_body, mass, NewtonBodyGetCollision(m_body));
   NewtonBodySetMassMatrix(m_body, mass, Ixx,Iyy,Izz);
   NewtonBodyGetCentreOfMass (m_body, &com[0]);
   com.m_x += com_x;
   com.m_y += com_y;
   com.m_z += com_z;
   NewtonBodySetCentreOfMass(m_body, &com[0]);
}

and in dNewtonKinematicBody::dNewtonKinematicBody
added
Code: Select all
   dFloat Ixx;
   dFloat Iyy;
   dFloat Izz;

and substitute :
Code: Select all
   NewtonBodyGetMass(m_body, &mass, &Ixx, &Iyy, &Izz);
   NewtonBodySetMassMatrix(m_body, mass, Ixx, Iyy, Izz);

for
Code: Select all
NewtonBodySetMassProperties(m_body, mass, NewtonBodyGetCollision(m_body));

same thing with dNewtonDynamicBody::dNewtonDynamicBody
in NewtonBody.cs added :
Code: Select all
    public Vector3 m_inertia = new Vector3(0.0f, 0.0f, 0.0f);
and changed SetcenterOfMass l94 :
Code: Select all
        m_body.SetCenterOfMass(m_centerOfMass.x, m_centerOfMass.y, m_centerOfMass.z,m_inertia.x,m_inertia.y,m_inertia.z);


But I'm not sure the inertia parameters are well initialized from the Editor inputs prior I call NewtonBodyGetMass
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: no inertia in Editor

Postby Sweenie » Thu Dec 07, 2017 6:39 am

Hi.

For the function dNewtonBody::SetCenterOfMass you added the function params Ixx, Iyy, Izz and uncommented the local variables Ixx, Iyy, Izz.

This call
Code: Select all
NewtonBodyGetMass(m_body, &mass, &Ixx, &Iyy, &Izz);

overwrites you functions parameters instead setting them to the inertia the body already has.

Keep the local variables but rename them like this...

Code: Select all
void dNewtonBody::SetCenterOfMass(float com_x, float com_y, float com_z, float Ixx, float Iyy, float Izz)
{
   dVector com;
   dFloat tmp_Ixx;
   dFloat tmp_Iyy;
   dFloat tmp_Izz;
   dFloat mass;

// This call is only used to get the mass value which whas set when the body was created, the inertia retrieved from this call is of no interest.
   NewtonBodyGetMass(m_body, &mass, &tmp_Ixx, &tmp_Iyy, &tmp_Izz);

//   NewtonCollision* const collision = NewtonBodyGetCollision(m_body);
//   NewtonBodySetMassProperties(m_body, mass, NewtonBodyGetCollision(m_body));

// Set the mass again(was already set during body creation) and our supplied inertia values.
   NewtonBodySetMassMatrix(m_body, mass, Ixx,Iyy,Izz);

   NewtonBodyGetCentreOfMass (m_body, &com[0]);
   com.m_x += com_x;
   com.m_y += com_y;
   com.m_z += com_z;
   NewtonBodySetCentreOfMass(m_body, &com[0]);
}


You can undo your changes to dNewtonDynamicBody::dNewtonDynamicBody and dNewtonKinematicBody::dNewtonKinematicBody because that happens before dNewtonBody::SetCenterOfMass is called.

This is what happens in Unity at the time the body is created...
Code: Select all
    public virtual void InitRigidBody()
    {
        // This will create the Dynamic or Kinetic body using the mass parameter from NewtonBody class and calculate inertia based on the body collider.
        CreateBodyAndCollision();

// This will adjust the center of mass and with your modification overwrite the intertia that was calculated in the above call.
        SetCenterOfMass();

        var handle = GCHandle.Alloc(this);
        m_body.SetUserData(GCHandle.ToIntPtr(handle));

        m_world.RegisterBody(this);
    }



If guess you could change this
Code: Select all
NewtonBodySetMassMatrix(m_body, mass, Ixx,Iyy,Izz);


to this...
This should make the custom inertia optional, that is, if you supply zero-values the calculated inertia will be used, if not your custom inertia will be used.
Code: Select all
if(Ixx > 0 && Iyy > 0 && Izz > 0)
{
  NewtonBodySetMassMatrix(m_body, mass, Ixx,Iyy,Izz);
}
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: no inertia in Editor

Postby blackbird_dream » Thu Dec 07, 2017 9:05 am

Thks
Yes I thought doing like that but if you want to model a pure mass it doesn't work.
Maybe a toggle instead.
I added a bool variable reachable from the editor:
Code: Select all
    public bool m_CalculateInertia;

I added a bool variable as input of SetCenterOfMass. In dNewtonBody.h :
Code: Select all
   void SetCenterOfMass(float com_x, float com_y, float com_z, float Ixx, float Iyy, float Izz,bool Calc_inertia);

in dNewtonBody.cpp :
Code: Select all
void dNewtonBody::SetCenterOfMass(float com_x, float com_y, float com_z, float Ixx, float Iyy, float Izz, bool Calc_inertia)
{
   dVector com;
   dFloat tmp_Ixx;
   dFloat tmp_Iyy;
   dFloat tmp_Izz;
   dFloat mass;

   NewtonBodyGetMass(m_body, &mass, &tmp_Ixx, &tmp_Iyy, &tmp_Izz);
   NewtonCollision* const collision = NewtonBodyGetCollision(m_body);
   if (Calc_inertia) {
      NewtonBodySetMassProperties(m_body, mass, NewtonBodyGetCollision(m_body));
         }
   else
   {
   NewtonBodySetMassMatrix(m_body, mass, Ixx,Iyy,Izz);
   NewtonBodyGetCentreOfMass (m_body, &com[0]);
   }
   com.m_x += com_x;
   com.m_y += com_y;
   com.m_z += com_z;
   NewtonBodySetCentreOfMass(m_body, &com[0]);
}

in NewtonBody.cs :
Code: Select all
    void SetCenterOfMass ()
    {
        m_body.SetCenterOfMass(m_centerOfMass.x, m_centerOfMass.y, m_centerOfMass.z, m_inertiaIxxIyyIzz.x, m_inertiaIxxIyyIzz.y, m_inertiaIxxIyyIzz.z, m_CalculateInertia);
    }
Last edited by blackbird_dream on Thu Dec 07, 2017 10:17 am, edited 1 time in total.
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: no inertia in Editor

Postby blackbird_dream » Thu Dec 07, 2017 10:16 am

Is it correct like that ?
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: no inertia in Editor

Postby Sweenie » Thu Dec 07, 2017 11:37 am

I think so.

A bit unsure about the center of mass though.

NewtonBodySetMassProperties should calculate the center of mass as well i remember correctly.

The unmodified version(our version) called NewtonBodySetMassProperties to calc inertia and com.
Then called NewtonBodyGetCentreOfMass to get the calculated com
then offset that com with the com values in the editor
and finally set the new com with NewtonBodySetCentreOfMass.

So if com is 0,0,0 in the editor, the calculated com would not be offset in any way.

I think maybe this would be better, assuming you don't want the com calculated either.
Code: Select all
if (Calc_inertia) {
      NewtonBodySetMassProperties(m_body, mass, NewtonBodyGetCollision(m_body));
      NewtonBodyGetCentreOfMass (m_body, &com[0]);
         }
   else
   {
     NewtonBodySetMassMatrix(m_body, mass, Ixx,Iyy,Izz);
     com.m_x = 0;
     com.m_y = 0;
     com.m_z = 0;
   }
   com.m_x += com_x;
   com.m_y += com_y;
   com.m_z += com_z;
   NewtonBodySetCentreOfMass(m_body, &com[0]);
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: no inertia in Editor

Postby blackbird_dream » Thu Dec 07, 2017 11:47 am

ok thks
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 12 guests