GyroscopicSetup.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "GyroscopicSetup.h"
  2. #include "../CommonInterfaces/CommonRigidBodyBase.h"
  3. struct GyroscopicSetup : public CommonRigidBodyBase
  4. {
  5. GyroscopicSetup(struct GUIHelperInterface* helper);
  6. virtual ~GyroscopicSetup()
  7. {
  8. }
  9. virtual void initPhysics();
  10. virtual void physicsDebugDraw(int debugFlags);
  11. void resetCamera()
  12. {
  13. float dist = 20;
  14. float pitch = 180;
  15. float yaw = 16;
  16. float targetPos[3]={-2.4,0.4,-0.24};
  17. m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
  18. }
  19. };
  20. static int gyroflags[4] = {
  21. 0,//none, no gyroscopic term
  22. BT_ENABLE_GYROSCOPIC_FORCE_EXPLICIT,
  23. BT_ENABLE_GYROSCOPIC_FORCE_IMPLICIT_WORLD,
  24. BT_ENABLE_GYROSCOPIC_FORCE_IMPLICIT_BODY
  25. };
  26. static const char* gyroNames[4] = {
  27. "No Gyroscopic",
  28. "Explicit",
  29. "Implicit (World)",
  30. "Implicit (Body)"
  31. };
  32. GyroscopicSetup::GyroscopicSetup(struct GUIHelperInterface* helper)
  33. :CommonRigidBodyBase(helper)
  34. {
  35. }
  36. void GyroscopicSetup::initPhysics()
  37. {
  38. m_guiHelper->setUpAxis(1);
  39. createEmptyDynamicsWorld();
  40. m_dynamicsWorld->setGravity(btVector3(0, 0, 0));
  41. m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
  42. btVector3 positions[4] = {
  43. btVector3( -10, 8,4),
  44. btVector3( -5, 8,4),
  45. btVector3( 0, 8,4),
  46. btVector3( 5, 8,4),
  47. };
  48. for (int i = 0; i<4; i++)
  49. {
  50. btCylinderShapeZ* pin = new btCylinderShapeZ(btVector3(0.1,0.1, 0.2));
  51. btBoxShape* box = new btBoxShape(btVector3(1,0.1,0.1));
  52. box->setMargin(0.01);
  53. pin->setMargin(0.01);
  54. btCompoundShape* compound = new btCompoundShape();
  55. compound->addChildShape(btTransform::getIdentity(), pin);
  56. btTransform offsetBox(btMatrix3x3::getIdentity(),btVector3(0,0,0.2));
  57. compound->addChildShape(offsetBox, box);
  58. btScalar masses[2] = {0.3,0.1};
  59. btVector3 localInertia;
  60. btTransform principal;
  61. compound->calculatePrincipalAxisTransform(masses,principal,localInertia);
  62. btRigidBody* body = new btRigidBody(1, 0, compound, localInertia);
  63. btTransform tr;
  64. tr.setIdentity();
  65. tr.setOrigin(positions[i]);
  66. body->setCenterOfMassTransform(tr);
  67. body->setAngularVelocity(btVector3(0, 0.1, 10));//51));
  68. //body->setLinearVelocity(btVector3(3, 0, 0));
  69. body->setFriction(btSqrt(1));
  70. m_dynamicsWorld->addRigidBody(body);
  71. body->setFlags(gyroflags[i]);
  72. m_dynamicsWorld->getSolverInfo().m_maxGyroscopicForce = 10.f;
  73. body->setDamping(0.0000f, 0.000f);
  74. }
  75. {
  76. //btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(0.5)));
  77. btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 0);
  78. m_collisionShapes.push_back(groundShape);
  79. btTransform groundTransform;
  80. groundTransform.setIdentity();
  81. groundTransform.setOrigin(btVector3(0, 0, 0));
  82. btRigidBody* groundBody;
  83. groundBody = createRigidBody(0, groundTransform, groundShape);
  84. groundBody->setFriction(btSqrt(2));
  85. }
  86. m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
  87. }
  88. void GyroscopicSetup::physicsDebugDraw(int debugFlags)
  89. {
  90. CommonRigidBodyBase::physicsDebugDraw(debugFlags);
  91. //render method names above objects
  92. for (int i=0;i<m_dynamicsWorld->getNumCollisionObjects();i++)
  93. {
  94. btRigidBody* body = btRigidBody::upcast(m_dynamicsWorld->getCollisionObjectArray()[i]);
  95. if (body && body->getInvMass()>0)
  96. {
  97. btTransform tr = body->getWorldTransform();
  98. btVector3 pos = tr.getOrigin()+btVector3(0,0,2);
  99. btScalar size=1;
  100. m_guiHelper->drawText3D(gyroNames[i],pos.x(),pos.y(),pos.z(),size);
  101. }
  102. }
  103. }
  104. class CommonExampleInterface* GyroscopicCreateFunc(CommonExampleOptions& options)
  105. {
  106. return new GyroscopicSetup(options.m_guiHelper);
  107. }