【发布时间】:2018-06-10 05:26:41
【问题描述】:
我有一个简单的程序,可以在某些地形上生成球,效果很好。但是尝试添加一个盒子会产生极端的性能下降和一个不会下降的盒子。
这是项目符号代码的完整范围
// create the physics world
auto collisionConfiguration = new btDefaultCollisionConfiguration();
auto dispatcher = new btCollisionDispatcher(collisionConfiguration);
auto broadphase = new btDbvtBroadphase();
auto solver = new btSequentialImpulseConstraintSolver;
auto dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0, 0, -10));
// create terrain
auto terrainIndices = std::vector<int>(terrainModel.faceData.size() / 11);
std::iota(terrainIndices.begin(), terrainIndices.end(), 0);
auto terrainVertices = terrainModel.faceData;
auto terrainMesh = new btTriangleIndexVertexArray(terrainIndices.size() / 3, terrainIndices.data(), 3 * sizeof(int), terrainVertices.size() / 11, terrainVertices.data(), 11 * sizeof(int));
auto terrainShape = new btBvhTriangleMeshShape(terrainMesh, false);
auto terrainMotionState = new btDefaultMotionState();
auto terrainBody = new btRigidBody(0.0, terrainMotionState, terrainShape);
dynamicsWorld->addRigidBody(terrainBody);
// vvvvvvvvvvvvvvvvvvv new code
// create box
auto boxShape = new btBoxShape(btVector3(0.3, 0.3, 0.3));
//auto boxShape = new btSphereShape(0.3); // this works just fine
auto boxMotionState = new btDefaultMotionState(btTransform(btMatrix3x3(), btVector3(0, 0, 5)));
auto boxBody = new btRigidBody(1.0, boxMotionState, boxShape);
dynamicsWorld->addRigidBody(boxBody);
// bind body to renderable
// ^^^^^^^^^^^^^^^^^^^
// create balls (called later on key press)
auto ballShape = new btSphereShape(0.1);
auto spawnBall = [&]() {
auto ballMotionState = new btDefaultMotionState(btTransform(btMatrix3x3(), btVector3(0, 0, 5)));
auto ballBody = new btRigidBody(1.0, ballMotionState, ballShape);
ballBody->setCcdMotionThreshold(0.1);
ballBody->setCcdSweptSphereRadius(0.2);
dynamicsWorld->addRigidBody(ballBody);
// bind body to renderable
};
while(...)
{
// process input
dynamicsWorld->stepSimulation(1.0f / 144.0f);
// render
}
我也尝试过使用具有相同效果的胶囊。需要明确的是,问题在于如果新对象是盒子或胶囊,它似乎不会动态表现,如果它是球体,它会按预期工作。初始位置远高于地形高度。
奇怪的是,性能下降只会在新生成的球下降一段时间后发生。
这是在从 Visual Studio 2017 中以调试模式运行的源代码编译的 Bullet 3 v2.87 上运行的。
【问题讨论】:
标签: c++ bulletphysics