how to get user data

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

how to get user data

Postby Yustme » Tue Aug 05, 2008 4:39 pm

Hi,

How can i get the user data that is passed in this callback:

void NewtonMaterialSetCollisionCallback (const NewtonWorld* newtonWorld, int id0, int id1, void* userData,
NewtonOnAABBOverlap aabbOverlap, NewtonContactProcess process);

I am trying to get the user data in the NewtonContactProcess function. I pass the class as userdata so that i can use that object to access data members of the class that are not static.

If it is this function: NewtonMaterialGetUserData() to retrieve the userdata, can you show me how to use it?

I must be doing something wrong, because my game crashes when it gets at this line:

if(id1 == newtonPt->sphereID || id1 == newtonPt->q3MapID)

GenericContactProcess code snippet:

Code: Select all
void CNewton::GenericContactProcess(const NewtonJoint* contactJoint, dFloat timestep, int threadIndex)
{
   NewtonBody* body0 = NewtonJointGetBody0(contactJoint);
   NewtonBody* body1 = NewtonJointGetBody1(contactJoint);

   int id1 = NewtonBodyGetMaterialGroupID(body0);
   int id2 = NewtonBodyGetMaterialGroupID(body1);
   
   CNewton* newtonPt = (CNewton*)NewtonMaterialGetUserData(NewtonBodyGetWorld(body0), id1, id2);
   
   if(id1 == newtonPt->sphereID || id1 == newtonPt->q3MapID)
      if(id2 == newtonPt->sphereID || id2 == newtonPt->q3MapID)
      {
         newtonPt->PlayTheSound();
      }
      
   delete [] newtonPt;
}


NewtonMaterialSetCollisionCallback (world, sphereID, q3MapID, userData, NULL, &CNewton::GenericContactProcess);

The userData is a type of CNewton.

What am i doing wrong guys?
Yustme
 
Posts: 37
Joined: Sat Dec 08, 2007 2:06 pm

Re: how to get user data

Postby Julio Jerez » Tue Aug 05, 2008 4:44 pm

you said you want to play a sounds per body. I gave yo uteh funtion to get user data from the bodies

The users data in the material you can use to play a sounds per material, that is bodies of the same type will play the same effect. For that you can call the material function to get the user material user data.
something like this
Code: Select all
void CNewton::GenericContactProcess(const NewtonJoint* contactJoint, .....)
{
 material = NewtonContactJointGetFirstContact (contactJoint);
 userData = NewtonMaterialGetMaterialPairUserData (material);....
}
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: how to get user data

Postby Yustme » Tue Aug 05, 2008 4:53 pm

Hi,

At this point I only want to play a sound when 2 bodies collide against each other.

This is how i did it a few months back which worked great.

So how would i get the user data that i passed to the NewtonMaterialSetCollisionCallback ?
Yustme
 
Posts: 37
Joined: Sat Dec 08, 2007 2:06 pm

Re: how to get user data

Postby Julio Jerez » Tue Aug 05, 2008 4:57 pm

I just told you.
void CNewton::GenericContactProcess(const NewtonJoint* contactJoint, .....)
{
material = NewtonContactJointGetFirstContact (contactJoint);
userData = NewtonMaterialGetMaterialPairUserData (material);
....
}

if you were doing teh way you sho win teh code above , you were doing it wrong.

the funtion NewtonMaterislGet( id, id)
are for defining the material graph, teh matrisl graph is node in copied to each contact,
therefore in the callback you must use NewtonMateriialGet (material* contact)


are you find this too complex?
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: how to get user data

Postby Yustme » Tue Aug 05, 2008 5:01 pm

OMG! Impossible! You must have changed your post while i was typing :D

No, it's not too complex. The problem is, the functions are so look a like. Always having a hard time finding the right one.

Because i don't know what they do behind the scenes.

Thank you!
Yustme
 
Posts: 37
Joined: Sat Dec 08, 2007 2:06 pm

Re: how to get user data

Postby Julio Jerez » Tue Aug 05, 2008 5:05 pm

here I modified the previus function to show you how to get the user data from the material in the call back
Code: Select all
void GenericContactProcess (const NewtonJoint* contactJoint, dFloat timestep, int threadIndex)
{
   float maxSound = 0;
   NewtonMaterial* bestcontak = 0;;

   for (void* contact = NewtonContactJointGetFirstContact (contactJoint); contact; contact = NewtonContactJointGetNextContact (contactJoint, contact)) {
      dFloat speed;
      dVector point;
      dVector normal;   
      dVector dir0;   
      dVector dir1;   
      NewtonMaterial* material;

      material = NewtonContactGetMaterial (contact);
      NewtonMaterialGetContactPositionAndNormal (material, &point.m_x, &normal.m_x);
      NewtonMaterialGetContactTangentDirections (material, &dir0.m_x, &dir1.m_x);
      speed = NewtonMaterialGetContactNormalSpeed(material);
      if ( speed >  maxSound) {
          maxSound = speed;
         bestcontact =  material;
         
      }
   }

   // play sound base of the contact speed.
   if ( speed > MIN_SOUND_SPEED) {
      dVector point;
      dVector normal;   
     MyType* userData;

     // get user data from material contact
     userData =  (MyType*)NewtonMaterialGetMaterialPairUserData (bestcontact);
 
      // ge the position of the contat
      NewtonMaterialGetContactPositionAndNormal (bestcontact, &point.m_x, &normal.m_x);
     
      volume = GetVolume ( speed);   
      playSpound (postion,   volume);
   }

   
   // do other stuff with contacts
}
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: how to get user data

Postby Yustme » Tue Aug 05, 2008 6:13 pm

Hi Julio,

My game keeps crashing when i try to access the member functions and data's of the CNewton class with userData.

It's like everything in the CNewton class is reset to NULL.

userData is declared as CNewton* as private member. And the userData gets initialized inside the callback like this:

userData = (CNewton*)NewtonMaterialGetMaterialPairUserData (bestcontact);

Then i try to access my function that plays the sound like this:

userData->PlaySound();

And then it executes this block of code:

void CNewton::PlayTheSound()
{
newtonKlangPtr->PlaySound();
}

And at this point it crashes with a null pointer exception looking like this:

Unhandled exception at 0x004155d6 in NewtonIrrlicht.exe: 0xC0000005: Access violation reading location 0x00000040.

I debugged it and i saw that everything gets initialized properly. But once it enters GenericContactProcess callback, newtonKlangPtr is set to NULL.
Yustme
 
Posts: 37
Joined: Sat Dec 08, 2007 2:06 pm

Re: how to get user data

Postby Julio Jerez » Tue Aug 05, 2008 6:45 pm

you are confused with the user data. do you have a demo?
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 19 guests

cron