Body move and SetTraformCallback

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

Body move and SetTraformCallback

Postby Kapitan » Sat Nov 01, 2008 3:25 pm

Hi, again with a newbie question.

After the creation of body and collision, i've to move
all things. I've set a force 9.81 * mass in the
Force and torque callback, but my object is stopped
and doesn't move at all.

Note that my object is flying. So the gravity would
make it move down...or not?

I say that it doesn't move because of the trasform
matrix callback isn't ever called.

Is there anything else to do?


Thanks again.

Note: i call the NewtonUpdate function and the body
is unfreezed. The NewtonBodySetMatrix is not been used
(i think is identity).
Kapitan
 
Posts: 37
Joined: Thu Apr 17, 2008 6:07 pm

Re: Body move and SetTraformCallback

Postby Gianluca » Sun Nov 02, 2008 5:42 am

If the body is unfreezed, and if you correctly set the callback ForceAndTorque and if you call correctly the NewtonUpdate... so, there is no evident reasons on the fact that your body doesn't fall down.

Please, post some portion of the code and/or give us more details to let us the possibility to figure out where is the problem.

Ciao,
Gianluca
Gianluca
 
Posts: 53
Joined: Fri Nov 11, 2005 4:37 am
Location: Rome - Italy

Re: Body move and SetTraformCallback

Postby Kapitan » Sun Nov 02, 2008 7:47 am

This is true at all.

So i've created a collision box + body and tested.
The box correctly falls down.My dice don't.
Code: Select all
// dice (id = 15)
   tmp = CreateNewtonCustomObjFromIrrSceneId(scenehandle,15);
   myDice = tmp;
   NewtonBodySetFreezeTreshold(tmp->body, 0.0000001f, 0.0000001f, 50);
   NewtonBodySetMassMatrix (tmp->body, 100.0f, 1.0f, 1.0f, 1.0f);
   NewtonBodySetAutoFreeze(tmp->body,0);
   NewtonWorldUnfreezeBody(nWorld, tmp->body);
   NewtonBodySetMaterialGroupID(tmp->body,i);
   NewtonBodySetTransformCallback(tmp->body, SetMeshTransformEvent);
   NewtonBodySetForceAndTorqueCallback(tmp->body, ApplyForceAndTorqueEvent);
   
   // temp box
   NewtonCollision *tmpcol = NewtonCreateBox(nWorld, 40.0f, 40.0f, 40.0f, NULL);
   NewtonBody *tmpbody = NewtonCreateBody(nWorld,tmpcol);
   tmp = new NewtonCustomObj();
   tmp->body = tmpbody;
   tmp->collision = tmpcol;
   othersIdArray.insert(50);
   othersMap.insert(50,tmp);
   NewtonBodySetFreezeTreshold(tmp->body, 0.0000001f, 0.0000001f, 50);
   NewtonBodySetMassMatrix (tmp->body, 100.0f, 1.0f, 1.0f, 1.0f);
   NewtonBodySetAutoFreeze(tmp->body,0);
   NewtonWorldUnfreezeBody(nWorld, tmp->body);
   NewtonBodySetMaterialGroupID(tmp->body,i);
   NewtonBodySetTransformCallback(tmp->body, SetMeshTransformEvent);
   NewtonBodySetForceAndTorqueCallback(tmp->body, ApplyForceAndTorqueEvent);


The only difference is collision creation,
the dice is a NewtonCreateConvexHull.
The creation is done in this function from Irrlicht.

Code: Select all
NewtonCustomObj *MyDiceApp::CreateNewtonCustomObjFromIrrSceneId(ISceneManager * sceneHandle,irr::c8 id)
{
   NewtonCustomObj *ret = new NewtonCustomObj();
   ISceneNode *sn = sceneHandle->getSceneNodeFromId(id);

   if ((sn == 0 ) || (sn->getType() != scene::ESNT_MESH))
      return(0);
   
   IMeshSceneNode *msn = static_cast<IMeshSceneNode*>(sn);
   IMesh *m = msn->getMesh();

   if (m == 0)
      return(0);

   irr::u32 nMeshBuffer = 0; //Mesh Buffer count
   IMeshBuffer *mesh_buffer = 0;
   dFloat *vertices;

   u32 nVertices = 0;
   
   for( nMeshBuffer=0 ; nMeshBuffer < m->getMeshBufferCount() ; nMeshBuffer++ )
   {
      nVertices += m->getMeshBuffer(nMeshBuffer)->getVertexCount();
   }

   vertices = (dFloat*)iaf->allocate(nVertices * 3);
   u32 tmpCounter = 0;
   
   matrix4 mat = sn->getRelativeTransformation();
   vector3df scaleVect = sn->getScale();

   for( nMeshBuffer=0 ; nMeshBuffer < m->getMeshBufferCount() ; nMeshBuffer++ )
   {

      mesh_buffer = m->getMeshBuffer(nMeshBuffer);

      if (mesh_buffer->getVertexType() != EVT_STANDARD)
         return 0;

      S3DVertex *s3vertices = (S3DVertex*)mesh_buffer->getVertices();
      
      vector3df irrOrig,irrTrasf;
      
      for(irr::u32 i=0; i<mesh_buffer->getVertexCount(); i++)
      {
         vertices[tmpCounter++] = s3vertices[i].Pos.X * scaleVect.X;
         vertices[tmpCounter++] = s3vertices[i].Pos.Y * scaleVect.Y;
         vertices[tmpCounter++] = s3vertices[i].Pos.Z * scaleVect.Z;
      }

   }

   //Create Newton collision object
   NewtonCollision *tmp = NewtonCreateConvexHull(nWorld,nVertices,vertices,sizeof(float)*3,NULL);

   //delete vertices
   iaf->deallocate(vertices);
      
   NewtonBody *body = NewtonCreateBody(nWorld, tmp);

   dFloat *matArray = mat.pointer();
   NewtonBodySetMatrix(body,matArray);
   
   ret->body = body;
   ret->collision = tmp;

   return(ret);
}
Last edited by Kapitan on Sun Nov 02, 2008 7:52 am, edited 1 time in total.
Kapitan
 
Posts: 37
Joined: Thu Apr 17, 2008 6:07 pm

Re: Body move and SetTraformCallback

Postby Kapitan » Sun Nov 02, 2008 7:50 am

A pretty image.

Newton.image.JPG
Newton.image.JPG (55.92 KiB) Viewed 7919 times
Kapitan
 
Posts: 37
Joined: Thu Apr 17, 2008 6:07 pm

Re: Body move and SetTraformCallback

Postby Kapitan » Mon Nov 03, 2008 3:23 pm

I'm switching to sdk 2.0, some changes occurred, the time
to set all things and i'll be back to this problem (i hope has
been solved).
Kapitan
 
Posts: 37
Joined: Thu Apr 17, 2008 6:07 pm

Re: Body move and SetTraformCallback

Postby Kapitan » Thu Nov 06, 2008 4:26 pm

Ok, now the forceandtorque callback is called correctly
on my dice, but still no move.

SetAutoSleep is 0, SetFreezeState is 0, but FreezeThreshold
has been removed so how can i control it ?

EDIT: NewtonGetSleepState called on my frozed object returns
1, so the object is sleeping, but how to wake up?
Kapitan
 
Posts: 37
Joined: Thu Apr 17, 2008 6:07 pm

Re: Body move and SetTraformCallback

Postby JernejL » Thu Nov 06, 2008 6:53 pm

they wake up automaticly now by default when you apply force, now you get the forcetorque callback called each frame rather than only when the body is active.
Help improving the Newton Game Dynamics WIKI
User avatar
JernejL
 
Posts: 1578
Joined: Mon Dec 06, 2004 2:00 pm
Location: Slovenia

Re: Body move and SetTraformCallback

Postby Gianluca » Fri Nov 07, 2008 5:53 am

So, let me summarize:
- you moved to Newton 2.0
- now, the ForceAndTorqueCallback is called at each NewtonUpdate on your Dice body
- and the Dice body is still frozen and doesn't respond to the applied Force
Is it right ???
If yes, try to understand:
- how you add force/torque ?? (if you simply call NewtonAddTorque is correct, if you do somethingelse please post it here)
- Are you sure that the un-movable Dice body is within the world dimensions ??? (did you call NewtonWorldSetSize ???)
Gianluca
 
Posts: 53
Joined: Fri Nov 11, 2005 4:37 am
Location: Rome - Italy

Re: Body move and SetTraformCallback

Postby Kapitan » Fri Nov 07, 2008 1:43 pm

I don't Believe it!!!

Was the NewtonSetWorldSize!!!

It seems to me that something was missing.

Thanks guys.
Kapitan
 
Posts: 37
Joined: Thu Apr 17, 2008 6:07 pm

Re: Body move and SetTraformCallback

Postby Kapitan » Fri Nov 07, 2008 4:54 pm

But i don't think the trasform matrix is ready to be applied
to irrlicht directly.

I'm in the TrasformCallback and while all things
moving, the scale diagonal continues to do up and down.

Others test coming...

For example.

If i apply a torque in the applyforceandtorque callback
the collision body gets bigger than when there was
not torque, as i modify collision vertex, but clearly
is not.
Kapitan
 
Posts: 37
Joined: Thu Apr 17, 2008 6:07 pm

Re: Body move and SetTraformCallback

Postby Kapitan » Sat Nov 08, 2008 12:06 pm

I think is a bug, or have not understand the torque.

If i set a force with NewtonBodyAddForce my
collision is as usual and work perfectly.

If i set a torque the scale diagonal of the trasformation
matrix has imperfections,
the 3 direction moves random and my collision is modified
and has wrong behaviour.

If you want i'll send you the .exe to see, my project is
made for university and so it can be distributed :D .
Kapitan
 
Posts: 37
Joined: Thu Apr 17, 2008 6:07 pm

Re: Body move and SetTraformCallback

Postby Julio Jerez » Sat Nov 08, 2008 12:11 pm

for a gravity object teh torque is zero.
What are you doing?
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: Body move and SetTraformCallback

Postby Kapitan » Sat Nov 08, 2008 3:37 pm

Yes but the problem is that when it collides
the torque is created by collision...and it's the same....

And if i would set a custom impulse on a gravity object? Can i?

Tomorrow i'll post other photos so you can see..
Kapitan
 
Posts: 37
Joined: Thu Apr 17, 2008 6:07 pm

Re: Body move and SetTraformCallback

Postby Kapitan » Sun Nov 09, 2008 7:57 am

Here is the code:

1) Creation of Collision
Code: Select all
// add points - part of CreateNewtonCustomObjFromIrrSceneId
for(irr::u32 i=0; i<mesh_buffer->getVertexCount(); i++)
      {
         
         vertices[tmpCounter++] = s3vertices[i].Pos.X;
         vertices[tmpCounter++] = s3vertices[i].Pos.Y;
         vertices[tmpCounter++] = s3vertices[i].Pos.Z;
      }
      
//Create Newton collision object
NewtonCollision *tmp = NewtonCreateConvexHull(nWorld,nVertices,vertices,sizeof(float)*3,0.50f,NULL);
   
//delete vertices
iaf->deallocate(vertices);

if (tmp != NULL)
{
   NewtonBody *body = NewtonCreateBody(nWorld, tmp);
      
   if (body != NULL)
   {
      NewtonBodySetUserData(body,sn);
      ret = new NewtonCustomObj();

      dFloat *matArray = mat.pointer();

      NewtonBodySetMatrix(body,matArray);
         
    ret->body = body;
      ret->collision = tmp;
   }
}


2) The call and the set of Newton Mass and Callback
Code: Select all
// the call
tmp = CreateNewtonCustomObjFromIrrSceneId(scenehandle,15);
if (tmp != NULL)
{
   myDice = tmp;
   NewtonBodySetAutoSleep(tmp->body,1);
   NewtonBodySetFreezeState(tmp->body,0);
         
   dFloat *inertia = (dFloat*)iaf->allocate(3);
   dFloat *origin = (dFloat*)iaf->allocate(3);
   NewtonConvexCollisionCalculateInertialMatrix(tmp->collision,inertia,origin);
   NewtonBodySetMassMatrix(tmp->body,kDiceMass,kDiceMass*inertia[0],kDiceMass*inertia[1],kDiceMass*inertia[2]);
   iaf->deallocate(inertia);
   iaf->deallocate(origin);

   NewtonBodySetTransformCallback(tmp->body, SetMeshTransformEvent);
   NewtonBodySetForceAndTorqueCallback(tmp->body, ApplyForceAndTorqueEvent);
}


3) The Omega Applied
Code: Select all
// the input
if(event.KeyInput.Key == KEY_RETURN)
{
   dFloat *velImpulse = (dFloat*)iaf->allocate(3);
   //dFloat *centre = (dFloat*)iaf->allocate(3);
   velImpulse[0] = 0.0f; velImpulse[1] = 5.0f; velImpulse[2] = 10.0f;
   //centre[0] = 0.0f; centre[1] = 0.0f; centre[2] = 0.0f;
            
   NewtonBodySetOmega(myDice->body,velImpulse); 
// NewtonBodySetVelocity doesn't made problems!!! Problems are only with torques?
   iaf->deallocate(velImpulse);
   //iaf->deallocate(centre);
}
Kapitan
 
Posts: 37
Joined: Thu Apr 17, 2008 6:07 pm

Re: Body move and SetTraformCallback

Postby Kapitan » Sun Nov 09, 2008 8:01 am

A snapshot.

When i press enter (see previous post,
i simply apply an omega to the dice),
the collision mesh changes immediately to this.

In debug i've tried with a NewtonBodySetOmega,
with a NewtonBodySetTorque and when a collision
make the dice rotate, and in any case the matrix
passed in the Trasform Callback has the scale
diagonal set to one, as if it was in 1.53.

The internal mesh (from irrlich) doesn't change
because i've not applied the scale in the Trasform
callback.

Newton.error.after.JPG
Newton.error.after.JPG (56.78 KiB) Viewed 7760 times
Kapitan
 
Posts: 37
Joined: Thu Apr 17, 2008 6:07 pm

Next

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 14 guests