Pendulum.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2014 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. ///Original author: Erwin Coumans, January 2016
  14. ///Compare the simulation of a pendulum with
  15. #ifdef USE_GTEST
  16. #include <gtest/gtest.h>
  17. #include "pendulum_gold.h"
  18. #endif
  19. #include "../CommonInterfaces/CommonMultiBodyBase.h"
  20. static btScalar radius(0.05);
  21. struct Pendulum : public CommonMultiBodyBase
  22. {
  23. btMultiBody* m_multiBody;
  24. btAlignedObjectArray<btMultiBodyJointFeedback*> m_jointFeedbacks;
  25. public:
  26. Pendulum(struct GUIHelperInterface* helper);
  27. virtual ~Pendulum();
  28. virtual void initPhysics();
  29. virtual void stepSimulation(float deltaTime);
  30. virtual void resetCamera()
  31. {
  32. float dist = 5;
  33. float pitch = 270;
  34. float yaw = 21;
  35. float targetPos[3]={0,0,0};
  36. m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
  37. }
  38. };
  39. Pendulum::Pendulum(struct GUIHelperInterface* helper)
  40. :CommonMultiBodyBase(helper)
  41. {
  42. }
  43. Pendulum::~Pendulum()
  44. {
  45. }
  46. void Pendulum::initPhysics()
  47. {
  48. int upAxis = 1;
  49. m_guiHelper->setUpAxis(upAxis);
  50. this->createEmptyDynamicsWorld();
  51. m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
  52. if (m_dynamicsWorld->getDebugDrawer())
  53. {
  54. m_dynamicsWorld->getDebugDrawer()->setDebugMode(
  55. //btIDebugDraw::DBG_DrawConstraints
  56. +btIDebugDraw::DBG_DrawWireframe
  57. +btIDebugDraw::DBG_DrawContactPoints
  58. +btIDebugDraw::DBG_DrawAabb
  59. );//+btIDebugDraw::DBG_DrawConstraintLimits);
  60. }
  61. {
  62. bool floating = false;
  63. bool damping = false;
  64. bool gyro = false;
  65. int numLinks = 1;
  66. bool canSleep = false;
  67. bool selfCollide = false;
  68. btVector3 linkHalfExtents(0.05, 0.5, 0.1);
  69. btVector3 baseHalfExtents(0.05, 0.5, 0.1);
  70. btVector3 baseInertiaDiag(0.f, 0.f, 0.f);
  71. float baseMass = 0.f;
  72. btMultiBody *pMultiBody = new btMultiBody(numLinks, baseMass, baseInertiaDiag, !floating, canSleep);
  73. //pMultiBody->useRK4Integration(true);
  74. m_multiBody = pMultiBody;
  75. pMultiBody->setBaseWorldTransform(btTransform::getIdentity());
  76. //init the links
  77. btVector3 hingeJointAxis(1, 0, 0);
  78. //y-axis assumed up
  79. btVector3 parentComToCurrentCom(0, -linkHalfExtents[1] , 0);
  80. btVector3 currentPivotToCurrentCom(0, -linkHalfExtents[1], 0);
  81. btVector3 parentComToCurrentPivot = parentComToCurrentCom - currentPivotToCurrentCom;
  82. for(int i = 0; i < numLinks; ++i)
  83. {
  84. float linkMass = 10.f;
  85. btVector3 linkInertiaDiag(0.f, 0.f, 0.f);
  86. btCollisionShape* shape = 0;
  87. {
  88. shape = new btSphereShape(radius);
  89. }
  90. shape->calculateLocalInertia(linkMass, linkInertiaDiag);
  91. delete shape;
  92. pMultiBody->setupRevolute(i, linkMass, linkInertiaDiag, i - 1,
  93. btQuaternion(0.f, 0.f, 0.f, 1.f),
  94. hingeJointAxis,
  95. parentComToCurrentPivot,
  96. currentPivotToCurrentCom, false);
  97. }
  98. pMultiBody->finalizeMultiDof();
  99. btMultiBodyDynamicsWorld* world = m_dynamicsWorld;
  100. world->addMultiBody(pMultiBody);
  101. pMultiBody->setCanSleep(canSleep);
  102. pMultiBody->setHasSelfCollision(selfCollide);
  103. pMultiBody->setUseGyroTerm(gyro);
  104. //
  105. if(!damping)
  106. {
  107. pMultiBody->setLinearDamping(0.f);
  108. pMultiBody->setAngularDamping(0.f);
  109. }else
  110. { pMultiBody->setLinearDamping(0.1f);
  111. pMultiBody->setAngularDamping(0.9f);
  112. }
  113. m_dynamicsWorld->setGravity(btVector3(0,-9.81,0));
  114. for (int i=0; i < pMultiBody->getNumLinks(); ++i)
  115. {
  116. btCollisionShape* shape =new btSphereShape(radius);
  117. m_guiHelper->createCollisionShapeGraphicsObject(shape);
  118. btMultiBodyLinkCollider* col = new btMultiBodyLinkCollider(pMultiBody, i);
  119. col->setCollisionShape(shape);
  120. bool isDynamic = 1;
  121. short collisionFilterGroup = isDynamic? short(btBroadphaseProxy::DefaultFilter) : short(btBroadphaseProxy::StaticFilter);
  122. short collisionFilterMask = isDynamic? short(btBroadphaseProxy::AllFilter) : short(btBroadphaseProxy::AllFilter ^ btBroadphaseProxy::StaticFilter);
  123. world->addCollisionObject(col,collisionFilterGroup,collisionFilterMask);//,2,1+2);
  124. btVector4 color(1,0,0,1);
  125. m_guiHelper->createCollisionObjectGraphicsObject(col,color);
  126. pMultiBody->getLink(i).m_collider=col;
  127. }
  128. btAlignedObjectArray<btQuaternion> scratch_q;
  129. btAlignedObjectArray<btVector3> scratch_m;
  130. pMultiBody->forwardKinematics(scratch_q,scratch_m);
  131. btAlignedObjectArray<btQuaternion> world_to_local;
  132. btAlignedObjectArray<btVector3> local_origin;
  133. pMultiBody->updateCollisionObjectWorldTransforms(world_to_local,local_origin);
  134. }
  135. }
  136. void Pendulum::stepSimulation(float deltaTime)
  137. {
  138. m_multiBody->addJointTorque(0, 20.0);
  139. #ifdef USE_GTEST
  140. m_dynamicsWorld->stepSimulation(1./1000.0,0);
  141. #else
  142. m_dynamicsWorld->stepSimulation(deltaTime);
  143. #endif
  144. btVector3 from = m_multiBody->getBaseWorldTransform().getOrigin();
  145. btVector3 to = m_multiBody->getLink(0).m_collider->getWorldTransform().getOrigin();
  146. btVector4 color(1,0,0,1);
  147. if (m_guiHelper->getRenderInterface())
  148. {
  149. m_guiHelper->getRenderInterface()->drawLine(from,to,color,btScalar(1));
  150. }
  151. }
  152. #ifdef USE_GTEST
  153. TEST(BulletDynamicsTest, pendulum)
  154. {
  155. DummyGUIHelper noGfx;
  156. Pendulum* setup = new Pendulum(&noGfx);
  157. setup->initPhysics();
  158. int numGoldValues = sizeof(sPendulumGold)/sizeof(float);
  159. for (int i=0;i<2000;i++)
  160. {
  161. setup->stepSimulation(0.001);
  162. int index = i*2+1;
  163. ASSERT_LE(index,numGoldValues);
  164. ASSERT_NEAR(setup->m_multiBody->getJointPos(0),sPendulumGold[index],0.005);
  165. }
  166. setup->exitPhysics();
  167. delete setup;
  168. }
  169. int main(int argc, char **argv) {
  170. #if _MSC_VER
  171. _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
  172. //void *testWhetherMemoryLeakDetectionWorks = malloc(1);
  173. #endif
  174. ::testing::InitGoogleTest(&argc, argv);
  175. return RUN_ALL_TESTS();
  176. }
  177. #endif //USE_GTEST
  178. class CommonExampleInterface* TestPendulumCreateFunc(struct CommonExampleOptions& options)
  179. {
  180. return new Pendulum(options.m_guiHelper);
  181. }