其他分享
首页 > 其他分享> > c – 检测与子弹的碰撞

c – 检测与子弹的碰撞

作者:互联网

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

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

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

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

解决方法:

如果您正在使用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));

每个帧(这通常在Update函数中):

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://bulletphysics.com/Bullet/BulletFull/
>一个完整的纲要
检测并响应与子弹的碰撞:
http://www.bulletphysics.org/mediawiki-1.5.8/index.php?title=Collision_Callbacks_and_Triggers

希望这可以帮助!

标签:bulletphysics,c
来源: https://codeday.me/bug/20190902/1791465.html