Generally, I want to shoot a bullet at a rigidBody (from a FPS perspectve).
Specifically, I want to use applyImpulse on a RigidBody and provide a force vector and relative position of the vector to the body.
My plan is to use the camera lookat for direction, normalize that, add a impVec and provide the relative offset from the pick pos and the center of mass xform. However, I
'm not getting the results that I expected. I've also used the post below to additionally include the world rotation of the body into the force vector.
I suspect others might have a different idea on how to approach this, but my only real requirement is that I use applyImpulse and use the ray from the mouse click to provide direction to the impulse.
http://bulletphysics.org/Bullet/phpBB3/ ... f=9&t=2366
Anyone have suggestions? or a link to examples?
Thanks
SOLVED: Shoot a bullet at a rigidBody
-
- Posts: 10
- Joined: Thu Jan 20, 2011 9:36 pm
SOLVED: Shoot a bullet at a rigidBody
Last edited by chbfiv on Fri Jun 22, 2012 11:51 am, edited 1 time in total.
-
- Posts: 4
- Joined: Wed Nov 19, 2008 7:22 pm
Re: Shoot a bullet at a rigidBody
I was just working on the same thing this morning. Here's some sample code that I wrote for my implementation. Does this help?
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.
Code: Select all
btVector3 start = cameraPosition;
btVector3 end = start + (cameraDirection * SIZE_OF_WORLD);
btCollisionWorld::ClosestRayResultCallback RayCallback( start, end );
dynamicsWorld->rayTest(start, end, RayCallback);
if(RayCallback.hasHit())
{
/// 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
gameObj->btRigidBody->activate(true);
btVector3 centerOfMass = gameObj->btRigidBody->getCenterOfMassPosition();
btVector3 hitPoint = RayCallback.m_hitPointWorld;
btVector3 force = cameraDirection * FORCE_SCALING_FACTOR;
gameObj->btRigidBody->applyImpulse( force, hitPoint - centerOfMass );
}
}
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.
-
- Posts: 10
- Joined: Thu Jan 20, 2011 9:36 pm
SOLVED: Shoot a bullet at a rigidBody
Thanks. I had a few issues, but my main problem was that the force vector is using world space and I was trying to use local space.