gpt4 book ai didi

c++ - 很少有刚体导致 Bullet Physics 缓慢

转载 作者:搜寻专家 更新时间:2023-10-31 02:13:56 25 4
gpt4 key购买 nike

我目前正在做将物理引擎 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 上产生极慢的代码。我所做的不同之处在于向dynamicsWorld 添加了300 个刚体。同时提供下面的调试信息。

1138ms,634ms,386ms,297ms,247ms,217ms,211ms,192ms,175ms,163ms,156ms,149ms147ms,147ms,137ms,137ms,133ms,126ms,128ms,123ms,126ms,127ms,119ms,119ms,115ms116ms,114ms,114ms,114ms,118ms,120ms,108ms,107ms,107ms,109ms,103ms,105ms,102ms115ms,106ms,102ms,99ms,99ms,96ms,94ms,93ms,93ms,97ms,94ms,94ms,89ms,90ms,89ms90ms,90ms,87ms,87ms,84ms,85ms,86ms,92ms,88ms,84ms,85ms,83ms,110ms,86ms,84ms83ms,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,68ms71ms,74ms,66ms,66ms,65ms,65ms,66ms,67ms,64ms,65ms,63ms,66ms,64ms,65ms,63ms67ms,64ms,63ms,62ms,66ms,63ms,61ms,63ms,62ms,64ms,61ms,63ms,61ms,61ms,64ms65ms,61ms,63ms,65ms,63ms,62ms,61ms,60ms,61ms,63ms,60ms,61ms,61ms,62ms,60ms,62ms,65ms,60ms,61ms

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

最佳答案

本题由BDL在评论区解答。只需将项目构建到发布版本,stepSimulation 即可在 0ms 内运行!

关于c++ - 很少有刚体导致 Bullet Physics 缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40574918/

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