I have just tried the ghost body.
My test case has only 2 bodies in the world.
Body1 is normal body, and Body2 is ghost.
When t=0, both objects are at (0,0,0).
When t=1, Body2 move to (3,0,0).
When t=2, Body2 move back to (0,0,0).
- bullet13.jpg (18.33 KiB) Viewed 8205 times
I want to detect manifold at t=0 and t=2. However, every time step, I can't detect anything.
Where am I wrong?
Here is a full test function(91 lines).
I notice that
collisionPair->m_algorithm always == nullptr
Code: Select all
#pragma once
#include <BulletCollision/CollisionShapes/btCollisionShape.h>
#include <BulletCollision/CollisionDispatch/btGhostObject.h>
#include <BulletCollision/CollisionShapes/btBoxShape.h>
#include <LinearMath/btDefaultMotionState.h>
#include <BulletCollision/BroadphaseCollision/btDbvtBroadphase.h>
#include <BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h>
#include <BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h>
#include <BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h>
#include <BulletCollision/CollisionDispatch/btActivatingCollisionAlgorithm.h>
#include <iostream>
int main(){
btDbvtBroadphase single_btDbvtBroadphase;
btDefaultCollisionConfiguration single_btDefaultCollisionConfiguration;
btCollisionDispatcher single_btCollisionDispatcher{&single_btDefaultCollisionConfiguration};
btSequentialImpulseConstraintSolver single_btSequentialImpulseConstraintSolver;
btDiscreteDynamicsWorld single_btDynamicsWorld{
&single_btCollisionDispatcher,
&single_btDbvtBroadphase,
&single_btSequentialImpulseConstraintSolver,
&single_btDefaultCollisionConfiguration
};
btGhostPairCallback btGhostPairCallback_;
single_btDynamicsWorld.getPairCache()->setInternalGhostPairCallback(&btGhostPairCallback_);
//v create body1 at (0,0,0)
btBoxShape shape1= btBoxShape(btVector3(1,1,1));
btDefaultMotionState motion1= btDefaultMotionState(btTransform::getIdentity());
btRigidBody body1 = btRigidBody(
btRigidBody::btRigidBodyConstructionInfo(
1, //mass
&motion1,
&shape1,
btVector3(1,1,1) //inertia
)
);
//v create body2 (ghost) at (0,0,0)
btPairCachingGhostObject body2{};
body2.setWorldTransform(btTransform::getIdentity());
btBoxShape shape2 = btBoxShape(btVector3(1,1,1));
body2.setCollisionShape (&shape2);
body2.setCollisionFlags(btCollisionObject::CF_NO_CONTACT_RESPONSE);
//v add both object to world
single_btDynamicsWorld.addCollisionObject(&body1,0xFFFF,0xFFFF);
single_btDynamicsWorld.addCollisionObject(&body2,btBroadphaseProxy::SensorTrigger,btBroadphaseProxy::AllFilter & ~btBroadphaseProxy::SensorTrigger);
// single_btDynamicsWorld.stepSimulation(0.0000001f,1);
for(int t=0;t<=2;t++){
//will move body2 : t=0->(0,0,0) ; t=1->(3,0,0) ; t=2->(0,0,0)
// Thus, I should detect collision at t=0 and t=2 only
btTransform tranNew=btTransform::getIdentity();
if(t==1)tranNew.setOrigin(btVector3(3,0,0));
body2.setWorldTransform(tranNew);
btManifoldArray manifoldArray;
btBroadphasePairArray& pairArray = body2.getOverlappingPairCache()->getOverlappingPairArray();
int numPairs = pairArray.size();
//no different if I call this : single_btDynamicsWorld.getDispatcher()->dispatchAllCollisionPairs(body2.getOverlappingPairCache(), single_btDynamicsWorld.getDispatchInfo(), single_btDynamicsWorld.getDispatcher());
bool myFoundCollision=false;
for (int i=0;i<numPairs;i++){
manifoldArray.clear();
const btBroadphasePair& pair = pairArray[i];
btBroadphasePair* collisionPair =single_btDynamicsWorld.getPairCache()->findPair(pair.m_pProxy0,pair.m_pProxy1);
if (!collisionPair)
continue;
if (collisionPair->m_algorithm)
collisionPair->m_algorithm->getAllContactManifolds(manifoldArray);
for (int j=0;j<manifoldArray.size();j++) {
btPersistentManifold* manifold = manifoldArray[j];
for (int p=0;p<manifold->getNumContacts();p++)
{
const btManifoldPoint&pt = manifold->getContactPoint(p);
myFoundCollision=true;
}
}
}
std::cout<<myFoundCollision<<std::endl; //should print : true false true
}
int afsdds=0;
}
I have read :-
-
https://raw.githubusercontent.com/kripk ... erDemo.cpp
-
http://www.bulletphysics.org/mediawiki- ... hostObject
-
https://pybullet.org/Bullet/phpBB3/viewtopic.php?t=3026