我目前正在将物理引擎 BulletPhysics 集成到我的图形引擎中,在此之前,我使用 SAP 和 Narrowphase 算法实现了简单的碰撞系统,SAP 和 Narrowphase 算法的时间成本为 3ms,大约是300 个对象。
由于我的算法存在一些错误,我决定更换为真正的物理引擎,BulletPhysics。所以我按照官方文章的教程进行操作。当我以为我知道如何在我的图形引擎中实现时,输出屏幕变为 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 上产生极其缓慢的代码。我所做的不同之处是向dynamicsWorld添加300个刚体。另请提供下面的调试信息。
1138毫秒、634毫秒、386毫秒、297毫秒、247毫秒、217毫秒、211毫秒、192毫秒、175毫秒、163毫秒、156毫秒、149毫秒 147毫秒,147毫秒,137毫秒,137毫秒,133毫秒,126毫秒,128毫秒,123毫秒,126毫秒,127毫秒,119毫秒,119毫秒,115毫秒 116毫秒、114毫秒、114毫秒、114毫秒、118毫秒、120毫秒、108毫秒、107毫秒、107毫秒、109毫秒、103毫秒、105毫秒、102毫秒 115毫秒、106毫秒、102毫秒、99毫秒、99毫秒、96毫秒、94毫秒、93毫秒、93毫秒、97毫秒、94毫秒、94毫秒、89毫秒、90毫秒、89毫秒 90ms,90ms,87ms,87ms,84ms,85ms,86ms,92ms,88ms,84ms,85ms,83ms,110ms,86ms,84ms 83毫秒、85毫秒、82毫秒、89毫秒、80毫秒、80毫秒、77毫秒、76毫秒、81毫秒、75毫秒、78毫秒、79毫秒、75毫秒、77毫秒、78毫秒、 76毫秒,78毫秒,79毫秒,75毫秒,77毫秒,74毫秒,74毫秒,73毫秒,72毫秒,78毫秒,72毫秒,71毫秒,72毫秒,73毫秒,73毫秒, 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 67毫秒,64毫秒,63毫秒,62毫秒,66毫秒,63毫秒,61毫秒,63毫秒,62毫秒,64毫秒,61毫秒,63毫秒,61毫秒,61毫秒,64毫秒 65毫秒,61毫秒,63毫秒,65毫秒,63毫秒,62毫秒,61毫秒,60毫秒,61毫秒,63毫秒,60毫秒,61毫秒,61毫秒,62毫秒,60毫秒, 62毫秒、65毫秒、60毫秒、61毫秒
在第35次循环之前,速度非常慢,之后暂时稳定在60毫秒。但这对于图形循环周期来说处理起来也很慢,那么我在 Hello World 教程中理解错在哪里?我需要有人帮助我:(
最佳答案
这个问题是BDL在评论区解决的。
只需将项目构建为发布版本,stepSimulation
即可在 0ms 内运行!
关于c++ - 很少的刚体导致子弹物理缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40574918/