Determining normal of collision

jtm
Posts: 2
Joined: Sun Oct 01, 2006 9:03 am

Determining normal of collision

Post by jtm »

Hey,

I'm using bullet to make a game where a ball shaped vehicle races around a track. I would like to be able to determine the normal of the point where the ball touches the track. I noticed that the collision callback doesn't actually provide collision data. Is there a way for me to get this information elsewhere, i.e. in the CollisionWorld class?

Cheers,

-JtM
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Post by Erwin Coumans »

Are you using the latest Bullet 2.11 version? A large refactoring and cleanup got rid of a lot of things, including CcdPhysicsEnvironment, which is replaced by btDynamicsWorld derived classes.

Which Collision Callback are you referring to?
All contact points are available in the btCollisionWorld, see the CollisionInterfaceDemo to access them, throught the btPersistentManifold:

Code: Select all

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

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

			glBegin(GL_LINES);
			glColor3f(1, 0, 1);
			
			btVector3 ptA = pt.getPositionWorldOnA();
			btVector3 ptB = pt.getPositionWorldOnB();

			glVertex3d(ptA.x(),ptA.y(),ptA.z());
			glVertex3d(ptB.x(),ptB.y(),ptB.z());
			glEnd();
		}

		//you can un-comment out this line, and then all points are removed
		//contactManifold->clearManifold();	
	}
There is also a global callback for each newly added contact point, and for each destroyed contact point. See ConcaveDemo for an example. However, for existing contact points, there is no callback, they are just kept and updated in the btPersistentManifold class for the two involved btCollisionObjects.

We might add some API/utility functions to get easier access, based on what users want.

Thanks for the feedback,
Erwin
jtm
Posts: 2
Joined: Sun Oct 01, 2006 9:03 am

Post by jtm »

Thanks this is what I'm looking for. At the moment I'm still using 2.0e but I
plan to update to the latest refactored version soon. The collision callback I was referring to is the old one from CcdPhysicsEnvironment. Basically what I'm interested in doing is getting the rough contact position between the ball and the track and calculating the normal based on that so that I can change the ball's gravity vector and have it 'stick' to the track no matter what angle the surface is.

I'd also like to mention that I'm having a lot of fun playing with Bullet. Concave mesh support is really useful and makes life quite a bit easier. I'll release some screenshots of the game soon once its a bit further along.

Cheers,

-JtM