Bridge.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2015 Google Inc. http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "Bridge.h"
  14. #include "btBulletDynamicsCommon.h"
  15. #include "LinearMath/btVector3.h"
  16. #include "LinearMath/btAlignedObjectArray.h"
  17. #include "../CommonInterfaces/CommonRigidBodyBase.h"
  18. const int TOTAL_PLANKS = 10;
  19. struct BridgeExample : public CommonRigidBodyBase
  20. {
  21. BridgeExample(struct GUIHelperInterface* helper)
  22. :CommonRigidBodyBase(helper)
  23. {
  24. }
  25. virtual ~BridgeExample(){}
  26. virtual void initPhysics();
  27. virtual void renderScene();
  28. void resetCamera()
  29. {
  30. float dist = 41;
  31. float pitch = 52;
  32. float yaw = 35;
  33. float targetPos[3]={0,0.46,0};
  34. m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
  35. }
  36. };
  37. void BridgeExample::initPhysics()
  38. {
  39. m_guiHelper->setUpAxis(1);
  40. createEmptyDynamicsWorld();
  41. m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
  42. if (m_dynamicsWorld->getDebugDrawer())
  43. m_dynamicsWorld->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe+btIDebugDraw::DBG_DrawContactPoints);
  44. ///create a few basic rigid bodies
  45. btBoxShape* groundShape = createBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
  46. m_collisionShapes.push_back(groundShape);
  47. btTransform groundTransform;
  48. groundTransform.setIdentity();
  49. groundTransform.setOrigin(btVector3(0,-50,0));
  50. {
  51. btScalar mass(0.);
  52. createRigidBody(mass,groundTransform,groundShape, btVector4(0,0,1,1));
  53. }
  54. //create two fixed boxes to hold the planks
  55. {
  56. //create a few dynamic rigidbodies
  57. // Re-using the same collision is better for memory usage and performance
  58. btScalar plankWidth = 0.4;
  59. btScalar plankHeight = 0.2;
  60. btScalar plankBreadth = 1;
  61. btScalar plankOffset = plankWidth; //distance between two planks
  62. btScalar bridgeWidth = plankWidth*TOTAL_PLANKS + plankOffset*(TOTAL_PLANKS-1);
  63. btScalar bridgeHeight = 5;
  64. btScalar halfBridgeWidth = bridgeWidth*0.5f;
  65. btBoxShape* colShape = createBoxShape(btVector3(plankWidth,plankHeight,plankBreadth));
  66. m_collisionShapes.push_back(colShape);
  67. /// Create Dynamic Objects
  68. btTransform startTransform;
  69. startTransform.setIdentity();
  70. btScalar mass(1.f);
  71. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  72. bool isDynamic = (mass != 0.f);
  73. btVector3 localInertia(0,0,0);
  74. if (isDynamic)
  75. colShape->calculateLocalInertia(mass,localInertia);
  76. //create a set of boxes to represent bridge
  77. btAlignedObjectArray<btRigidBody*> boxes;
  78. int lastBoxIndex = TOTAL_PLANKS-1;
  79. for(int i=0;i<TOTAL_PLANKS;++i) {
  80. float t = float(i)/lastBoxIndex;
  81. t = -(t*2-1.0f) * halfBridgeWidth;
  82. startTransform.setOrigin(btVector3(
  83. btScalar(t),
  84. bridgeHeight,
  85. btScalar(0)
  86. )
  87. );
  88. boxes.push_back(createRigidBody((i==0 || i==lastBoxIndex)?0:mass,startTransform,colShape));
  89. }
  90. //add N-1 spring constraints
  91. for(int i=0;i<TOTAL_PLANKS-1;++i) {
  92. btRigidBody* b1 = boxes[i];
  93. btRigidBody* b2 = boxes[i+1];
  94. btPoint2PointConstraint* leftSpring = new btPoint2PointConstraint(*b1, *b2, btVector3(-0.5,0,-0.5), btVector3(0.5,0,-0.5));
  95. m_dynamicsWorld->addConstraint(leftSpring);
  96. btPoint2PointConstraint* rightSpring = new btPoint2PointConstraint(*b1, *b2, btVector3(-0.5,0,0.5), btVector3(0.5,0,0.5));
  97. m_dynamicsWorld->addConstraint(rightSpring);
  98. }
  99. }
  100. m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
  101. }
  102. void BridgeExample::renderScene()
  103. {
  104. CommonRigidBodyBase::renderScene();
  105. }
  106. CommonExampleInterface* ET_BridgeCreateFunc(CommonExampleOptions& options)
  107. {
  108. return new BridgeExample(options.m_guiHelper);
  109. }