BasicExample.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "BasicExample.h"
  14. #include "btBulletDynamicsCommon.h"
  15. #define ARRAY_SIZE_Y 5
  16. #define ARRAY_SIZE_X 5
  17. #define ARRAY_SIZE_Z 5
  18. #include "LinearMath/btVector3.h"
  19. #include "LinearMath/btAlignedObjectArray.h"
  20. #include "../CommonInterfaces/CommonRigidBodyBase.h"
  21. struct BasicExample : public CommonRigidBodyBase
  22. {
  23. BasicExample(struct GUIHelperInterface* helper)
  24. :CommonRigidBodyBase(helper)
  25. {
  26. }
  27. virtual ~BasicExample(){}
  28. virtual void initPhysics();
  29. virtual void renderScene();
  30. void resetCamera()
  31. {
  32. float dist = 4;
  33. float pitch = 52;
  34. float yaw = 35;
  35. float targetPos[3]={0,0,0};
  36. m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
  37. }
  38. };
  39. void BasicExample::initPhysics()
  40. {
  41. m_guiHelper->setUpAxis(1);
  42. createEmptyDynamicsWorld();
  43. //m_dynamicsWorld->setGravity(btVector3(0,0,0));
  44. m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
  45. if (m_dynamicsWorld->getDebugDrawer())
  46. m_dynamicsWorld->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe+btIDebugDraw::DBG_DrawContactPoints);
  47. ///create a few basic rigid bodies
  48. btBoxShape* groundShape = createBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
  49. //groundShape->initializePolyhedralFeatures();
  50. //btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50);
  51. m_collisionShapes.push_back(groundShape);
  52. btTransform groundTransform;
  53. groundTransform.setIdentity();
  54. groundTransform.setOrigin(btVector3(0,-50,0));
  55. {
  56. btScalar mass(0.);
  57. createRigidBody(mass,groundTransform,groundShape, btVector4(0,0,1,1));
  58. }
  59. {
  60. //create a few dynamic rigidbodies
  61. // Re-using the same collision is better for memory usage and performance
  62. btBoxShape* colShape = createBoxShape(btVector3(.1,.1,.1));
  63. //btCollisionShape* colShape = new btSphereShape(btScalar(1.));
  64. m_collisionShapes.push_back(colShape);
  65. /// Create Dynamic Objects
  66. btTransform startTransform;
  67. startTransform.setIdentity();
  68. btScalar mass(1.f);
  69. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  70. bool isDynamic = (mass != 0.f);
  71. btVector3 localInertia(0,0,0);
  72. if (isDynamic)
  73. colShape->calculateLocalInertia(mass,localInertia);
  74. for (int k=0;k<ARRAY_SIZE_Y;k++)
  75. {
  76. for (int i=0;i<ARRAY_SIZE_X;i++)
  77. {
  78. for(int j = 0;j<ARRAY_SIZE_Z;j++)
  79. {
  80. startTransform.setOrigin(btVector3(
  81. btScalar(0.2*i),
  82. btScalar(2+.2*k),
  83. btScalar(0.2*j)));
  84. createRigidBody(mass,startTransform,colShape);
  85. }
  86. }
  87. }
  88. }
  89. m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
  90. }
  91. void BasicExample::renderScene()
  92. {
  93. CommonRigidBodyBase::renderScene();
  94. }
  95. CommonExampleInterface* BasicExampleCreateFunc(CommonExampleOptions& options)
  96. {
  97. return new BasicExample(options.m_guiHelper);
  98. }
  99. B3_STANDALONE_EXAMPLE(BasicExampleCreateFunc)