NewtonMaterialSetCollisionCallback

A place to discuss everything related to Newton Dynamics.

Moderators: Sascha Willems, walaber

NewtonMaterialSetCollisionCallback

Postby Yustme » Mon Aug 04, 2008 2:07 pm

Hi,

How can i use the NewtonMaterialSetCollisionCallback function in the newton version 2.0 beta 16?

I tried to understand it from the samples that came along with them.

What i am trying to do is, play a sound if 2 specific bodies collide against each other.

And if was also wondering if this is possible if an object collides against a wall of a quake 3 map.

Thanks in advance!
Yustme
 
Posts: 37
Joined: Sat Dec 08, 2007 2:06 pm

Re: NewtonMaterialSetCollisionCallback

Postby Julio Jerez » Mon Aug 04, 2008 2:37 pm

you can modify the basic contact callback form file PhysicsUtil.cpp to somethong liek this

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;   
      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: NewtonMaterialSetCollisionCallback

Postby Yustme » Mon Aug 04, 2008 2:39 pm

Hi Julio,

Nice code snippet, thank you very much!

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

Re: NewtonMaterialSetCollisionCallback

Postby Yustme » Mon Aug 04, 2008 5:13 pm

Hi again,

There are a few things you forgot to mention Julio. I get 4 compiler errors which can be solved very easy.

In the code snippet you gave me, there are 2 variables that are not declared. And a function that i don't got and don't know how to fill it in:

"bestcontact" undeclared;
"volume" undeclared;
"GetVolume" identifier not found;

I have looked in the newton header file for the volume and getVolume, but i found nothing, neither in the solution of version 2 beta 16.

Can you tell me how to declare them?

Thanks in advance!
Yustme
 
Posts: 37
Joined: Sat Dec 08, 2007 2:06 pm

Re: NewtonMaterialSetCollisionCallback

Postby Julio Jerez » Mon Aug 04, 2008 5:28 pm

I di nti tested it.

Best contact is a local variable, it is crrated at the beggining an dI mispeleed.
volume is a funtion you sould write to convert the pedd if teh impact into a round volume.
basically teh funtion sarch for thr contact with the strinest impact velocity and save it into the varicable best contact.
the i play the soudn base of teh speed of teh contact.

thet is only one critira to play sound, you cna also play eh saoun base of scraping, for exampel to ycan calculate teh conct along the tangent and play friction sounds.

essencially is just a search for the maximun feature over the contact array, and use the contact that satisfy the condition to play the sound.
.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonMaterialSetCollisionCallback

Postby Yustme » Mon Aug 04, 2008 5:43 pm

Hi Julio,

I understand you. I got another question.

How can i get the 2 bodies that are colliding against each other (or their ID's) in that GenericContactProcess function?

I am trying to do something like this:

Code: Select all
void* firstContact = NewtonContactJointGetFirstContact(contactJoint);
void* secondContact = NewtonContactJointGetNextContact(contactJoint, firstContact);
   
if(firstContact == sphereID && secondContact == cubeID)
{
   // play sound
}
Yustme
 
Posts: 37
Joined: Sat Dec 08, 2007 2:06 pm

Re: NewtonMaterialSetCollisionCallback

Postby Julio Jerez » Mon Aug 04, 2008 6:12 pm

the contact is a joint you can use thse funtions
body0 = NewtonJointGetBody0(contactJoint);
body1 = NewtonJointGetBody1(contactJoint);

I use them in the SDK in many callbacks


the code you pster below donot make sence, the contact joint is a collision between two bodies.
all of the contact in that jint are contcat betweeen teh same two bodies.
Code: Select all
void* firstContact = NewtonContactJointGetFirstContact(contactJoint);
void* secondContact = NewtonContactJointGetNextContact(contactJoint, firstContact);
   
if(firstContact == sphereID && secondContact == cubeID)
{
   // play sound
}


you need to get the bodies from the joints, and from the body retriave the userdata, wich will be you graphics body, and from the body get the IDs of the object.
Julio Jerez
Moderator
Moderator
 
Posts: 12249
Joined: Sun Sep 14, 2003 2:18 pm
Location: Los Angeles

Re: NewtonMaterialSetCollisionCallback

Postby Yustme » Mon Aug 04, 2008 6:21 pm

Julio Jerez wrote:the contact is a joint you can use thse funtions
body0 = NewtonJointGetBody0(contactJoint);
body1 = NewtonJointGetBody1(contactJoint);

I use them in the SDK in many callbacks


the code you pster below donot make sence, the contact joint is a collision between two bodies.
all of the contact in that jint are contcat betweeen teh same two bodies.
Code: Select all
void* firstContact = NewtonContactJointGetFirstContact(contactJoint);
void* secondContact = NewtonContactJointGetNextContact(contactJoint, firstContact);
   
if(firstContact == sphereID && secondContact == cubeID)
{
   // play sound
}


you need to get the bodies from the joints, and from the body retriave the userdata, wich will be you graphics body, and from the body get the IDs of the object.



Hi Julio,

I know what code i pasted wasn't making sence and shouldn't work. But it was to make the thing i want to do clear and it worked :D

Now i know how to get the bodies which i was after. You may have used it many times in the SDK, but even thought i saw it, i probably didn't know what it does. But looking at it again, it should have made sence!

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


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 18 guests