[C++] Collision callback

Ripiz
Posts: 47
Joined: Mon Aug 16, 2010 10:43 am

[C++] Collision callback

Post by Ripiz »

I'm trying to implement something similar to missiles, which explode on first contact with anything, however I wasn't able to find how to create Collision Callback or something similar. Maybe anyone could help me?

Here's my body code:

Code: Select all

		bodyCollision = new btSphereShape(1);
		btTransform startTransform;
		startTransform.setIdentity();
		startTransform.setOrigin(btVector3(position.x, position.y, position.z));
		startTransform.setRotation(btQuaternion(btVector3(1, 0, 0), 0) * btQuaternion(btVector3(0, 1, 0), 0) * btQuaternion(btVector3(0, 0, 1), 0));

		btVector3 localInertia(0, 0, 0);
		bodyCollision->calculateLocalInertia(1, localInertia);

		bodyMotionState = new btDefaultMotionState(startTransform);
		btRigidBody::btRigidBodyConstructionInfo rbInfo(1, bodyMotionState, bodyCollision, localInertia);

		body = new btRigidBody(rbInfo);
		physics->addRigidBody(body);
		body->setLinearVelocity(btVector3(direction.x * 100, direction.y * 100, direction.z * 100));
		body->setFriction(0.5f);
Thank you in advance.
ramsampath
Posts: 19
Joined: Fri Sep 05, 2008 8:54 pm

Re: [C++] Collision callback

Post by ramsampath »

There are a couple of ways to do this

one way is to implement is

Code: Select all

world->setInternalTickCallback( tickcallback, user_data )
and tickcallback()will be called after every tick and you can do the processing there

or if you want the callback to be called after the collision point is processed there's a global variable called

gContactAddedCallback which you can set to the callback function , the function is called with several parameters which include btManifoldPoint, btCollisionObject* etc.

hope this helps.

ram.
Ripiz
Posts: 47
Joined: Mon Aug 16, 2010 10:43 am

Re: [C++] Collision callback

Post by Ripiz »

I've tried Ghost Object but no luck

Code: Select all

		bodyCollision = new btSphereShape(1);
		btTransform startTransform;
		startTransform.setIdentity();
		startTransform.setOrigin(btVector3(position.x, position.y, position.z));
		startTransform.setRotation(btQuaternion(btVector3(1, 0, 0), 0) * btQuaternion(btVector3(0, 1, 0), 0) * btQuaternion(btVector3(0, 0, 1), 0));

		btVector3 localInertia(0, 0, 0);
		bodyCollision->calculateLocalInertia(1, localInertia);

		bodyMotionState = new btDefaultMotionState(startTransform);
		btRigidBody::btRigidBodyConstructionInfo rbInfo(1, bodyMotionState, bodyCollision, localInertia);

		body = new btRigidBody(rbInfo);
		physics->addRigidBody(body);
		body->setLinearVelocity(btVector3(direction.x * 100, direction.y * 100, direction.z * 100));
		body->setFriction(0.5f);

		m_ghostObject = new btPairCachingGhostObject();
		m_ghostObject->setWorldTransform(startTransform);
		physics->getPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
		m_ghostObject->setCollisionShape(bodyCollision);

		physics->addCollisionObject(m_ghostObject, btBroadphaseProxy::DefaultFilter, btBroadphaseProxy::DefaultFilter);
My m_ghostObject never changes it's origin =/