用子弹检测碰撞

时间:2012-02-02 18:34:57

标签: c++ bulletphysics

我用Ogre3D创建了一个应用程序,已经生成了子弹的对象,我现在需要的只是检测对象之间的碰撞。

我查看了CollisionInterfaceDemo演示,但它并不能满足我的需求。

检测三个球体之间碰撞的必要步骤是什么,只知道它是否发生碰撞(我不关心碰撞点)?

我只知道我可以通过设置其变换来移动CollisionObject。

2 个答案:

答案 0 :(得分:4)

如果你正在使用Bullet,你可以看一些演示来帮助你。

但基本上,这是破旧(大部分内容来自他们的例子):

在你的标题或物理系统的任何地方:

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;
}

这就是它!

重申:

  1. 初始化模拟所需的内容。
  2. 创建一个对象并将其添加到项目符号的“世界”中。
  3. 将每个帧的世界更新一些时间步。
  4. Bullet将为您处理碰撞。如果您需要某种方式在发生冲突时完成功能,我相信您可以将自定义回调分配为将发生的冲突行为。

    希望这有帮助!

答案 1 :(得分:1)

当处理球体之间的碰撞检测时,你需要知道两件事:每个球体半径及其位置。

然后,您必须浏览每个球体并将其与其他球体进行比较。对于每一对,您需要首先找出它们之间的距离。

http://www.purplemath.com/modules/distform.htm

这是基本的2d距离公式,为了使它成为3d所有你需要做的是在平方根生成结果之前将(z2 - z1)平方加到另外2个坐标。

一旦你有了这个距离,只需将2个球体半径加在一起,然后将它们与它们之间的距离进行比较。如果距离小于或等于半径之和,则球体发生碰撞。

我并不是特别熟悉Ogre3D,但是如果你能改变一个对象,你也应该能够把它放在一边。

相关问题