Collision detecttion between two btrigidBody

landryx
Posts: 3
Joined: Tue Mar 24, 2009 9:52 pm

Collision detecttion between two btrigidBody

Post by landryx »

Hello

I have the impression that my question is frequently asked, but there is no solution witch could help me. I'am actually working on a school project in wich i have to build a robot, using sensors especially.

My problem is that the robot should have collision sensor (and some others) witch have to detect if a collision happened between the robot and another rigid body. The fact is that I don't know how to get the information about the collision, and I tryed many Bullet functions, but nothing worked. So i need some help.

Could some one tell me how to do this ? I'm working on it for two weeks, and i am discouraged a bit :( .

Thanks

Edit >>
PS: I am a new Bullet user, so i don't know if my message needs more information. So let me know about it ^^
landryx
Posts: 3
Joined: Tue Mar 24, 2009 9:52 pm

Re: Collision detecttion between two btrigidBody

Post by landryx »

Hi,

I found this in the Bullet user manual:

Code: Select all

struct fCallback : public btOverlapFilterCallback
{

	// return true when pairs need collision
	virtual bool needBroadphaseCollision(btBroadphaseProxy* proxy0,btBroadphaseProxy* proxy1)
	const
	{
		bool collides = (proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0;

		collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask);

		//add some additional logic here that modified 'collides'
		return collides;
	}
};
I tried to use it, but i don't understand verywell what the function needBroadphaseCollision is really about. I just want to know that the collision hapened between two bodies (one of them is on my robot, representing a sensor, and the other one is a static body on the scene) due to send an order to my robot.

Please Help ^^ ...

Thanks
xwipeoutx
Posts: 9
Joined: Fri Mar 20, 2009 3:57 am

Re: Collision detecttion between two btrigidBody

Post by xwipeoutx »

I'm fairly sure that function there is just for filtering possible collisions, so bullet knows whether or not to keep trying it.

What you want to do is loop through all the manifolds and contacts of those manifolds after you do a world->performDiscreteCollisionDetection() call (or in your case, after a stepSimulation call - if your framerate is low, you need to do it INSIDE the stepSimulation call, check the wiki for how to do that)

Code: Select all

int numManifolds = phyWorld->getDispatcher()->getNumManifolds();
	for (int i=0;i<numManifolds;++i)
	{
		btPersistentManifold* contactManifold = phyWorld->getDispatcher()->getManifoldByIndexInternal(i);
		btCollisionObject* obA = static_cast<btCollisionObject*>(contactManifold->getBody0());
		btCollisionObject* obB = static_cast<btCollisionObject*>(contactManifold->getBody1());

		int numContacts = contactManifold->getNumContacts();
		for (int j=0;j<numContacts;j++)
		{

			btManifoldPoint& pt = contactManifold->getContactPoint(j);
			if (pt.getDistance()<0.f)
			{
				/** Your collision logic can go in here */
			}
		}

	}
That's the idea anyway.

If you want some kind of response, you'll need to use setUserPointer and getUserPointer to figure out how to handle the different object. I just created a physics response interface for this, and attached it when I create my object:

Code: Select all

class IPhysicsResponse
{
public:
	IPhysicsResponse(void){}
	virtual ~IPhysicsResponse(void){}
	virtual void Collide(IPhysicsResponse *other)=0;
};
and then in the collision bit, simply something like:

Code: Select all

				void* ptrA = obA->getUserPointer();
				void* ptrB = obB->getUserPointer();

				IPhysicsResponse *responseA = ptrA ? static_cast<IPhysicsResponse*>(ptrA) : NULL;
				IPhysicsResponse *responseB = ptrB ? static_cast<IPhysicsResponse*>(ptrB) : NULL;

				if (responseA)
					responseA->Collide(responseB);
				if (responseB)
					responseB->Collide(responseA);
				break;
Override the IPhysicsResponse with your own response class, and call setUserPointer when creating the rigid body and voila!

I've only just got this worked out myself, so I hope that gives you less headaches then I had!
landryx
Posts: 3
Joined: Tue Mar 24, 2009 9:52 pm

Re: Collision detecttion between two btrigidBody

Post by landryx »

hi,

thanks, in deed i have less headaches ^^. My code works nice now (i wish it keeps going like that) ...

So i have another question, is it possible to make an object walk through another one ?

cheers :mrgreen:
xwipeoutx
Posts: 9
Joined: Fri Mar 20, 2009 3:57 am

Re: Collision detecttion between two btrigidBody

Post by xwipeoutx »

I haven't done it yet myself, but check out collision flags in the wiki - not certain if it works for dynamics, or if it's just collision, but I'd imagine it's dynamics as well.