[Solved]Callback is not reached

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

[Solved]Callback is not reached

Postby Cookiezzz » Fri Jul 15, 2011 11:38 am

Hi,

I'm developing a test Game using Newton Game Dynamics and Ogre3d. I've set all Callbacks correctly and there should be 2 objects, one should falling down to the other. But when I starts the Game there happens.... Nothing! The Objects appear, but they float in the air.
Thats the simplified code:
Create Scene:
Code: Select all
void Game::createScene(void)
{   
NewtonCollision* shape;
   World = NewtonCreate ();
   NewtonSetPlatformArchitecture (World, 0);
   Ogre::Vector3 minSize (-500000.0f, -500000.0f, -500000.0f);
   Ogre::Vector3 maxSize ( 500000.0f,  500000.0f,  500000.0f);
   NewtonSetWorldSize (World, &minSize[0], &maxSize[0]);
   NewtonSetSolverModel (World, 0);
   Ogre::Vector3 size(2500,2500,2500);
   Ogre::Entity* Map = mSceneMgr->createEntity("Map", MapName);
   Ogre::SceneNode* MapNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("MapNode",Ogre::Vector3(0,-842,0));
   MapNode->attachObject(Map);
   MapNode->scale(2500, 2500, 2500 );
   Ogre::Entity* PlayerAct = mSceneMgr->createEntity("Player", PlayerName);
   Ogre::SceneNode* PlayerNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("PlayerNode",Ogre::Vector3(500,500,500));
   PlayerNode->attachObject(PlayerAct);
   size = Ogre::Vector3(1,1,1);
   mSceneMgr->setAmbientLight(Ogre::ColourValue(1, 1, 1));
   Ogre::Light* l = mSceneMgr->createLight("MainLight");
   l->setPosition(20,80,50);


   shape = CreateNewtonBox (World, MapNode, 0);
   floorBody = CreateRigidBody (World, MapNode, shape, 0.0f);
   NewtonReleaseCollision (World, shape);


   Ogre::Matrix4 matrix (MapNode->getOrientation());
   matrix.setTrans (MapNode->getPosition());
   NewtonBodySetMatrix (floorBody, &matrix[0][0]);




   shape = CreateNewtonBox (World, PlayerNode, 1);
   playerBody = CreateRigidBody (World, PlayerNode, shape, 0.0f);
   NewtonReleaseCollision (World, shape);


   Ogre::Matrix4 matrix2 (MapNode->getOrientation());
   matrix2.setTrans (MapNode->getPosition());
   NewtonBodySetMatrix (playerBody, &matrix2[0][0]);

}



Update Physics,called each Frame:
Code: Select all
void Game::Physic(Ogre::FrameEvent evt)
{
   NewtonUpdate (World,evt.timeSinceLastFrame);
}


Code: Select all
NewtonCollision* CreateNewtonBox (NewtonWorld* world, Ogre::SceneNode* nod, int shapeId)
{
   Ogre::Vector3 minBox;
   Ogre::Vector3 maxBox;
   Ogre::AxisAlignedBox box;
   box=nod->_getWorldAABB();
   minBox = box.getMinimum();
   maxBox = box.getMaximum();

   Ogre::Vector3 size (maxBox - minBox);
   Ogre::Vector3 origin ((maxBox + minBox)* 0.5f);

   Ogre::Matrix4 offset= Ogre::Matrix4::IDENTITY;
   offset.setTrans(origin);

   return NewtonCreateBox (world, size.x, size.y, size.z, shapeId, &offset[0][0]);
}

Code: Select all
NewtonBody* CreateRigidBody (NewtonWorld* world, Ogre::SceneNode* ent, NewtonCollision* collision, dFloat mass)
{
   Ogre::Vector3 minBox;
   Ogre::Vector3 maxBox;
   Ogre::Vector3 origin;
   Ogre::Vector3 inertia;
   NewtonBody* body;
   Ogre::Matrix4 mat;
   mat=Ogre::Matrix4::IDENTITY;

   body = NewtonCreateBody (world, collision,&mat[0][0]);

   NewtonBodySetDestructorCallback (body, DestroyBodyCallback);

   NewtonBodySetUserData (body, ent);

   Ogre::Matrix4 matrix (ent->getOrientation());
   matrix.setTrans(ent->getPosition());
   NewtonBodySetMatrix (body, &matrix[0][0]);

   NewtonConvexCollisionCalculateInertialMatrix (collision, &inertia[0], &origin[0]);   


   NewtonBodySetMassMatrix (body, mass, mass * inertia.x, mass * inertia.y, mass * inertia.z);


   NewtonBodySetCentreOfMass (body, &origin[0]);

   NewtonBodySetForceAndTorqueCallback (body, ApplyForceAndTorqueCallback);

   NewtonBodySetTransformCallback (body, SetTransformCallback);

   return body;
}

My Callbacks:
Code: Select all
void SetTransformCallback (const NewtonBody* body, const dFloat* matrix, int threadIndex)
{
   Ogre::SceneNode* ent;
   Ogre::Vector3 oldposition;
   Ogre::Quaternion oldrotation;

   // Get the position from the matrix
   Ogre::Vector3 posit (matrix[12], matrix[13], matrix[14]);
   Ogre::Quaternion rotation;
   Ogre::Radian rad;

   // we will ignore the Rotation part of matrix and use the quaternion rotation stored in the body
   rad = rotation.getPitch();
   float f = rad.valueRadians();
   NewtonBodyGetRotation(body,&f);

   // get the entity associated with this rigid body
   ent = (Ogre::SceneNode*) 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.
   oldposition = ent->getPosition();
   oldrotation = ent->getOrientation();
   if (ent->getOrientation().Dot(rotation) < 0.0f) {
      oldrotation*(-1.0f);
   }

   // set the new position and orientation for this entity
   ent->setPosition(posit);
   ent->setOrientation(rotation);
}


Code: Select all
void DestroyBodyCallback (const NewtonBody* body)
{

}

Code: Select all
void ApplyForceAndTorqueCallback (const NewtonBody* body, dFloat timestep, int threadIndex)
{
   dFloat Ixx;
   dFloat Iyy;
   dFloat Izz;
   dFloat mass;

   // for this tutorial the only external force in the Gravity
   NewtonBodyGetMassMatrix (body, &mass, &Ixx, &Iyy, &Izz);

   Ogre::Vector3 gravityForce  (0.0f, mass * -10, 0.0f);
   NewtonBodyAddForce(body, &gravityForce[0]);
}


I set one breakpoint on each callback in my IDE (VC++ 2010) and they are not reached. :(

Cookiezzz
Last edited by Cookiezzz on Tue Jul 19, 2011 11:44 am, edited 1 time in total.
Cookiezzz
 
Posts: 5
Joined: Fri Jul 15, 2011 9:15 am

Re: Callback is not reached

Postby JoeJ » Sat Jul 16, 2011 12:47 pm

I don't know Ogre, but in Newton you need to apply gravity inside the Force an Tourque callback,
which in your case seems to be the SetTransformCallback.
Then bodys will fall down and collide as expected.
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Callback is not reached

Postby Cookiezzz » Sat Jul 16, 2011 1:14 pm

Hi,
I do!

Code: Select all
   NewtonBodyAddForce(body, &gravityForce[0]);
Cookiezzz
 
Posts: 5
Joined: Fri Jul 15, 2011 9:15 am

Re: Callback is not reached

Postby Cookiezzz » Sat Jul 16, 2011 2:09 pm

Ok, i tried OgreNewt now. I read the Tutorial and sourcecode on http://www.ogre3d.org/tikiwiki/OgreNewt+2 , but in the line
Code: Select all
OgreNewt::Body* playerbody = new OgreNewt::Body( mWorld, col );
it says:
Unbehandelte Ausnahme bei 0x002e40a9 in The Timetravel.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xd7cff9a3.

in english(Google translate):
An unhandled exception occurred during 0x002e40a9 in The Timetravel.exe: 0xC0000005: Access violation reading location at 0xd7cff9a3.


Whats wrong? :cry: :cry: :cry:

Cookiezzz
Cookiezzz
 
Posts: 5
Joined: Fri Jul 15, 2011 9:15 am

Re: Callback is not reached

Postby JoeJ » Sat Jul 16, 2011 2:53 pm

Cookiezzz wrote:Hi,
I do!


Hoppala, it seems i only saw the first half of your post... maybe got lost in scrolling :oops:
Now it looks complete and right, and i can't help.
You've postet this in Ogre forum too already?
Beside that the only tip i have is to get just a single body falling down working first.

playerBody = CreateRigidBody (World, PlayerNode, shape, 0.0f);

...can't find anything on that on Ogre Wiki, but maybe it assigns zero mass == static?
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Callback is not reached

Postby JoeJ » Sat Jul 16, 2011 3:10 pm

Cookiezzz wrote:Unbehandelte Ausnahme bei 0x002e40a9 in The Timetravel.exe: 0xC0000005: Zugriffsverletzung beim Lesen an Position 0xd7cff9a3.



Note

If you get an error like this:
Unhandled exception at 0x00423d76 in newtontest.exe: 0xC0000005: Access violation reading location 0xfeef003e
In the line: m_debugnode->detachAllObjects();

You need to deinit the debugger. To do this, add
OgreNewt::Debugger::getSingleton().deInit();
to the destructor of your application class.



Source: http://www.ogre3d.org/tikiwiki/OgreNewt&structure=Libraries

... maybe not very helpful too :)
User avatar
JoeJ
 
Posts: 1489
Joined: Tue Dec 21, 2010 6:18 pm

Re: Callback is not reached

Postby Cookiezzz » Sun Jul 17, 2011 11:10 am

JoeJ wrote:
Cookiezzz wrote:Hi,
I do!


Hoppala, it seems i only saw the first half of your post... maybe got lost in scrolling :oops:
Now it looks complete and right, and i can't help.
You've postet this in Ogre forum too already?
Beside that the only tip i have is to get just a single body falling down working first.

playerBody = CreateRigidBody (World, PlayerNode, shape, 0.0f);

...can't find anything on that on Ogre Wiki, but maybe it assigns zero mass == static?


Yeah, right! :D The Callbacks are now reached,but the Body starts on the floor, so he doesn't fall down :? .


EDIT: OK, it seems that Ogres Matrix is the other way around than Newtons Matrix. It works now! But (Problems never end...) Body 1 doesn't collide with Body 2....
Cookiezzz
 
Posts: 5
Joined: Fri Jul 15, 2011 9:15 am

Re: Callback is not reached

Postby Cookiezzz » Tue Jul 19, 2011 11:44 am

Ok, i add this utility function now and it works fine.It Converts an Ogrematrix to a newtonmatrix:

Code: Select all
void ConvertMatrix(Ogre::Matrix4 &mat)
{
   Ogre::Matrix4 tmp = mat;
   mat.m[0][1] = tmp.m[1][0];
   mat.m[0][2] = tmp.m[2][0];
   mat.m[0][3] = tmp.m[3][0];
   mat.m[1][0] = tmp.m[0][1];
   mat.m[1][2] = tmp.m[2][1];
   mat.m[1][3] = tmp.m[3][1];
   mat.m[2][0] = tmp.m[2][0];
   mat.m[2][1] = tmp.m[1][2];
   mat.m[2][3] = tmp.m[3][2];
   mat.m[3][0] = tmp.m[0][3];
   mat.m[3][1] = tmp.m[1][3];
   mat.m[3][2] = tmp.m[2][3];
}


all is solved now!
Cookiezzz
 
Posts: 5
Joined: Fri Jul 15, 2011 9:15 am

Re: Callback is not reached

Postby jiandingzhe » Wed Jul 20, 2011 1:33 am

Cookiezzz wrote:Ok, i add this utility function now and it works fine.It Converts an Ogrematrix to a newtonmatrix:

Code: Select all
void ConvertMatrix(Ogre::Matrix4 &mat)
{
   Ogre::Matrix4 tmp = mat;
   mat.m[0][1] = tmp.m[1][0];
   mat.m[0][2] = tmp.m[2][0];
   mat.m[0][3] = tmp.m[3][0];
   mat.m[1][0] = tmp.m[0][1];
   mat.m[1][2] = tmp.m[2][1];
   mat.m[1][3] = tmp.m[3][1];
   mat.m[2][0] = tmp.m[2][0];
   mat.m[2][1] = tmp.m[1][2];
   mat.m[2][3] = tmp.m[3][2];
   mat.m[3][0] = tmp.m[0][3];
   mat.m[3][1] = tmp.m[1][3];
   mat.m[3][2] = tmp.m[2][3];
}


all is solved now!


Actually not needed. Ogre Matrix has a method "transpose" that does what you need.
I also really confused about why Ogre choose that way to store matrix.
User avatar
jiandingzhe
 
Posts: 48
Joined: Fri Jul 08, 2011 11:21 am
Location: Beijing


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest