ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

c – 检测与子弹的碰撞

2019-09-02 14:07:26  阅读:197  来源: 互联网

标签:bulletphysics 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有