我使用Ogre3D制作了一个应用程序,已经生成了bullet物体,现在我需要做的就是检测物体之间的碰撞。我查看了CollisionInterfaceDemo演示,但它并不符合我的需求。如果要检测3个球之间的碰撞,只需要知道是否会发生碰撞(不关心碰撞点),需要哪些步骤呢?我只知道可以通过设置变换来移动CollisionObject。
btDefaultCollisionConfiguration* mPhysicsConfig;
btCollisionDispatcher* mPhysicsDispatcher;
btBroadphaseInterface* mPhysicsCache;
btSequentialImpulseConstraintSolver* mPhysicsSolver;
btDiscreteDynamicsWorld* mPhysicsWorld;
btAlignedObjectArray<btCollisionShape*> mPhysicsShapes;
第一步(初始化):
///collision configuration contains default setup for memory, collision setup.
mPhysicsConfig = new btDefaultCollisionConfiguration();
///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
mPhysicsDispatcher = new btCollisionDispatcher(mPhysicsConfig);
///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
mPhysicsCache = new btDbvtBroadphase();
///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
mPhysicsSolver = new btSequentialImpulseConstraintSolver;
mPhysicsWorld = new btDiscreteDynamicsWorld(mPhysicsDispatcher,mPhysicsCache,mPhysicsSolver,mPhysicsConfig);
mPhysicsWorld->setGravity(btVector3(0,-9.81f,0));
mPhysicsWorld->stepSimulation( timestep , 10 );
btRigidBody* MyPhysicsSystem::CreateSphere(float sx, float px, float py, float pz, float mass)
{
btCollisionShape* colShape = new btSphereShape(btScalar(sx));
mPhysicsShapes.push_back(colShape);
btTransform startTransform;
startTransform.setIdentity();
btScalar tMass(mass);
//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = (tMass != 0.f);
btVector3 localInertia(0,0,0);
if (isDynamic)
colShape->calculateLocalInertia(tMass,localInertia);
startTransform.setOrigin(btVector3(px,py,pz));
//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(tMass,myMotionState,colShape,localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
mPhysicsWorld->addRigidBody(body);
return body;
}
大致就是这样了!
再次强调:
Bullet会为您处理碰撞。如果您需要在发生碰撞时执行某些功能,我相信您可以将自定义回调分配为碰撞行为。
希望这可以帮到你!
在处理球体之间的碰撞检测时,您需要知道两件事:每个球体的半径和它们的位置。
然后,您必须遍历每个球体并将其与其他球体进行比较。对于每一对,您需要首先找出它们之间的距离。
http://www.purplemath.com/modules/distform.htm
这是基本的二维距离公式,要将其转换为三维,您只需要在平方根之前将(z2-z1)平方添加到其他两个坐标中。
一旦您得到了该距离,只需将两个球体的半径相加并将其与它们之间的距离进行比较。如果距离小于或等于半径之和,则球体已经碰撞。
我不熟悉Ogre3D,但如果您可以转换一个对象,那么您也应该能够获取它的位置。