gpt4 book ai didi

c++ - 检测与子弹的碰撞

转载 作者:太空狗 更新时间:2023-10-29 20:46:05 25 4
gpt4 key购买 nike

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

我看了CollisionInterfaceDemo的demo,不太符合我的需求。

检测假设 3 个球体之间的碰撞所需的步骤是什么,只知道它是否发生碰撞(我不关心碰撞点)?

我只知道我可以通过设置变换来移动 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));

每一帧(这通常在更新函数中进行):

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. 按某个时间步每帧更新该世界。

Bullet 将为您处理碰撞。如果您需要某种方式在碰撞发生时完成功能,我相信您可以指定一个自定义回调作为将要发生的碰撞行为。

希望这对您有所帮助!

关于c++ - 检测与子弹的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9117932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com