很少有刚体导致 Bullet Physics 缓慢

Few rigidbody cause Bullet Physics slowly

我目前正在将物理引擎 Bullet Physics 集成到我的图形引擎中,在此之前,我使用 SAP 和 Narrowphase 算法实现了简单的碰撞系统,SAP 和 Narrowphase 的时间成本为 3ms大约有 300 个对象。

由于我的算法存在一些错误,我决定改用真正的物理引擎Bullet Physics。所以我跟着官方文章的教程。当我以为我知道如何在我的图形引擎中实现时,输出屏幕变成了 3 fps。

看来是我理解的问题。所以我做了一个真正简单的例子来重现我遇到的延迟。

btBroadphaseInterface* broadphase = new btDbvtBroadphase();

btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);

btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;

btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);

dynamicsWorld->setGravity(btVector3(0, -10, 0));


btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 1);

btCollisionShape* fallShape = new btSphereShape(1);


btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, -1, 0)));
btRigidBody::btRigidBodyConstructionInfo
    groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0));
btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
dynamicsWorld->addRigidBody(groundRigidBody);


btDefaultMotionState* fallMotionState =
    new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 50, 0)));
btScalar mass = 1;
btVector3 fallInertia(0, 0, 0);
fallShape->calculateLocalInertia(mass, fallInertia);
btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, fallShape, fallInertia);
btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
btRigidBody** fallRigidBodies = new btRigidBody*[300];

for (int i = 0; i < 300; i++)
{
    fallRigidBodies[i] = new btRigidBody(fallRigidBodyCI);
    dynamicsWorld->addRigidBody(fallRigidBodies[i]);
}


for (int i = 0; i < 1000; i++) {
    Debug::StartMeasureNumber(10); // my time measurement function & measurement id
    dynamicsWorld->stepSimulation(1 / 60.f, 10);
    Debug::EndMeasureNumber(10); // this will report the time elapsed.
    btTransform trans;
    fallRigidBody->getMotionState()->getWorldTransform(trans);

    //std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl;
}

dynamicsWorld->removeRigidBody(fallRigidBody);
delete fallRigidBody->getMotionState();
delete fallRigidBody;

dynamicsWorld->removeRigidBody(groundRigidBody);
delete groundRigidBody->getMotionState();
delete groundRigidBody;


delete fallShape;

delete groundShape;


delete dynamicsWorld;
delete solver;
delete collisionConfiguration;
delete dispatcher;
delete broadphase;

在上面的代码中,我只是修改了页面最后的Hello World tutorial。在 stepSimulation 上产生极慢的代码。我所做的不同之处在于将 300 个刚体 添加到 dynamicsWorld。同时提供下面的调试信息。

1138ms,634ms,386ms,297ms,247ms,217ms,211ms,192ms,175ms,163ms,156ms,149ms 147ms,147ms,137ms,137ms,133ms,126ms,128ms,123ms,126ms,127ms,119ms,119ms,115ms 116ms,114ms,114ms,114ms,118ms,120ms,108ms,107ms,107ms,109ms,103ms,105ms,102ms 115ms,106ms,102ms,99ms,99ms,96ms,94ms,93ms,93ms,97ms,94ms,94ms,89ms,90ms,89ms 90ms,90ms,87ms,87ms,84ms,85ms,86ms,92ms,88ms,84ms,85ms,83ms,110ms,86ms,84ms 83ms,85ms,82ms,89ms,80ms,80ms,77ms,76ms,81ms,75ms,78ms,79ms,75ms,77ms,78ms, 76ms,78ms,79ms,75ms,77ms,74ms,74ms,73ms,72ms,78ms,72ms,71ms,72ms,73ms,73ms, 77ms,77ms,71ms,70ms,71ms,68ms,71ms,71ms,73ms,69ms,68ms,67ms,67ms,66ms,68ms 71ms,74ms,66ms,66ms,65ms,65ms,66ms,67ms,64ms,65ms,63ms,66ms,64ms,65ms,63ms 67ms,64ms,63ms,62ms,66ms,63ms,61ms,63ms,62ms,64ms,61ms,63ms,61ms,61ms,64ms 65ms,61ms,63ms,65ms,63ms,62ms,61ms,60ms,61ms,63ms,60ms,61ms,61ms,62ms,60ms, 62ms,65ms,60ms,61ms

第35次循环前极慢,之后暂时稳定在60ms。但这对于图形循环周期来说处理起来也很慢,那么我在 Hello World 教程中哪里理解错了?我需要有人帮助我:(

本题由BDL在评论区解答。 只需构建项目发布版本,0ms 后 stepSimulation 为 运行!