Bullet Physics Simplest Collision Example

前端 未结 3 2496
醉酒成梦
醉酒成梦 2021-02-20 11:44

I\'m trying to use Bullet Physics for collision detection only. I don\'t need it to move any objects for me or handle rendering with callbacks. I just want to update object loca

3条回答
  •  别那么骄傲
    2021-02-20 12:06

    You can check the contact information as explained here:

    Contact Information

    The best way to determine if collisions happened between existing objects in the world, is to iterate over all contact manifolds. This should be done during a simulation tick (substep) callback, because contacts might be added and removed during several substeps of a single stepSimulation call. A contact manifold is a cache that contains all contact points between pairs of collision objects. A good way is to iterate over all pairs of objects in the entire collision/dynamics world:

    //Assume world->stepSimulation or world->performDiscreteCollisionDetection has been called
    
        int numManifolds = world->getDispatcher()->getNumManifolds();
        for (int i=0;igetDispatcher()->getManifoldByIndexInternal(i);
            btCollisionObject* obA = static_cast(contactManifold->getBody0());
            btCollisionObject* obB = static_cast(contactManifold->getBody1());
    
            int numContacts = contactManifold->getNumContacts();
            for (int j=0;jgetContactPoint(j);
                if (pt.getDistance()<0.f)
                {
                    const btVector3& ptA = pt.getPositionWorldOnA();
                    const btVector3& ptB = pt.getPositionWorldOnB();
                    const btVector3& normalOnB = pt.m_normalWorldOnB;
                }
            }
        }
    

    You may be interested in btGhostObject that keeps track of its own overlapping pairs.

提交回复
热议问题