Collision only working sometimes !??!

webjeff
Posts: 15
Joined: Tue Dec 16, 2008 1:46 am

Collision only working sometimes !??!

Post by webjeff »

Basically, sometimes my collision gets detected, and sometimes it doesnt.... I have this...

Code: Select all

    mDynamicsWorld->stepSimulation(ms/1000.0f,2);

    int numManifolds = mDynamicsWorld->getDispatcher()->getNumManifolds();
    for(int i = 0;i < numManifolds;i++)
        {
        btPersistentManifold* contact = mDynamicsWorld->getDispatcher()->getManifoldByIndexInternal(i);

        if (contact->getNumContacts())
            {
            btRigidBody* a = dynamic_cast<btRigidBody*>((btRigidBody*)contact->getBody0());
            btRigidBody* b = dynamic_cast<btRigidBody*>((btRigidBody*)contact->getBody1());

            EntBase* ent1 = (EntBase*)a->getUserPointer();
            EntBase* ent2 = (EntBase*)b->getUserPointer();
            if (ent1 && ent2)
                {
                ent1->Collide(ent2);
                }
            }
        }


I have 3 objects, my ground, a box target and a box object thats being thrown. If it hits the target box I should get a collision, but sometimes it doesn't work, and then I get the ground collision. Any ideas if the above code looks correct?

Thanks!
Jeff.
webjeff
Posts: 15
Joined: Tue Dec 16, 2008 1:46 am

Re: Collision only working sometimes !??!

Post by webjeff »

I think the problem is that it's moving too fast that inside the step, its collided and already moving away when I check for a collision, hmmm, any easy methods around this?

Thanks
Jeff.
gleemer
Posts: 15
Joined: Sat Nov 29, 2008 6:34 am

Re: Collision only working sometimes !??!

Post by gleemer »

I found this collision routine on this board somewhere and it has been working nicely.
Hope this helps.
:D

Code: Select all

// ====================================================================================== //
//
// ====================================================================================== //
bool CheckForCollision(btRigidBody* body1, btRigidBody* body2) {
		unsigned short  numContacts;
		
		btCollisionAlgorithm* pAlgorithm = sDynamicsWorld->getDispatcher()->findAlgorithm( body1, body2 );
		btManifoldResult oManifoldResult(  body1, body2 );
		pAlgorithm->processCollision( body1, body2, sDynamicsWorld->getDispatchInfo(), &oManifoldResult );
		btPersistentManifold* pManifold = oManifoldResult.getPersistentManifold();
		numContacts = pManifold->getNumContacts();
		//printf("-- num of contacts=%d\n",   numContacts);
		if ( numContacts > 0 ) {
			// Clear contact pts
			pManifold->clearManifold();
			return true;
		}
		return false;
		
		
} // CheckForCollision
thloh85
Posts: 26
Joined: Mon Feb 09, 2009 10:07 am

Re: Collision only working sometimes !??!

Post by thloh85 »

Check this link, I think you've got the same problem as I do.

http://www.bulletphysics.com/Bullet/php ... f=9&t=3545