Jump to content
  • Advertisement
  • entries
  • comments
  • views

Adding bullet collisions

Sign in to follow this  


Bullet Collision

After reading the bullet manual, I only figured out I needed to add a callback at the Tick level. The problem with callbacks and C++ is they dont mix well. Anyway I added a static function to my main Bullet class which holds the Dynamics World, called lsTickCallback. Then I added the following to my constructor.

dynamicsWorld = new btDiscreteDynamicsWorld( dispatcher, axis_sweep3, solver, collisionConfiguration );

// NEW set callback
dynamicsWorld->setInternalTickCallback( &lsTickCallback, static_cast<void *>(this) );

Here is the full static function.

void lsBulletWorld::lsTickCallback( btDynamicsWorld *world, btScalar timeStep )
lsBulletWorld *w = static_cast(world->getWorldUserInfo());
w->lsProcessCallback( timeStep );

As you can see I pull the real class instance I need to call from the User defined pointer I set in the first listing. Once I have the pointer I call my function.

Now I just need to read in all the hit pairs from the cashe. Not to hard. Here's my full debug function.

void lsBulletWorld::lsProcessCallback( btScalar timeStep )
// local instance callback
cout << scene->getName() << " : bullet world just ticked by " << timeStep << " seconds" << endl;

lsNode* obj_A;
lsNode* obj_B;

// get hits
int numManifolds = dynamicsWorld->getDispatcher()->getNumManifolds();
for (int i=0;i {
btPersistentManifold* contactManifold = dynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i);
btCollisionObject* obA = static_cast(contactManifold->getBody0());
btCollisionObject* obB = static_cast(contactManifold->getBody1());

// Scene nodes
obj_A = (lsNode*)obA->getUserPointer();
obj_B = (lsNode*)obB->getUserPointer();
if( obj_A )
cout << "Node A : " << obj_A->getName() << endl;
if( obj_B )
cout << "Node B : " << obj_B->getName() << endl;

int numContacts = contactManifold->getNumContacts();
for (int j=0;j {
btManifoldPoint& pt = contactManifold->getContactPoint(j);
if (pt.getDistance()<0.f)
const btVector3& ptA = pt.getPositionWorldOnA();
const btVector3& ptB = pt.getPositionWorldOnB();
const btVector3& normalOnB = pt.m_normalWorldOnB;

cout << "Hit B: " << ptB.getX();
cout << ", " << ptB.getY();
cout << ", " << ptB.getZ();
cout << endl;

cout << "Normal B: " << normalOnB.getX();
cout << ", " << normalOnB.getY();
cout << ", " << normalOnB.getZ();
cout << endl;



I should explain how I set the UserPointer that I get from obj_A = (lsNode*)obA->getUserPointer();. When any bullet object is set to a lsNode in my scenes I automaticlly set the UserPointer to that node. So now its just an easy operation to tell the nodes what happened. Just keep in mind if your scene objects are deleted, you may not be able to access them. Call in the right order.
Sign in to follow this  


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!