问题
I'm trying to implement a simple ghost object in bulletphysics, this is how I create the ghost objects:
btGhostPairCallback* ghostCall = new btGhostPairCallback();
world->getBroadphase()->getOverlappingPairCache()->setInternalGhostPairCallback(ghostCall);
btGhostObject* ghostObj = new btGhostObject();
btCollisionShape* shape = new btBoxShape(btVector3(ofGetWidth()+1000, ofGetHeight()+1000, 50));
ghostObj->setCollisionShape(shape);
btTransform trans;
trans.setIdentity();
trans.setOrigin(btVector3(0,0,-500));
ghostObj->setWorldTransform(trans);
ghostObj->setCollisionFlags( btCollisionObject::CF_NO_CONTACT_RESPONSE);
world->addCollisionObject(ghostObj,btBroadphaseProxy::SensorTrigger,btBroadphaseProxy::AllFilter & ~btBroadphaseProxy::SensorTrigger);
and this is how to try to find the collision:
btCollisionObject* obj = world->getCollisionObjectArray()[j];
btRigidBody* body = btRigidBody::upcast(obj);
btAlignedObjectArray < btCollisionObject* > objsInsidePairCachingGhostObject;
btAlignedObjectArray < btCollisionObject* >* pObjsInsideGhostObject = NULL;
btGhostObject* ghost = btGhostObject::upcast(obj);
if(ghost){
objsInsidePairCachingGhostObject.resize(0);
pObjsInsideGhostObject = &ghost->getOverlappingPairs();
cout << ghost->getNumOverlappingObjects() << endl;
but I always get a response that all my world objects are in collision with the ghost object.
Anyone can help me to get a functional simple ghost object?
thanks
回答1:
From what little I understand of the GhostObject, you're overriding its default collision flags. Try changing this line
ghostObj->setCollisionFlags( btCollisionObject::CF_NO_CONTACT_RESPONSE);
to:
ghostObj->setCollisionFlags( ghostObj->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE);
HTH
回答2:
While I agree that the collision flags should be set properly by adding the new flag to the existing set of flags I'd also like to point out that the parameter to btBoxShape is a btVector3 that defines the half-extents of the object. That means the width, height, and length are actually twice as large as those parameters.
来源:https://stackoverflow.com/questions/5202594/ghost-objects-bulletphysics