How to offset a collision shape correctly?

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

How to offset a collision shape correctly?

Postby Marc » Fri Jul 06, 2012 8:41 am

I'm trying to offset a collision shape relative to my body. It doesn't turn out the way I want it and I'm not really sure if I assume a few things correctly.

I want to attache a cylinder to my body but instead of of having the cylinder along the x axis, I want it along the z axis. So I do this:

Code: Select all
   dMatrix orientation;
   orientation.m_front = dVector (0.0f, 0.0f, 1.0f, 0.0f);         // this is the player up direction
   orientation.m_up    = dVector (1.0f, 0.0f, 0.0f, 0.0f);         // this is the player front direction
   orientation.m_right = orientation.m_front * orientation.m_up;   // this is the player sideway direction
   NewtonCollision *colup = NewtonCreateCylinder(nworldcol,
      r,
      h,
      0,
      &orientation[0][0]
      );


I think that's correct so far.

Now I also want to offset the cylinder along the zaxis so the center of the cylinder is not where the position of the body is. I want the center of the cylinder B be shifted by d relative to the origin of the body A:

Image

So I tried this:

Code: Select all
   dMatrix orientation;
   orientation.m_front = dVector (0.0f, 0.0f, 1.0f, 0.0f);         // this is the player up direction
   orientation.m_up    = dVector (1.0f, 0.0f, 0.0f, 0.0f);         // this is the player front direction
   orientation.m_right = orientation.m_front * orientation.m_up;   // this is the player sideway direction
   orientation.m_posit = dVector (0.0f, 0.0f, d, 1.0f);
   NewtonCollision *colup = NewtonCreateCylinder(nworldcol,
      r,
      h,
      0,
      &orientation[0][0]
      );


I'm not sure if maybe it has to be -d. Or maybe it has to be dVector (+-d, 0.0f, 0.0f, 1.0f) ? I don't know, which one would be correct?

I'm also doing this afterwards

Code: Select all
   dVector origin;
   dVector inertia;
   NewtonConvexCollisionCalculateInertialMatrix (colup, &inertia[0], &origin[0]);   

   body = NewtonCreateBody (nworld, colup);
   NewtonBodySetMassMatrix (body, mass, mass * inertia.m_x, mass * inertia.m_y, mass * inertia.m_z);
   NewtonBodySetCentreOfMass (body, &origin[0]);


Here I'm not sure at all. NewtonConvexCollisionCalculateInertialMatrix calculates the inertia and origin of the collision shape alone, so the offset is not taken into consideration. And then I set the massmatrix and the center of mass again without considering the shifting of d along the z axis. Don't I have to put d in there as well somewhere?
Millenium Project Enterprises - Hobbyist Gamedev Group http://www.mpe-online.org
Walkover is now on Steam => http://store.steampowered.com/app/348700/
User avatar
Marc
 
Posts: 281
Joined: Sun Mar 14, 2004 4:07 pm
Location: Germany

Re: How to offset a collision shape correctly?

Postby Julio Jerez » Fri Jul 06, 2012 9:06 am

this looks correct.
Code: Select all
dMatrix orientation;
   orientation.m_front = dVector (0.0f, 0.0f, 1.0f, 0.0f);         // this is the player up direction
   orientation.m_up    = dVector (1.0f, 0.0f, 0.0f, 0.0f);         // this is the player front direction
   orientation.m_right = orientation.m_front * orientation.m_up;   // this is the player sideway direction
   orientation.m_posit = dVector (0.0f, 0.0f, d, 1.0f);
   NewtonCollision *colup = NewtonCreateCylinder(nworldcol,  r,   h,   0,    &orientation[0][0] );



the center of mass is calculate for the shape, so what you attach the shape to the body, in generat you also set the center of mass and inertia to teh calculated values for that shape.

you can doa a test with tow know object. say yo utake a box with center at 0,0,0
and another with center x/2, 0,0
the momet of intetia and centert of mass will be different, if you attach these tow boxed tow tow bodies they will act differently.
but if you set the origin and momenet of inertai to the respective values they the two bodies will beheve identically.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: How to offset a collision shape correctly?

Postby Marc » Fri Jul 06, 2012 6:31 pm

Thx :)
Millenium Project Enterprises - Hobbyist Gamedev Group http://www.mpe-online.org
Walkover is now on Steam => http://store.steampowered.com/app/348700/
User avatar
Marc
 
Posts: 281
Joined: Sun Mar 14, 2004 4:07 pm
Location: Germany


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest

cron