Shooter collision callback

luislairet
Posts: 1
Joined: Fri Aug 03, 2012 3:05 am

Shooter collision callback

Post by luislairet »

I've been looking at bullet physics for quite a bit now, and I've recently decided to build an FPS that utilizes the API. I've been looking at the demo's and trying to put a prototype up that serves as an example for different game mechanics.

I've put up a demo app with static geometry and a basic sphere that moves around using WASD, that's all fair and simple but now I'm trying to fire projectiles from the sphere onto other objects. Projectiles in the game are handled in 2 ways. A sniper kind of weapon that does a raycast to determine if it hit an enemy. And all the other weapons will use sphere's as projectiles that move at slower speeds (not instant).

My problem is when shooting projectiles I would like to get more information on the object that it has collided with, when doing this:

Code: Select all

const int manifoldCount(m_dispatcher->getNumManifolds());
	for(int loop = 0; loop < manifoldCount; loop++) 
	{
		const btPersistentManifold *mf = m_dispatcher->getManifoldByIndexInternal(loop);
		const void *obja = mf->getBody0();
		const void *objb = mf->getBody1();

		if(obja == moveableBall || objb == moveableBall) 
		{
		// This manifold deals with the btRigidBody we are looking for.
		// A manifold is a RB-RB pair containing a list of potential (predicted) contact points.
		 const unsigned int numContacts(mf->getNumContacts());
			for(int check = 0; check < numContacts; check++) 
			{
				 const btManifoldPoint &pt(mf->getContactPoint(check));
				 // do something here, in case you're interested.
			}
		}
	}
I can't really access what object types are colliding, is there any way to get more specific data on the objects that have collided?

For example I want to know when rockets collide with walls, characters, floor, etc. When rifle rounds collide with wall,characters...

Does anyone have a recommendation or any idea on how I could put this together?

Thank you very much,
Luis Lairet
Crackerjack55
Posts: 3
Joined: Mon Sep 24, 2012 11:42 pm

Re: Shooter collision callback

Post by Crackerjack55 »

I'm not sure if this is a little late but what you need to do is use m_rigidBody->setUserPointer(void*), that pointer can point to any of your own classes. Have it point to a common base class of colliding objects(In my game, The common base class is GameObject, I just put m_rigidBody->setUserPointer(this) after i'd created the rigidBody info for the GameObject.

Then in your // do something here, in case you're interested bit
you need to call obja->getUserPointer() and cast it to your base class.

In mine it looks something like this:
m_numContacts = mp_manifold->getNumContacts();
for ( j=0; j < m_numContacts; j++)
{
btManifoldPoint& pt = mp_manifold->getContactPoint(j);
if (pt.getDistance()<0.f)
{
GameObject* go1 = static_cast<GameObject*>(mp_obA->getUserPointer());
GameObject* go2 = static_cast<GameObject*>(mp_obB->getUserPointer());
go1->BTCollided();
go2->BTCollided();
}
}

Hope this helps or at least helps someone else who's also struggling.