2014-07-25 81 views
-1

我正在玩子彈,我有一個地面和一個球,我希望球落在地面上彈跳。然而,這並沒有發生,即使是非常高的值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; 
+1

只是說:讀代碼真的很痛苦,可以做些什麼來縮進它,並將其更好地分開? –

+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;不再需要。