Static boxes and treecollision ignore model offset.

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Static boxes and treecollision ignore model offset.

Postby andylhansen » Sun Jun 12, 2011 12:22 am

In my program, when I create either a static box or collisiontree, the offset in the model is ignored. It works perfectly when I give the body a mass.

I'm using this code to create the bodies:
Code: Select all
NewtonBody* CPhysicsInterface::addBody(NewtonCollision* shape,float mass,CBody* entity){
   NewtonBody* body;
   XMFLOAT3 pos=entity->getPosition();
   XMFLOAT4 rot=entity->getQuaternion();
   dMatrix world;
   world=dMatrix(dQuaternion(rot.x,rot.y,rot.z,rot.w),dVector(pos.x,pos.y,pos.z,1.0f));

   body=NewtonCreateBody(pWorld,shape,&world[0][0]);

   NewtonBodySetDestructorCallback(body,CPhysicsInterface::DestroyBodyCallback);
   NewtonBodySetUserData(body,entity);

   dVector origin;
   dVector inertia;

   NewtonConvexCollisionCalculateInertialMatrix(shape,&inertia[0],&origin[0]);
   NewtonBodySetMassMatrix (body,mass,mass*inertia.m_x, mass * inertia.m_y, mass * inertia.m_z);
   NewtonBodySetCentreOfMass(body,&origin[0]);

   NewtonBodySetForceAndTorqueCallback (body, ApplyForceAndTorqueCallback);
   NewtonBodySetTransformCallback (body, SetTransformCallback);

   return body;
}

NewtonBody* CPhysicsInterface::addBodyBox(float dx,float dy,float dz,float mass,CBody* entity,NewtonCollision* &shapeOut){
   NewtonCollision* shape;

   XMFLOAT3 o=entity->getOrigin();
   dMatrix offset(GetIdentityMatrix());
   offset.m_posit=dVector(o.x,o.y,o.z);
   shape=NewtonCreateBox(pWorld,dx,dy,dz,0,&offset[0][0]);
   shapeOut=shape;
   NewtonBody* body=addBody(shape,mass,entity);
   NewtonReleaseCollision(pWorld,shape);
   return body;
}

NewtonBody* CPhysicsInterface::addBodyStaticMesh(CBody* entity,vector<float> &vertices,NewtonCollision* &shapeOut){
   assert(vertices.size()%9==0);

   NewtonCollision* shape=NewtonCreateTreeCollision(pWorld,0);
   NewtonTreeCollisionBeginBuild(shape);
   int vertexCount=vertices.size()/3;
   int faceCount=vertexCount/3;
   for(int i=0; i<faceCount; i++){
      dVector vertex[3];

      for(int v=0; v<3; v++){
         vertex[v].m_x=vertices[i*9+3*v];
         vertex[v].m_y=vertices[i*9+3*v+1];
         vertex[v].m_z=vertices[i*9+3*v+2];
      }
      NewtonTreeCollisionAddFace(shape,3,&vertex[0].m_x,sizeof(dVector),i+1);
   }
   NewtonTreeCollisionEndBuild(shape,1);
   NewtonBody* body=addBody(shape,0,entity);
   NewtonReleaseCollision(pWorld,shape);
   return body;
}


To test this, I created a box in my modeler that was offset by quite a bit. I gave it a mass of 5.
I also created a box to act as the ground, which I placed in my modeler at about (0,-5,0).
When I load the ground model and set it's mass to 0, it acts like the ground is centered at 0 rather than -5. The same thing happens whether I call addBodyBox or addBodyStaticMesh. If I edit the model to be centered at 0,0,0 then everything works as normal. What's really confusing me is why the collisiontree is doing this. Is my addBody method doing something to recenter the model?


Edit:
It looks like the model is getting inverted in at least the y direction. I tried negating the y coordinates that were getting passed into the collisiontree, but that created even worse behavior.
andylhansen
 
Posts: 21
Joined: Sat Jun 04, 2011 6:32 am

Re: Static boxes and treecollision ignore model offset.

Postby andylhansen » Sun Jun 12, 2011 1:29 am

Well after tinkering around some more, it looks like I have to negate both the x and y coordinates that I pass into the model. This must be due to a difference in the coordinate system Newton uses.
andylhansen
 
Posts: 21
Joined: Sat Jun 04, 2011 6:32 am

Re: Static boxes and treecollision ignore model offset.

Postby Julio Jerez » Sun Jun 12, 2011 8:04 am

I do not remeber collsion tree having matrix offset, I am confused as to waht you aaare tryin to do.

you do not have to invert anything, the offset should for all othe shapes always works and it is independent of the mass value.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Static boxes and treecollision ignore model offset.

Postby andylhansen » Sun Jun 12, 2011 1:02 pm

In order to get the collision tree to match what was being rendered on screen, I had to negate the x and y coordinates of the vertices I passed in. The dynamic bodies are being passed the world matrix that Newton gives in the callback. The static ones are using the identity matrix I give them with XMMatrixIdentity(). I think it has to do with a difference in the world matrices.
andylhansen
 
Posts: 21
Joined: Sat Jun 04, 2011 6:32 am

Re: Static boxes and treecollision ignore model offset.

Postby Julio Jerez » Sun Jun 12, 2011 1:48 pm

in general the negation, or flippin of any value lead to some other problems, like I said you should not have to do that in anyway.
implement the debug function so that you can see how the collision shape looks like, they should match the visual exactly.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Static boxes and treecollision ignore model offset.

Postby andylhansen » Sun Jun 12, 2011 6:56 pm

What do you mean by the debug function? Is that the visual debugger that is mentioned in the tutorial?

Edit: I found the problem. The quaternion that I was passing to newton was not correct. I changed it to use Euler angles, and it seems to work now.
andylhansen
 
Posts: 21
Joined: Sat Jun 04, 2011 6:32 am

Re: Static boxes and treecollision ignore model offset.

Postby JernejL » Mon Jun 13, 2011 4:46 am

andylhansen wrote:What do you mean by the debug function? Is that the visual debugger that is mentioned in the tutorial?

Edit: I found the problem. The quaternion that I was passing to newton was not correct. I changed it to use Euler angles, and it seems to work now.


You shouldn't be using that if you don't want any problems with gimbal lock. Just correct the quaterionion->matrix4x4 transformation function
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest