目录

bullet碰撞检测

调用方法

别听网上的乱分析。bullet一共有4种碰撞检测,分别是单体检测,全体检测,射线检测,扫描检测

单体检测

检测与一个物体碰撞的所有物体

全体检测

检测碰撞世界中发生的所有碰撞

射线检测

检测与一根线碰撞的所有物体

扫描检测

假定一个物体向前移动一段距离,检测路上发生的碰撞

底层原理

粗碰撞阶段

btVector3 aabbMin,aabbMax;
colObj->getCollisionShape()->getAabb(colObj->getWorldTransform(),aabbMin,aabbMax);
btSingleContactCallback	contactCB(colObj,this,resultCallback);

m_broadphasePairCache->aabbTest(aabbMin,aabbMax,contactCB);//底层是层次包围盒

精确碰撞阶段

virtual bool	process(const btBroadphaseProxy* proxy)
{
	btCollisionObject*	collisionObject = (btCollisionObject*)proxy->m_clientObject;
	if (collisionObject == m_collisionObject)
		return true;
  
	//only perform raycast if filterMask matches
	if(m_resultCallback.needsCollision(collisionObject->getBroadphaseHandle())) 
	{
		btCollisionObjectWrapper ob0(0,m_collisionObject->getCollisionShape(),m_collisionObject,m_collisionObject->getWorldTransform(),-1,-1);
		btCollisionObjectWrapper ob1(0,collisionObject->getCollisionShape(),collisionObject,collisionObject->getWorldTransform(),-1,-1);//获取形状
  
		btCollisionAlgorithm* algorithm = m_world->getDispatcher()->findAlgorithm(&ob0,&ob1,0, BT_CLOSEST_POINT_ALGORITHMS);//查找针对这两个形状的精确碰撞接口
		if (algorithm)
		{
			btBridgedManifoldResult contactPointResult(&ob0,&ob1, m_resultCallback);
			//discrete collision detection query
			
			algorithm->processCollision(&ob0,&ob1, m_world->getDispatchInfo(),&contactPointResult);//调用接口
  
			algorithm->~btCollisionAlgorithm();
			m_world->getDispatcher()->freeCollisionAlgorithm(algorithm);
		}
	}
	return true;
}