我正在玩Bullet,我有一个底层和一个球,我希望球落在地面上反弹。然而,这种情况不会发生,即使m_restitution
的值非常高,这应该可以调节弹性。
任何线索为何会发生这种情况?这让我折磨了好几个小时,没有运气。
btBroadphaseInterface* broadphase = new btDbvtBroadphase();
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0,+9.8,0)); // GLWidget - y axis points downwards !!!
/////////////////////////////////////////////////////////////////////////////
//// Ground ///////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0,0,0)));
btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0,0,0));
groundRigidBodyCI.m_restitution = 1.0f;
groundRigidBodyCI.m_friction = 3.0f;
groundRigidBodyCI.m_rollingFriction = 3.0f;
groundRigidBodyCI.m_mass = 0.0f;
btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
dynamicsWorld->addRigidBody(groundRigidBody);
/////////////////////////////////////////////////////////////////////////////
//// Ball /////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
btDefaultMotionState* ballMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0,-100,0)));
btScalar ballMass = 1;
btVector3 ballInertia;//(0,0,0);
ballShape->calculateLocalInertia(ballMass, ballInertia);
btRigidBody::btRigidBodyConstructionInfo ballRigidBodyCI(ballMass, ballMotionState, ballShape, ballInertia);
groundRigidBodyCI.m_restitution = 1.0f;
ballRigidBodyCI.m_friction = 1.0f;
ballRigidBodyCI.m_rollingFriction = 1.0f;
btRigidBody* ballRigidBody = new btRigidBody(ballRigidBodyCI);
dynamicsWorld->addRigidBody(ballRigidBody);
//Without the next it doesn't bounce at all
//With it, it bounces just a TINY little bit
dynamicsWorld->getSolverInfo().m_splitImpulse = false;
答案 0 :(得分:2)
好的,上面的代码应该在理论上有效,但我不确定它为什么不起作用。
如上所述,调节弹性的属性是m_restitution
。这可以通过两种方式设置。第一种方式在问题帖子中注明。另一种方式如下:
groundRigidBody->setRestitution(1.0);
ballRigidBody->setRestitution(1.0);
......它有效。基于我在网上阅读的内容(例如here),两种方式都应该没问题,但在我的情况下,它只适用于第二种方式。我在Ubuntu上使用bullet-2.82-r2704
。
只有m_restitution
属性才会发生这种情况。所有其他属性都设置为我的问题。
另外,不再需要dynamicsWorld->getSolverInfo().m_splitImpulse = false;
行。