Physics Simulation Forum

 

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Jun 20, 2012 2:13 pm 
Offline

Joined: Thu Jan 20, 2011 9:36 pm
Posts: 10
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.

viewtopic.php?f=9&t=2366

Anyone have suggestions? or a link to examples?

Thanks


Last edited by chbfiv on Fri Jun 22, 2012 11:51 am, edited 1 time in total.

Top
 Profile  
 
PostPosted: Wed Jun 20, 2012 4:29 pm 
Offline

Joined: Wed Nov 19, 2008 7:22 pm
Posts: 4
I was just working on the same thing this morning. Here's some sample code that I wrote for my implementation. Does this help?

Code:
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 );
    }
}


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.


Top
 Profile  
 
PostPosted: Fri Jun 22, 2012 11:50 am 
Offline

Joined: Thu Jan 20, 2011 9:36 pm
Posts: 10
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group