MultiDofDemo.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #include "MultiDofDemo.h"
  2. #include "../OpenGLWindow/SimpleOpenGL3App.h"
  3. #include "btBulletDynamicsCommon.h"
  4. #include "BulletDynamics/Featherstone/btMultiBody.h"
  5. #include "BulletDynamics/Featherstone/btMultiBodyConstraintSolver.h"
  6. #include "BulletDynamics/Featherstone/btMultiBodyDynamicsWorld.h"
  7. #include "BulletDynamics/Featherstone/btMultiBodyLinkCollider.h"
  8. #include "BulletDynamics/Featherstone/btMultiBodyLink.h"
  9. #include "BulletDynamics/Featherstone/btMultiBodyJointLimitConstraint.h"
  10. #include "BulletDynamics/Featherstone/btMultiBodyJointMotor.h"
  11. #include "BulletDynamics/Featherstone/btMultiBodyPoint2Point.h"
  12. #include "../OpenGLWindow/GLInstancingRenderer.h"
  13. #include "BulletCollision/CollisionShapes/btShapeHull.h"
  14. #include "../CommonInterfaces/CommonMultiBodyBase.h"
  15. class MultiDofDemo : public CommonMultiBodyBase
  16. {
  17. public:
  18. MultiDofDemo(GUIHelperInterface* helper);
  19. virtual ~MultiDofDemo();
  20. virtual void initPhysics();
  21. virtual void stepSimulation(float deltaTime);
  22. virtual void resetCamera()
  23. {
  24. float dist = 1;
  25. float pitch = 50;
  26. float yaw = 35;
  27. float targetPos[3]={-3,2.8,-2.5};
  28. m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
  29. }
  30. btMultiBody* createFeatherstoneMultiBody_testMultiDof(class btMultiBodyDynamicsWorld* world, int numLinks, const btVector3& basePosition, const btVector3 &baseHalfExtents, const btVector3 &linkHalfExtents, bool spherical = false, bool floating = false);
  31. void addColliders_testMultiDof(btMultiBody *pMultiBody, btMultiBodyDynamicsWorld *pWorld, const btVector3 &baseHalfExtents, const btVector3 &linkHalfExtents);
  32. void addBoxes_testMultiDof();
  33. };
  34. static bool g_floatingBase = false;
  35. static bool g_firstInit = true;
  36. static float scaling = 0.4f;
  37. static float friction = 1.;
  38. #define ARRAY_SIZE_X 5
  39. #define ARRAY_SIZE_Y 5
  40. #define ARRAY_SIZE_Z 5
  41. //maximum number of objects (and allow user to shoot additional boxes)
  42. #define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024)
  43. #define START_POS_X -5
  44. //#define START_POS_Y 12
  45. #define START_POS_Y 2
  46. #define START_POS_Z -3
  47. MultiDofDemo::MultiDofDemo(GUIHelperInterface* helper)
  48. :CommonMultiBodyBase(helper)
  49. {
  50. m_guiHelper->setUpAxis(1);
  51. }
  52. MultiDofDemo::~MultiDofDemo()
  53. {
  54. }
  55. void MultiDofDemo::stepSimulation(float deltaTime)
  56. {
  57. //use a smaller internal timestep, there are stability issues
  58. float internalTimeStep = 1./240.f;
  59. m_dynamicsWorld->stepSimulation(deltaTime,10,internalTimeStep);
  60. }
  61. void MultiDofDemo::initPhysics()
  62. {
  63. m_guiHelper->setUpAxis(1);
  64. if(g_firstInit)
  65. {
  66. m_guiHelper->getRenderInterface()->getActiveCamera()->setCameraDistance(btScalar(10.*scaling));
  67. m_guiHelper->getRenderInterface()->getActiveCamera()->setCameraPitch(50);
  68. g_firstInit = false;
  69. }
  70. ///collision configuration contains default setup for memory, collision setup
  71. m_collisionConfiguration = new btDefaultCollisionConfiguration();
  72. ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
  73. m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
  74. m_broadphase = new btDbvtBroadphase();
  75. //Use the btMultiBodyConstraintSolver for Featherstone btMultiBody support
  76. btMultiBodyConstraintSolver* sol = new btMultiBodyConstraintSolver;
  77. m_solver = sol;
  78. //use btMultiBodyDynamicsWorld for Featherstone btMultiBody support
  79. btMultiBodyDynamicsWorld* world = new btMultiBodyDynamicsWorld(m_dispatcher,m_broadphase,sol,m_collisionConfiguration);
  80. m_dynamicsWorld = world;
  81. // m_dynamicsWorld->setDebugDrawer(&gDebugDraw);
  82. m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
  83. m_dynamicsWorld->setGravity(btVector3(0,-10,0));
  84. ///create a few basic rigid bodies
  85. btVector3 groundHalfExtents(50,50,50);
  86. btCollisionShape* groundShape = new btBoxShape(groundHalfExtents);
  87. //groundShape->initializePolyhedralFeatures();
  88. // btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50);
  89. m_collisionShapes.push_back(groundShape);
  90. btTransform groundTransform;
  91. groundTransform.setIdentity();
  92. groundTransform.setOrigin(btVector3(0,-50,00));
  93. /////////////////////////////////////////////////////////////////
  94. /////////////////////////////////////////////////////////////////
  95. bool damping = true;
  96. bool gyro = true;
  97. int numLinks = 5;
  98. bool spherical = true; //set it ot false -to use 1DoF hinges instead of 3DoF sphericals
  99. bool multibodyOnly = false;
  100. bool canSleep = true;
  101. bool selfCollide = false;
  102. btVector3 linkHalfExtents(0.05, 0.37, 0.1);
  103. btVector3 baseHalfExtents(0.05, 0.37, 0.1);
  104. btMultiBody* mbC = createFeatherstoneMultiBody_testMultiDof(world, numLinks, btVector3(-0.4f, 3.f, 0.f), linkHalfExtents, baseHalfExtents, spherical, g_floatingBase);
  105. //mbC->forceMultiDof(); //if !spherical, you can comment this line to check the 1DoF algorithm
  106. g_floatingBase = ! g_floatingBase;
  107. mbC->setCanSleep(canSleep);
  108. mbC->setHasSelfCollision(selfCollide);
  109. mbC->setUseGyroTerm(gyro);
  110. //
  111. if(!damping)
  112. {
  113. mbC->setLinearDamping(0.f);
  114. mbC->setAngularDamping(0.f);
  115. }else
  116. { mbC->setLinearDamping(0.1f);
  117. mbC->setAngularDamping(0.9f);
  118. }
  119. //
  120. m_dynamicsWorld->setGravity(btVector3(0, -9.81 ,0));
  121. //////////////////////////////////////////////
  122. if(numLinks > 0)
  123. {
  124. btScalar q0 = 45.f * SIMD_PI/ 180.f;
  125. if(!spherical)
  126. {
  127. mbC->setJointPosMultiDof(0, &q0);
  128. }
  129. else
  130. {
  131. btQuaternion quat0(btVector3(1, 1, 0).normalized(), q0);
  132. quat0.normalize();
  133. mbC->setJointPosMultiDof(0, quat0);
  134. }
  135. }
  136. ///
  137. addColliders_testMultiDof(mbC, world, baseHalfExtents, linkHalfExtents);
  138. /////////////////////////////////////////////////////////////////
  139. btScalar groundHeight = -51.55;
  140. if (!multibodyOnly)
  141. {
  142. btScalar mass(0.);
  143. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  144. bool isDynamic = (mass != 0.f);
  145. btVector3 localInertia(0,0,0);
  146. if (isDynamic)
  147. groundShape->calculateLocalInertia(mass,localInertia);
  148. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  149. groundTransform.setIdentity();
  150. groundTransform.setOrigin(btVector3(0,groundHeight,0));
  151. btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
  152. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
  153. btRigidBody* body = new btRigidBody(rbInfo);
  154. //add the body to the dynamics world
  155. m_dynamicsWorld->addRigidBody(body,1,1+2);//,1,1+2);
  156. }
  157. /////////////////////////////////////////////////////////////////
  158. if(!multibodyOnly)
  159. {
  160. btVector3 halfExtents(.5,.5,.5);
  161. btBoxShape* colShape = new btBoxShape(halfExtents);
  162. //btCollisionShape* colShape = new btSphereShape(btScalar(1.));
  163. m_collisionShapes.push_back(colShape);
  164. /// Create Dynamic Objects
  165. btTransform startTransform;
  166. startTransform.setIdentity();
  167. btScalar mass(1.f);
  168. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  169. bool isDynamic = (mass != 0.f);
  170. btVector3 localInertia(0,0,0);
  171. if (isDynamic)
  172. colShape->calculateLocalInertia(mass,localInertia);
  173. startTransform.setOrigin(btVector3(
  174. btScalar(0.0),
  175. -0.95,
  176. btScalar(0.0)));
  177. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  178. btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
  179. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
  180. btRigidBody* body = new btRigidBody(rbInfo);
  181. m_dynamicsWorld->addRigidBody(body);//,1,1+2);
  182. }
  183. m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
  184. /////////////////////////////////////////////////////////////////
  185. }
  186. btMultiBody* MultiDofDemo::createFeatherstoneMultiBody_testMultiDof(btMultiBodyDynamicsWorld *pWorld, int numLinks, const btVector3 &basePosition, const btVector3 &baseHalfExtents, const btVector3 &linkHalfExtents, bool spherical, bool floating)
  187. {
  188. //init the base
  189. btVector3 baseInertiaDiag(0.f, 0.f, 0.f);
  190. float baseMass = 1.f;
  191. if(baseMass)
  192. {
  193. btCollisionShape *pTempBox = new btBoxShape(btVector3(baseHalfExtents[0], baseHalfExtents[1], baseHalfExtents[2]));
  194. pTempBox->calculateLocalInertia(baseMass, baseInertiaDiag);
  195. delete pTempBox;
  196. }
  197. bool canSleep = false;
  198. btMultiBody *pMultiBody = new btMultiBody(numLinks, baseMass, baseInertiaDiag, !floating, canSleep);
  199. btQuaternion baseOriQuat(0.f, 0.f, 0.f, 1.f);
  200. pMultiBody->setBasePos(basePosition);
  201. pMultiBody->setWorldToBaseRot(baseOriQuat);
  202. btVector3 vel(0, 0, 0);
  203. // pMultiBody->setBaseVel(vel);
  204. //init the links
  205. btVector3 hingeJointAxis(1, 0, 0);
  206. float linkMass = 1.f;
  207. btVector3 linkInertiaDiag(0.f, 0.f, 0.f);
  208. btCollisionShape *pTempBox = new btBoxShape(btVector3(linkHalfExtents[0], linkHalfExtents[1], linkHalfExtents[2]));
  209. pTempBox->calculateLocalInertia(linkMass, linkInertiaDiag);
  210. delete pTempBox;
  211. //y-axis assumed up
  212. btVector3 parentComToCurrentCom(0, -linkHalfExtents[1] * 2.f, 0); //par body's COM to cur body's COM offset
  213. btVector3 currentPivotToCurrentCom(0, -linkHalfExtents[1], 0); //cur body's COM to cur body's PIV offset
  214. btVector3 parentComToCurrentPivot = parentComToCurrentCom - currentPivotToCurrentCom; //par body's COM to cur body's PIV offset
  215. //////
  216. btScalar q0 = 0.f * SIMD_PI/ 180.f;
  217. btQuaternion quat0(btVector3(0, 1, 0).normalized(), q0);
  218. quat0.normalize();
  219. /////
  220. for(int i = 0; i < numLinks; ++i)
  221. {
  222. if(!spherical)
  223. pMultiBody->setupRevolute(i, linkMass, linkInertiaDiag, i - 1, btQuaternion(0.f, 0.f, 0.f, 1.f), hingeJointAxis, parentComToCurrentPivot, currentPivotToCurrentCom, false);
  224. else
  225. //pMultiBody->setupPlanar(i, linkMass, linkInertiaDiag, i - 1, btQuaternion(0.f, 0.f, 0.f, 1.f)/*quat0*/, btVector3(1, 0, 0), parentComToCurrentPivot*2, false);
  226. pMultiBody->setupSpherical(i, linkMass, linkInertiaDiag, i - 1, btQuaternion(0.f, 0.f, 0.f, 1.f), parentComToCurrentPivot, currentPivotToCurrentCom, false);
  227. }
  228. pMultiBody->finalizeMultiDof();
  229. ///
  230. pWorld->addMultiBody(pMultiBody);
  231. ///
  232. return pMultiBody;
  233. }
  234. void MultiDofDemo::addColliders_testMultiDof(btMultiBody *pMultiBody, btMultiBodyDynamicsWorld *pWorld, const btVector3 &baseHalfExtents, const btVector3 &linkHalfExtents)
  235. {
  236. btAlignedObjectArray<btQuaternion> world_to_local;
  237. world_to_local.resize(pMultiBody->getNumLinks() + 1);
  238. btAlignedObjectArray<btVector3> local_origin;
  239. local_origin.resize(pMultiBody->getNumLinks() + 1);
  240. world_to_local[0] = pMultiBody->getWorldToBaseRot();
  241. local_origin[0] = pMultiBody->getBasePos();
  242. {
  243. // float pos[4]={local_origin[0].x(),local_origin[0].y(),local_origin[0].z(),1};
  244. btScalar quat[4]={-world_to_local[0].x(),-world_to_local[0].y(),-world_to_local[0].z(),world_to_local[0].w()};
  245. if (1)
  246. {
  247. btCollisionShape* box = new btBoxShape(baseHalfExtents);
  248. btMultiBodyLinkCollider* col= new btMultiBodyLinkCollider(pMultiBody, -1);
  249. col->setCollisionShape(box);
  250. btTransform tr;
  251. tr.setIdentity();
  252. tr.setOrigin(local_origin[0]);
  253. tr.setRotation(btQuaternion(quat[0],quat[1],quat[2],quat[3]));
  254. col->setWorldTransform(tr);
  255. pWorld->addCollisionObject(col, 2,1+2);
  256. col->setFriction(friction);
  257. pMultiBody->setBaseCollider(col);
  258. }
  259. }
  260. for (int i=0; i < pMultiBody->getNumLinks(); ++i)
  261. {
  262. const int parent = pMultiBody->getParent(i);
  263. world_to_local[i+1] = pMultiBody->getParentToLocalRot(i) * world_to_local[parent+1];
  264. local_origin[i+1] = local_origin[parent+1] + (quatRotate(world_to_local[i+1].inverse() , pMultiBody->getRVector(i)));
  265. }
  266. for (int i=0; i < pMultiBody->getNumLinks(); ++i)
  267. {
  268. btVector3 posr = local_origin[i+1];
  269. // float pos[4]={posr.x(),posr.y(),posr.z(),1};
  270. btScalar quat[4]={-world_to_local[i+1].x(),-world_to_local[i+1].y(),-world_to_local[i+1].z(),world_to_local[i+1].w()};
  271. btCollisionShape* box = new btBoxShape(linkHalfExtents);
  272. btMultiBodyLinkCollider* col = new btMultiBodyLinkCollider(pMultiBody, i);
  273. col->setCollisionShape(box);
  274. btTransform tr;
  275. tr.setIdentity();
  276. tr.setOrigin(posr);
  277. tr.setRotation(btQuaternion(quat[0],quat[1],quat[2],quat[3]));
  278. col->setWorldTransform(tr);
  279. col->setFriction(friction);
  280. pWorld->addCollisionObject(col,2,1+2);
  281. pMultiBody->getLink(i).m_collider=col;
  282. }
  283. }
  284. void MultiDofDemo::addBoxes_testMultiDof()
  285. {
  286. //create a few dynamic rigidbodies
  287. // Re-using the same collision is better for memory usage and performance
  288. btBoxShape* colShape = new btBoxShape(btVector3(1,1,1));
  289. //btCollisionShape* colShape = new btSphereShape(btScalar(1.));
  290. m_collisionShapes.push_back(colShape);
  291. /// Create Dynamic Objects
  292. btTransform startTransform;
  293. startTransform.setIdentity();
  294. btScalar mass(1.f);
  295. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  296. bool isDynamic = (mass != 0.f);
  297. btVector3 localInertia(0,0,0);
  298. if (isDynamic)
  299. colShape->calculateLocalInertia(mass,localInertia);
  300. float start_x = START_POS_X - ARRAY_SIZE_X/2;
  301. float start_y = START_POS_Y;
  302. float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
  303. for (int k=0;k<ARRAY_SIZE_Y;k++)
  304. {
  305. for (int i=0;i<ARRAY_SIZE_X;i++)
  306. {
  307. for(int j = 0;j<ARRAY_SIZE_Z;j++)
  308. {
  309. startTransform.setOrigin(btVector3(
  310. btScalar(3.0*i + start_x),
  311. btScalar(3.0*k + start_y),
  312. btScalar(3.0*j + start_z)));
  313. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  314. btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
  315. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
  316. btRigidBody* body = new btRigidBody(rbInfo);
  317. m_dynamicsWorld->addRigidBody(body);//,1,1+2);
  318. }
  319. }
  320. }
  321. }
  322. class CommonExampleInterface* MultiDofCreateFunc(struct CommonExampleOptions& options)
  323. {
  324. return new MultiDofDemo(options.m_guiHelper);
  325. }