HelloWorld.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2007 Erwin Coumans http://continuousphysics.com/Bullet/
  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. ///-----includes_start-----
  14. #include "btBulletDynamicsCommon.h"
  15. #include <stdio.h>
  16. /// This is a Hello World program for running a basic Bullet physics simulation
  17. int main(int argc, char** argv)
  18. {
  19. ///-----includes_end-----
  20. int i;
  21. ///-----initialization_start-----
  22. ///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
  23. btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
  24. ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
  25. btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
  26. ///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
  27. btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();
  28. ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
  29. btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
  30. btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);
  31. dynamicsWorld->setGravity(btVector3(0,-10,0));
  32. ///-----initialization_end-----
  33. //keep track of the shapes, we release memory at exit.
  34. //make sure to re-use collision shapes among rigid bodies whenever possible!
  35. btAlignedObjectArray<btCollisionShape*> collisionShapes;
  36. ///create a few basic rigid bodies
  37. //the ground is a cube of side 100 at position y = -56.
  38. //the sphere will hit it at y = -6, with center at -5
  39. {
  40. btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
  41. collisionShapes.push_back(groundShape);
  42. btTransform groundTransform;
  43. groundTransform.setIdentity();
  44. groundTransform.setOrigin(btVector3(0,-56,0));
  45. btScalar mass(0.);
  46. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  47. bool isDynamic = (mass != 0.f);
  48. btVector3 localInertia(0,0,0);
  49. if (isDynamic)
  50. groundShape->calculateLocalInertia(mass,localInertia);
  51. //using motionstate is optional, it provides interpolation capabilities, and only synchronizes 'active' objects
  52. btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
  53. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
  54. btRigidBody* body = new btRigidBody(rbInfo);
  55. //add the body to the dynamics world
  56. dynamicsWorld->addRigidBody(body);
  57. }
  58. {
  59. //create a dynamic rigidbody
  60. //btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
  61. btCollisionShape* colShape = new btSphereShape(btScalar(1.));
  62. collisionShapes.push_back(colShape);
  63. /// Create Dynamic Objects
  64. btTransform startTransform;
  65. startTransform.setIdentity();
  66. btScalar mass(1.f);
  67. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  68. bool isDynamic = (mass != 0.f);
  69. btVector3 localInertia(0,0,0);
  70. if (isDynamic)
  71. colShape->calculateLocalInertia(mass,localInertia);
  72. startTransform.setOrigin(btVector3(2,10,0));
  73. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  74. btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
  75. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
  76. btRigidBody* body = new btRigidBody(rbInfo);
  77. dynamicsWorld->addRigidBody(body);
  78. }
  79. /// Do some simulation
  80. ///-----stepsimulation_start-----
  81. for (i=0;i<150;i++)
  82. {
  83. dynamicsWorld->stepSimulation(1.f/60.f,10);
  84. //print positions of all objects
  85. for (int j=dynamicsWorld->getNumCollisionObjects()-1; j>=0 ;j--)
  86. {
  87. btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[j];
  88. btRigidBody* body = btRigidBody::upcast(obj);
  89. btTransform trans;
  90. if (body && body->getMotionState())
  91. {
  92. body->getMotionState()->getWorldTransform(trans);
  93. } else
  94. {
  95. trans = obj->getWorldTransform();
  96. }
  97. printf("world pos object %d = %f,%f,%f\n",j,float(trans.getOrigin().getX()),float(trans.getOrigin().getY()),float(trans.getOrigin().getZ()));
  98. }
  99. }
  100. ///-----stepsimulation_end-----
  101. //cleanup in the reverse order of creation/initialization
  102. ///-----cleanup_start-----
  103. //remove the rigidbodies from the dynamics world and delete them
  104. for (i=dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
  105. {
  106. btCollisionObject* obj = dynamicsWorld->getCollisionObjectArray()[i];
  107. btRigidBody* body = btRigidBody::upcast(obj);
  108. if (body && body->getMotionState())
  109. {
  110. delete body->getMotionState();
  111. }
  112. dynamicsWorld->removeCollisionObject( obj );
  113. delete obj;
  114. }
  115. //delete collision shapes
  116. for (int j=0;j<collisionShapes.size();j++)
  117. {
  118. btCollisionShape* shape = collisionShapes[j];
  119. collisionShapes[j] = 0;
  120. delete shape;
  121. }
  122. //delete dynamics world
  123. delete dynamicsWorld;
  124. //delete solver
  125. delete solver;
  126. //delete broadphase
  127. delete overlappingPairCache;
  128. //delete dispatcher
  129. delete dispatcher;
  130. delete collisionConfiguration;
  131. //next line is optional: it will be cleared by the destructor when the array goes out of scope
  132. collisionShapes.clear();
  133. }