小编Azt*_*tal的帖子

Bullet物理最简单的碰撞示例

我正在尝试使用Bullet Physics进行碰撞检测.我不需要它为我移动任何对象或使用回调处理渲染.我只想每帧更新对象位置,并用它来告诉我何时发生碰撞.为了得到最简单的例子,我试图找到以btBoxShape为形状的对象之间的碰撞.一切运行良好,没有崩溃或明显的内存泄漏,但我没有碰撞,所以我必须在某处犯错.我会尽量保持这个尽可能简短而不留下任何重要的东西.

这是我的世界设置功能:

collisionConfig      = new btDefaultCollisionConfiguration();
dispatcher           = new btCollisionDispatcher(collisionConfig);
overlappingPairCache = new btDbvtBroadphase();
solver               = new btSequentialImpulseConstraintSolver;
dynamicsWorld        = new btDiscreteDynamicsWorld(dispatcher, 
overlappingPairCache, solver, collisionConfig);         

dynamicsWorld->setGravity(btVector3(0.0f, -9.8f, 0.0f));
Run Code Online (Sandbox Code Playgroud)

现在我有btCollisionObject*类型的玩家和敌人对象.我把它们设置成这样:

mPlayerBox = new btBoxShape(btVector3(1,3,1));
mPlayerObject = new btCollisionObject();
mPlayerObject->setCollisionShape(mPlayerBox);
btTransform playerWorld;
playerWorld.setIdentity();
//playerPos is a D3DXVECTOR3 that holds the camera position.
playerWorld.setOrigin(btVector3(playerPos.x, playerPos.y, playerPos.z));
mPlayerObject->setWorldTransform(playerWorld);
mPlayerObject->forceActivationState(DISABLE_DEACTIVATION);//maybe not needed
dynamicsWorld->addCollisionObject(mPlayerObject);
Run Code Online (Sandbox Code Playgroud)

我对敌人的物体基本上做同样的事情.

然后每一帧我用这样的东西更新我的所有对象:

btTransform updatedWorld;
updatedWorld.setIdentity();
updatedWorld.setOrigin(btVector3(position.x, position.y, position.z));
mPlayerObject->setWorldTransform(updatedWorld);

//do the same for my enemies, and then...

dynamicsWorld->performDiscreteCollisionDetection();
//Also tried doing …
Run Code Online (Sandbox Code Playgroud)

c++ game-physics bulletphysics

9
推荐指数
1
解决办法
2万
查看次数

标签 统计

bulletphysics ×1

c++ ×1

game-physics ×1