colliders in Unity

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

colliders in Unity

Postby blackbird_dream » Tue Apr 03, 2018 4:29 am

When I add a Newtoncollider (for instance a box collider) to a GameObject holding a Newton body, the CoM is automatically moved to the center of the collision box. Is it normal ?
In my opinion , No as in previous version of ND it was possible to add a translationmatrix as argument of the function Newtoncreatebox
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: colliders in Unity

Postby Sweenie » Tue Apr 03, 2018 4:56 am

The matrix passed to NewtonCreateBox wasn't the com offset but was and still is the collider offset matrix. In older Newton I believe the com was at 0,0,0 by default and could be set manually by the user. In never Newton this com can still be set manually but the new function SetMassProperties(which the plugin uses) automatically calculates both inertia and com based on the collider shapes.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: colliders in Unity

Postby Sweenie » Tue Apr 03, 2018 4:59 am

Didn't you add some custom code to override the com?
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: colliders in Unity

Postby blackbird_dream » Tue Apr 03, 2018 8:28 am

yes you are right it was the offset matrix with respect to the NewtonBody reference frame and the CoM was set at its origin by default. So we could shift the collision box from the CoM as needed.
I did'nt try yet but I'll have a look at the source , maybe I am able to override the Com oncreate.
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: colliders in Unity

Postby blackbird_dream » Tue Apr 03, 2018 8:40 am

You are saying that in Unity when you add a collision box to a Nbody the plugin doesn't take into account the CoM position exposed in the editor fields anymore ?
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: colliders in Unity

Postby Sweenie » Tue Apr 03, 2018 8:55 am

Overriding the com with a custom value should be exposed in Unity by default of course but i haven't had much time in a while to maintain the Unity plugin, mostly because I'm not using Unity myself anymore. I've spent most of my time on a custom engine(nothing advanced, mostly for fun and easy prototyping) and recently the Godot engine caught my attention. It's so much easier to integrate external apis in Godot since you can write custom modules in c++ which integrates directly into the engine and editor instead of creating c# wrappers on top of the engine like in Unity.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: colliders in Unity

Postby Sweenie » Tue Apr 03, 2018 9:00 am

Hmm, checked the code and you are right, it does use the com defined in the editor.
Code: Select all
   public virtual void InitRigidBody()
    {
        CreateBodyAndCollision();

        SetCenterOfMass();

        m_body.SetLinearDamping(m_linearDamping);
        m_body.SetAngularDamping(m_angularDamping.x, m_angularDamping.y, m_angularDamping.z);

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

        m_world.RegisterBody(this);
    }

    void SetCenterOfMass ()
    {
        m_body.SetCenterOfMass(m_centerOfMass.x, m_centerOfMass.y, m_centerOfMass.z);
    }


I think I've might confused it with the inertia.
Maybe it was the inertia you wanted to define yourself instead of letting ND calculate it for you.
In the code above CreateBodyAndCollision creates the body and calculate inertia and com. But directly after it calls SetCenterOfMass which will set the com as defined in the editor.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: colliders in Unity

Postby blackbird_dream » Tue Apr 03, 2018 10:00 am

it doesn't in practice.
The CoM is calculated from the collider and not from the values exposed in the editor.
I built a demo to show it :
https://ufile.io/bjvrr
a simple cube with a horizontal hinge at its base srtaight under its CoM (according to the values edited). the collision is shifted horizontally. As a result the cube rotates. So the CoM was shifted with the collider.
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: colliders in Unity

Postby Sweenie » Tue Apr 03, 2018 10:08 am

ah, should have looked a little deeper.
Assumed SetCenterOfMass only set the mass directly, it didn't.
Code: Select all
void dNewtonBody::SetCenterOfMass(float com_x, float com_y, float com_z)
{
   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));
   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]);
}


It calculates the mass and then add the editor defined com as an offset to that calculated origin.

I think this way is a bit too confusing. If and when I get the time it should probably just a checkbox in the editor called "Use custom COM" and it checked the body directly set the userdefined COM instead of offsetting it from a calculated center.
Sweenie
 
Posts: 498
Joined: Mon Jan 24, 2005 7:59 am
Location: Sweden

Re: colliders in Unity

Postby blackbird_dream » Tue Apr 03, 2018 10:48 am

if I understand well I just need to modify SetCenterOfMass :

Code: Select all
    void SetCenterOfMass ()
    {

   NewtonBodySetMassmatrix(m_body, m_mass, m_Ixx,m_Iyy,m_Izz);
   NewtonBodySetCentreOfMass(m_body, &m_centerOfMass[0]); 
  }


and expose m_Ixx,m_Iyy,m_Izz the same way as m_mass ?

Then the mass properties are completely independent from the collider
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: colliders in Unity

Postby Julio Jerez » Tue Apr 03, 2018 10:51 am

Maybe a trick can be add-in a com offset to the editor.
Then use the shape com + editioN as the com
Then that way the com act as the reference point for body com. Not check needed
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: colliders in Unity

Postby blackbird_dream » Tue Apr 03, 2018 10:54 am

I need my own inertia, my own mass, my own CoM and my own collisionbox all independent.
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: colliders in Unity

Postby blackbird_dream » Wed Apr 04, 2018 8:40 am

ok I understand what you mean.
Because dNewtonDynamicBody::dNewtonDynamicBody creates the newtonbody in the collision's centre, it's easier to change the Com back from the editor's values afterwards.
I did it and it seems to work.
The ideal solution would be to set the CoM from the collider's position values set in the editor automatically.

Or an even better solution would to modify dNewtonDynamicBody::dNewtonDynamicBody
especially :
Code: Select all
   m_body = NewtonCreateDynamicBody(newton, collision->m_shape, &matrix[0][0]);
and put the CoM shift in matrix so that by default the Newtonbody keeps in position and isn't shifted to the collider's centre.

PS : I had to expose SetMassMatrix first in Newton.h and Newton.cpp
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Re: colliders in Unity

Postby blackbird_dream » Wed Apr 04, 2018 10:05 am

is there someting to retrieve the collision's CoM ?

something like m_collision.GetShape().m_centerOfMass.x ?

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

Re: colliders in Unity

Postby blackbird_dream » Wed Apr 04, 2018 10:23 am

I don't understand your function void dNewtonBody::SetCenterOfMass(float com_x, float com_y, float com_z)
what is the meaning of :
Code: Select all
   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]);
User avatar
blackbird_dream
 
Posts: 354
Joined: Wed Jun 07, 2006 3:08 pm
Location: France

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 25 guests

cron