newtonupdate not calling the settransformcallback function

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

newtonupdate not calling the settransformcallback function

Postby r4ccoon » Wed Nov 18, 2009 4:28 am

hi.

i am trying to integrate my engine which is based on directX DXUT with this physics engine.
and i am stucked at the newtonupdate being not able to call the callback function settransformcallback .

i am following the samples. i am now at the 102 adding rigid body sample.

i have my code uploaded here.
http://www.mediafire.com/?uuwohwmy0jj

if you guys dont wanna bother the download, here is the code.

Code: Select all
rigidbodyutil.cpp
// Transform callback to set the matrix of a the visual entity
static void SetTransformCallback (const NewtonBody* body, const dFloat* matrix, int threadIndex)
{
   CMeshInstance* ent;

   // Get the position from the matrix
   dVector posit (matrix[12], matrix[13], matrix[14], 1.0f);
   dQuaternion rotation;

   // we will ignore the Rotation part of matrix and use the quaternion rotation stored in the body
   NewtonBodyGetRotation(body, &rotation.m_q0);

   // get the entity associated with this rigid body
   ent = (CMeshInstance*) NewtonBodyGetUserData(body);

   // since this tutorial run the physics and a different fps than the Graphics
   // we need to save the entity current transformation state before updating the new state.
   ent->setPrevPosition(ent->getPosition());
   ent->setPrevRotationFromQuaternion(ent->getDQRotation());

   //create temp quaternion with d3dxquaternion format
   D3DXQUATERNION tempQ(rotation.m_q1,rotation.m_q2,rotation.m_q3,rotation.m_q0);

   if (D3DXQuaternionDot(&ent->getQRotation(),&tempQ) < 0.0f) {
      dQuaternion tempQ = ent->getDQRotation();
      tempQ.Scale(-1.0f);
      ent->setPrevRotationFromQuaternion(tempQ);
   }

   // set the new position and orientation for this entity
   ent->setPosition(D3DXVECTOR3(posit.m_x,posit.m_y,posit.m_z));
   ent->setRotationFromQuaternion(rotation);
}


and this is the createrigidbody that actually set the settransformcallback
Code: Select all
rigidbodyutil.cpp
NewtonBody* CreateRigidBody (NewtonWorld* world, CTerrain* ent, NewtonCollision* collision, dFloat mass)
{
   dVector minBox;
   dVector maxBox;
   dVector origin;
   dVector inertia;
   NewtonBody* body;
 
   body = NewtonCreateBody (world, collision);
   NewtonBodySetDestructorCallback (body, DestroyBodyCallback);
   NewtonBodySetUserData (body, ent);
   dMatrix matrix (ent->getQRotation(), ent->GetPosition());
   NewtonBodySetMatrix (body, &matrix[0][0]);
   NewtonConvexCollisionCalculateInertialMatrix (collision, &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;
}


and this is the code that is representing createscene in the sample

Code: Select all
void CPlayState::createScene(){
   NewtonBody* floorBody;
   NewtonBody* carBody;
   NewtonBody* boxBody;

   NewtonCollision* shape;
   
   // crate physics//////////////////////////////////
   // floor physics//////////////////////////////////
   // add static floor Physics
   shape = CreateNewtonBox (g_world, &m_terrain, 0);
   floorBody = CreateRigidBody (g_world, &m_terrain, shape, 0.0f);
   NewtonReleaseCollision (g_world, shape);

   // set the Transformation Matrix for this rigid body
   dMatrix matrix (m_terrain.getQRotation(), m_terrain.GetPosition() );
   NewtonBodySetMatrix (floorBody, &matrix[0][0]);

   // now we will use the properties of this body to set a proper world size.
   dVector minBox;
   dVector maxBox;
   NewtonCollisionCalculateAABB (shape, &matrix[0][0], &minBox[0], &maxBox[0]); 

   // add a body with a box shape
   shape = CreateNewtonBox (g_world, CMeshCall::getInstance()->getMeshByIndex(CRATE_ID_START), 0);
   boxBody = CreateRigidBody (g_world, CMeshCall::getInstance()->getMeshByIndex(CRATE_ID_START), shape, 10.0f);
   NewtonReleaseCollision (g_world, shape);
}


and this is when i call the newtonupdate.
the breakpoint is not stopping when i put breakpoint sign inside the SetTransformCallback() in rigidbodyutil.cpp

Code: Select all
while ((loops < MAX_PHYSICS_LOOPS) && (g_timeAccumulator >= DEMO_FPS_IN_MICROSECUNDS))
   {
      loops ++;
 
      // sample time before the Update
      g_physicTime = game->GetTimeInMicrosenconds();
 
      NewtonUpdate (g_world, (1.0f / DEMO_PHYSICS_FPS));

      // calculate the time spent in the physical Simulation
      g_physicTime = game->GetTimeInMicrosenconds() - g_physicTime;

      // subtract time from time accumulator
      g_timeAccumulator -= DEMO_FPS_IN_MICROSECUNDS;
      physicTime ++;
 
      physicLoopsTimeAcc += g_physicTime;
   }
r4ccoon
 
Posts: 12
Joined: Wed Nov 18, 2009 3:52 am

Re: newtonupdate not calling the settransformcallback function

Postby thedmd » Wed Nov 18, 2009 6:16 am

Did you set appropiate world size? Default is (-100, -100, -100) to (100, 100, 100). It your body object is outside this bounding box it is not simulated and callbacks are not called. Check out NewtonSetWorldSize().
thedmd
 

Re: newtonupdate not calling the settransformcallback function

Postby r4ccoon » Wed Nov 18, 2009 7:15 am

yeah. i have it.

Code: Select all
 // set a fix world size
   dVector minSize (-700.0f, -700.0f, -700.0f);
   dVector maxSize ( 700.0f,  700.0f,  700.0f);
   NewtonSetWorldSize (g_world, &minSize[0], &maxSize[0]);
   // configure the Newton world to use iterative solve mode 0
   // this is the most efficient but the less accurate mode
   NewtonSetSolverModel (g_world, 1);


i put above code before calling the create scene.

and it still doesnt work
r4ccoon
 
Posts: 12
Joined: Wed Nov 18, 2009 3:52 am

Re: newtonupdate not calling the settransformcallback function

Postby Van » Wed Nov 18, 2009 8:09 am

Make sure you set the mass of the body via NewtonBodySetMassMatrix(). If your body has no mass then it won't be transformed. Sometimes I find that the mass is zero.
Stop Global Whining.
Van
 
Posts: 17
Joined: Mon Nov 08, 2004 12:36 am

Re: newtonupdate not calling the settransformcallback function

Postby JernejL » Wed Nov 18, 2009 8:39 am

Do you have forcetorque callbacks registered and apply the forces there - to make objects actually move?
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1587
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: newtonupdate not calling the settransformcallback function

Postby r4ccoon » Wed Nov 18, 2009 8:40 am

yeah. i have that too. and still doesnt work

i set it 10 for the crate. and 0 for the floor. because the wiki said that 0 is static object.

this is the code for adding static rigid body for floor
Code: Select all
// floor physics//////////////////////////////////
   // add static floor Physics
   shape = CreateNewtonBox (g_world, &m_terrain, 0);
   floorBody = CreateRigidBody (g_world, &m_terrain, shape, 0.0f);
   NewtonReleaseCollision (g_world, shape);


this is for adding rigid body of the crates
Code: Select all
// add a body with a box shape
   shape = CreateNewtonBox (g_world, CMeshCall::getInstance()->getMeshByIndex(CRATE_ID_START), 1);
   boxBody = CreateRigidBody (g_world, CMeshCall::getInstance()->getMeshByIndex(CRATE_ID_START), shape, 10.0f);
   NewtonReleaseCollision (g_world, shape);


and this the header for createRigidBody
Code: Select all
CreateRigidBody (NewtonWorld* world, CMeshInstance* ent, NewtonCollision* collision, dFloat mass)


i am curious about shape ID.
i made them all the same which is all zero.
then i tried to varied it with 0 for the floor, and 1 for the crate.
but still doesnt work.

FYI, in the create rigid body i have.
Code: Select all
NewtonBodySetMassMatrix (body, mass, mass * inertia.m_x, mass * inertia.m_y, mass * inertia.m_z);

the mass there is 10.
but my inertia always 0 like on the sample. what does that mean?
r4ccoon
 
Posts: 12
Joined: Wed Nov 18, 2009 3:52 am

Re: newtonupdate not calling the settransformcallback function

Postby r4ccoon » Wed Nov 18, 2009 9:01 am

i just watched man from nasa telling something about inertia at wikipedia.
so newtondynamic engine will give the crate the inertia of the gravitation right?
r4ccoon
 
Posts: 12
Joined: Wed Nov 18, 2009 3:52 am

Re: newtonupdate not calling the settransformcallback function

Postby Julio Jerez » Wed Nov 18, 2009 9:28 am

r4ccoon wrote:FYI, in the create rigid body i have.
Code: Select all
NewtonBodySetMassMatrix (body, mass, mass * inertia.m_x, mass * inertia.m_y, mass * inertia.m_z);

the mass there is 10.
but my inertia always 0 like on the sample. what does that mean?


thats the problem, the inertia of a dynmaic body can not be zero.
use the function calculate inercia and center of mass for teh shape.
use those valuse ot set the inertia like you are doing in the above function

and not Netwon will no give teh body teh inertia of the graviation,
the moment of inertia and the mass are two uncorrelated properties of each body, tw bodie of equal mass and equal size can have diffrnet inertia, for example a booling ball and a beach ball.
the total inertia of a body have two components a linear and angular.
when force and torque are applied to a body, the integrator calculate teh total inerca change tand distribute to teh body accpsrding to the mass and momenet of inerta coeficients distribution,
the more moment of inertia the body have the less part of the total inertia is convertied to rotational inertia, bu the more teh body tend to retain it angula inertia.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: newtonupdate not calling the settransformcallback function

Postby r4ccoon » Wed Nov 18, 2009 5:32 pm

ok. so i hardcoded all the inertia parameter to 1.0f.
still doesnt work.
r4ccoon
 
Posts: 12
Joined: Wed Nov 18, 2009 3:52 am

Re: newtonupdate not calling the settransformcallback function

Postby Julio Jerez » Wed Nov 18, 2009 5:51 pm

are you applying any force to teh body?

Transform call back is only called when a body matrix changes value.
if in your force callback no force is applied and the velocity is zero, the teh body does no move and teh Traform matrix is no changed.
verify that and see if it work.
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: newtonupdate not calling the settransformcallback function

Postby r4ccoon » Wed Nov 18, 2009 11:00 pm

yeah i did that.
and it still not working.

i ll try other examples. let see if that will work
r4ccoon
 
Posts: 12
Joined: Wed Nov 18, 2009 3:52 am

Re: newtonupdate not calling the settransformcallback function

Postby Julio Jerez » Thu Nov 19, 2009 12:29 am

what version of teh engine are you using.
I thing you are using an older version

this funtion take and extra parameter, the shape ID
return NewtonCreateBox (world, size.m_x, size.m_y, size.m_z, shapeId, &offset[0][0]);

all of the collision creation functions are missing the shape ID parameter too
you need to ge version 2.10
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: newtonupdate not calling the settransformcallback function

Postby r4ccoon » Thu Nov 19, 2009 1:32 am

i have 2.10.
i have function CreateNewtonBox which is a wrapper for NewtonCreateBox.

i need to find out what do i need to make the rigidbody fall from the sky to my terrain.
1. the meshes. i have terrain, and mesh
2. the mass. i have 0 for the terrain. and 10 for mesh.
3. i have 10.0f as the gravity applied with the NewtonBodySetForceAndTorqueCallback (body, ApplyForceAndTorqueCallback);

what else do i need?
r4ccoon
 
Posts: 12
Joined: Wed Nov 18, 2009 3:52 am

Re: newtonupdate not calling the settransformcallback function

Postby Julio Jerez » Thu Nov 19, 2009 8:04 am

Sorry My mistake and had teh path set wrong

I think I found it, you have a bug in this function

Code: Select all
void CTerrain::GetBBox ()
{
   // initialize the Box bound to ridicules values
   m_minBox = dVector ( 1.0e10f,  1.0e10f,  1.0e10f, 1.0f);
   m_maxBox = dVector (-1.0e10f, -1.0e10f, -1.0e10f, 1.0f);
 
   int numVerts = m_numVertices;

   //count bounding box for physics
   Vertex *tVertices = NULL;
   //////////////////////////////////////////////////////////////////
   //
   ///////////////////////////////////////////////////////////
   m_vb.getVB()->Lock(0,sizeof( PositionTextured ), (void**) & tVertices,0 ); {
      for (int j = 0; j < numVerts; j++) {
         dFloat val;

         // adjust to a better bound for x
         val = tVertices[j].m_x;
         m_minBox.m_x = (val < m_minBox.m_x) ? val : m_minBox.m_x;
         m_maxBox.m_x = (val > m_maxBox.m_x) ? val : m_maxBox.m_x;

         // adjust to a better bound for y
         val = tVertices[j].m_y;
         m_minBox.m_y = (val < m_minBox.m_y) ? val : m_minBox.m_y;
         m_maxBox.m_y = (val > m_maxBox.m_y) ? val : m_maxBox.m_y;

         // adjust to a better bound for z
         val = tVertices[j].m_z;
     //  Julio this never set m_minBox.z value an dteh box size is 1e10
         //m_maxBox.m_z = (val < m_maxBox.m_z) ? val : m_maxBox.m_z;
      // it should be like this 
         m_minBox.m_z = (val < m_minBox.m_z) ? val : m_minBox.m_z;

         m_maxBox.m_z = (val > m_maxBox.m_z) ? val : m_maxBox.m_z;
      }
   }
   // unlock the buffer as we're done working with the vertices
   m_vb.getVB()->Unlock();
}


I edited the code and put the fix and it started to work
Julio Jerez
Moderator
Moderator
 
Posts: 12426
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: newtonupdate not calling the settransformcallback function

Postby r4ccoon » Thu Nov 19, 2009 3:24 pm

thank you very much.
i am working on other part of my program.

i will try it this afternoon. i will get back here after applying and testing it.
r4ccoon
 
Posts: 12
Joined: Wed Nov 18, 2009 3:52 am

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 0 guests

cron