collision detection between 2 bodies

Yustme

04-01-2008 16:31:24

Hi,

I'm trying to play a sound when 2 specific bodies are colliding against each other. I have managed to make the callbacks for it but i don't know how to actually check if 2 bodies collided against each other.

I have searched in the forums of newtondynamics and ogrenewt. I couldn't find an example where this was actually done in C++.

This is my default properties of newton:


// Set up default material properties for newton
int i = NewtonMaterialGetDefaultGroupID(nWorld);
NewtonMaterialSetDefaultFriction (nWorld, i, i, 0.5f, 0.4f);
NewtonMaterialSetDefaultElasticity (nWorld, i, i, 0.75f);
NewtonMaterialSetDefaultSoftness (nWorld, i, i, 0.0f);
NewtonMaterialSetCollisionCallback (nWorld, i, i, spherebody1, beginCallback, processCallback, endCallback);
NewtonMaterialSetCollisionCallback (nWorld, i, i, sphereBody2, beginCallback, processCallback, endCallback);



And these are my callback functions:



struct COLLISION
{
const NewtonBody *colBody0, *colBody1;
} g_col;

// check if bodies are almost colliding
int _cdecl beginCallback(const NewtonMaterial* material, const NewtonBody* body0, const NewtonBody* body1)
{
g_col.colBody0 = body0;
g_col.colBody1 = body1;
return 1;
}

// process the collided bodies
int _cdecl processCallback(const NewtonMaterial* material, const NewtonContact* contact)
{
return true;
}

// end the callback and play sound
void _cdecl endCallback(const NewtonMaterial* material)
{

vector3 pos(sphere->getPosition());
const char* filename;
filename = "explosion.wav";
pos.X /= 100 * 10;
pos.Y /= 100 * 10;
pos.Z /= 100 * 10;
engine->play3D(filename, pos);
cout << "pos.X: " << pos.X << " pos.Y: " << pos.Y << " pos.Z: " << pos.Z << endl;
}



Basically, i just want to play a sound when 2 bodies collide. I have no interest whether how fast the sphere's moved or not, or anything else.

But at the moment as it is, it plays a sound the whole time, even if there is no collision.

Can anyone help me with this issue?

Thanks in advance!

walaber

04-01-2008 19:34:38

first, you can use NewtonBodySetUserData and NewtonBodyGetUserData to store a pointer to some object of your own with a NewtonBody. use that to determine exactly which 2 bodies are colliding.

second, you only have a true collision when the contactProcess function is called... contacBegin is called if the AABB of the bodies overlap, and a collision is "likely".

contactProcess is called for each unique collision between the bodies.

contactEnd is called regardless once at the end.

so you would probably want to do something like this:
// process the collided bodies
int _cdecl processCallback(const NewtonMaterial* material, const NewtonContact* contact)
{
g_col.trueCollision = true;
return true;
}

// end the callback and play sound
void _cdecl endCallback(const NewtonMaterial* material)
{
if (g_col.trueCollision) {
vector3 pos(sphere->getPosition());
const char* filename;
filename = "explosion.wav";
pos.X /= 100 * 10;
pos.Y /= 100 * 10;
pos.Z /= 100 * 10;

engine->play3D(filename, pos);
cout << "pos.X: " << pos.X << " pos.Y: " << pos.Y << " pos.Z: " << pos.Z << endl;

g_col.trueCollision = false;
}

}



also, is there a particular reason you are not using OgreNewt? I'm just curious.

Yustme

04-01-2008 20:23:54

Hi walaber,

First of all, thank you for your reply!

I did used the function NewtonBodySetUserData() for all my bodies but the function NewtonBodyGetUserData() is a void which is really odd to me (making a function of return type void with the word "Get" in it).

Can you give me an example on how to compare 2 bodies whether they collided against each other?

As for your curiosity :D , i just started with ogre3d. I used to use irrlicht as a 3d engine. I used newton with it too but with no wrapper. Thats how i learned newton :D

Thanks in advance!

Yustme

04-01-2008 21:49:47

Hi,

I managed to fix it by getting the user data with (NewtonBody*) cast in front of the NewtonBodyGetUserData() function and passed it on a temporarily variable.

walaber

04-01-2008 22:26:41

feel free to give OgreNewt a try, it should make Ogre3d + newton projects much simpler, and let you ignore all of the details of setting up low-level callbacks, etc. you can get the download from my website (walaber.com), it includes several demos.