I was just working on the same thing this morning. Here's some sample code that I wrote for my implementation. Does this help?
btVector3 start = cameraPosition;
btVector3 end = start + (cameraDirection * SIZE_OF_WORLD);
btCollisionWorld::ClosestRayResultCallback RayCallback( start, end );
dynamicsWorld->rayTest(start, end, RayCallback);
/// in my implementation, every rigid body have a pointer to a GameObject, which contains other information about the object
/// alternatively, you could just do: btCollisionObject* body = RayCallback.m_collisionObject;
Game::GameObject* gameObj = static_cast<Game::GameObject*>( RayCallback.m_collisionObject->getUserPointer() );
if (gameObj != 0)
// important: remember to disable sleeping by calling this function
btVector3 centerOfMass = gameObj->btRigidBody->getCenterOfMassPosition();
btVector3 hitPoint = RayCallback.m_hitPointWorld;
btVector3 force = cameraDirection * FORCE_SCALING_FACTOR;
gameObj->btRigidBody->applyImpulse( force, hitPoint - centerOfMass );
Key things to remember:
1) Use applyImpulse() instead of applyForce() when you want objects to respond to bullets. From my understanding, applyForce() is better suited for tasks when you want to apply force over a period of time
. I found that calling applyForce() just once (i.e, when a bullet hits an object) does not always register.
2) Always remember to call activate() on your rigid body.