MultiThreadedDemo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans https://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 "MultiThreadedDemo.h"
  14. #include "CommonRigidBodyMTBase.h"
  15. #include "../CommonInterfaces/CommonParameterInterface.h"
  16. #include "btBulletDynamicsCommon.h"
  17. #include "btBulletCollisionCommon.h"
  18. #include "LinearMath/btAlignedObjectArray.h"
  19. #include <stdio.h> //printf debugging
  20. #include <algorithm>
  21. #include <cmath>
  22. static btScalar gSliderStackRows = 1.0f;
  23. static btScalar gSliderStackColumns = 1.0f;
  24. static btScalar gSliderStackHeight = 15.0f;
  25. static btScalar gSliderStackWidth = 8.0f;
  26. static btScalar gSliderGroundHorizontalAmplitude = 0.0f;
  27. static btScalar gSliderGroundVerticalAmplitude = 0.0f;
  28. static btScalar gSliderGroundTilt = 0.0f;
  29. static btScalar gSliderRollingFriction = 0.0f;
  30. static bool gSpheresNotBoxes = false;
  31. static void boolPtrButtonCallback(int buttonId, bool buttonState, void* userPointer)
  32. {
  33. if (bool* val = static_cast<bool*>(userPointer))
  34. {
  35. *val = !*val;
  36. }
  37. }
  38. /// MultiThreadedDemo shows how to setup and use multithreading
  39. class MultiThreadedDemo : public CommonRigidBodyMTBase
  40. {
  41. static const int kUpAxis = 1;
  42. btRigidBody* localCreateRigidBody(btScalar mass, const btTransform& worldTransform, btCollisionShape* colSape);
  43. btVector3 m_cameraTargetPos;
  44. float m_cameraPitch;
  45. float m_cameraYaw;
  46. float m_cameraDist;
  47. btRigidBody* m_groundBody;
  48. btTransform m_groundStartXf;
  49. float m_groundMovePhase;
  50. float m_prevRollingFriction;
  51. void createStack(const btTransform& trans, btCollisionShape* boxShape, const btVector3& halfBoxSize, int size, int width);
  52. void createSceneObjects();
  53. void destroySceneObjects();
  54. public:
  55. BT_DECLARE_ALIGNED_ALLOCATOR();
  56. MultiThreadedDemo(struct GUIHelperInterface* helper);
  57. virtual ~MultiThreadedDemo() {}
  58. btQuaternion getGroundRotation() const
  59. {
  60. btScalar tilt = gSliderGroundTilt * SIMD_2_PI / 360.0f;
  61. return btQuaternion(btVector3(1.0f, 0.0f, 0.0f), tilt);
  62. }
  63. struct TestSumBody : public btIParallelSumBody
  64. {
  65. virtual btScalar sumLoop(int iBegin, int iEnd) const BT_OVERRIDE
  66. {
  67. btScalar sum = 0.0f;
  68. for (int i = iBegin; i < iEnd; ++i)
  69. {
  70. if (i > 0)
  71. {
  72. sum += 1.0f / btScalar(i);
  73. }
  74. }
  75. return sum;
  76. }
  77. };
  78. virtual void stepSimulation(float deltaTime) BT_OVERRIDE
  79. {
  80. if (m_dynamicsWorld)
  81. {
  82. if (m_prevRollingFriction != gSliderRollingFriction)
  83. {
  84. m_prevRollingFriction = gSliderRollingFriction;
  85. btCollisionObjectArray& objArray = m_dynamicsWorld->getCollisionObjectArray();
  86. for (int i = 0; i < objArray.size(); ++i)
  87. {
  88. btCollisionObject* obj = objArray[i];
  89. obj->setRollingFriction(gSliderRollingFriction);
  90. }
  91. }
  92. if (m_groundBody)
  93. {
  94. // update ground
  95. const float cyclesPerSecond = 1.0f;
  96. m_groundMovePhase += cyclesPerSecond * deltaTime;
  97. m_groundMovePhase -= std::floor(m_groundMovePhase); // keep phase between 0 and 1
  98. btTransform xf = m_groundStartXf;
  99. float gndHOffset = btSin(m_groundMovePhase * SIMD_2_PI) * gSliderGroundHorizontalAmplitude;
  100. float gndHVel = btCos(m_groundMovePhase * SIMD_2_PI) * gSliderGroundHorizontalAmplitude * cyclesPerSecond * SIMD_2_PI; // d(gndHOffset)/dt
  101. float gndVOffset = btSin(m_groundMovePhase * SIMD_2_PI) * gSliderGroundVerticalAmplitude;
  102. float gndVVel = btCos(m_groundMovePhase * SIMD_2_PI) * gSliderGroundVerticalAmplitude * cyclesPerSecond * SIMD_2_PI; // d(gndVOffset)/dt
  103. btVector3 offset(0, 0, 0);
  104. btVector3 vel(0, 0, 0);
  105. int horizAxis = 2;
  106. offset[horizAxis] = gndHOffset;
  107. vel[horizAxis] = gndHVel;
  108. offset[kUpAxis] = gndVOffset;
  109. vel[kUpAxis] = gndVVel;
  110. xf.setOrigin(xf.getOrigin() + offset);
  111. xf.setRotation(getGroundRotation());
  112. m_groundBody->setWorldTransform(xf);
  113. m_groundBody->setLinearVelocity(vel);
  114. }
  115. // always step by 1/60 for benchmarking
  116. m_dynamicsWorld->stepSimulation(1.0f / 60.0f, 0);
  117. }
  118. #if 0
  119. {
  120. // test parallelSum
  121. TestSumBody testSumBody;
  122. float testSum = btParallelSum( 1, 10000000, 10000, testSumBody );
  123. printf( "sum = %f\n", testSum );
  124. }
  125. #endif
  126. }
  127. virtual void initPhysics() BT_OVERRIDE;
  128. virtual void resetCamera() BT_OVERRIDE
  129. {
  130. m_guiHelper->resetCamera(m_cameraDist,
  131. m_cameraYaw,
  132. m_cameraPitch,
  133. m_cameraTargetPos.x(),
  134. m_cameraTargetPos.y(),
  135. m_cameraTargetPos.z());
  136. }
  137. };
  138. MultiThreadedDemo::MultiThreadedDemo(struct GUIHelperInterface* helper)
  139. : CommonRigidBodyMTBase(helper)
  140. {
  141. m_groundBody = NULL;
  142. m_groundMovePhase = 0.0f;
  143. m_cameraTargetPos = btVector3(0.0f, 0.0f, 0.0f);
  144. m_cameraPitch = -30.0f;
  145. m_cameraYaw = 90.0f;
  146. m_cameraDist = 48.0f;
  147. m_prevRollingFriction = -1.0f;
  148. helper->setUpAxis(kUpAxis);
  149. }
  150. void MultiThreadedDemo::initPhysics()
  151. {
  152. createEmptyDynamicsWorld();
  153. m_dynamicsWorld->setGravity(btVector3(0, -10, 0));
  154. {
  155. SliderParams slider("Stack height", &gSliderStackHeight);
  156. slider.m_minVal = 1.0f;
  157. slider.m_maxVal = 30.0f;
  158. slider.m_clampToIntegers = true;
  159. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  160. }
  161. {
  162. SliderParams slider("Stack width", &gSliderStackWidth);
  163. slider.m_minVal = 1.0f;
  164. slider.m_maxVal = 30.0f;
  165. slider.m_clampToIntegers = true;
  166. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  167. }
  168. {
  169. SliderParams slider("Stack rows", &gSliderStackRows);
  170. slider.m_minVal = 1.0f;
  171. slider.m_maxVal = 20.0f;
  172. slider.m_clampToIntegers = true;
  173. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  174. }
  175. {
  176. SliderParams slider("Stack columns", &gSliderStackColumns);
  177. slider.m_minVal = 1.0f;
  178. slider.m_maxVal = 20.0f;
  179. slider.m_clampToIntegers = true;
  180. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  181. }
  182. {
  183. // horizontal ground shake
  184. SliderParams slider("Ground horiz amp", &gSliderGroundHorizontalAmplitude);
  185. slider.m_minVal = 0.0f;
  186. slider.m_maxVal = 1.0f;
  187. slider.m_clampToNotches = false;
  188. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  189. }
  190. {
  191. // vertical ground shake
  192. SliderParams slider("Ground vert amp", &gSliderGroundVerticalAmplitude);
  193. slider.m_minVal = 0.0f;
  194. slider.m_maxVal = 1.0f;
  195. slider.m_clampToNotches = false;
  196. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  197. }
  198. {
  199. // ground tilt
  200. SliderParams slider("Ground tilt", &gSliderGroundTilt);
  201. slider.m_minVal = -45.0f;
  202. slider.m_maxVal = 45.0f;
  203. slider.m_clampToNotches = false;
  204. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  205. }
  206. {
  207. // rolling friction
  208. SliderParams slider("Rolling friction", &gSliderRollingFriction);
  209. slider.m_minVal = 0.0f;
  210. slider.m_maxVal = 1.0f;
  211. slider.m_clampToNotches = false;
  212. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  213. }
  214. {
  215. ButtonParams button("Spheres not boxes", 0, false);
  216. button.m_initialState = gSpheresNotBoxes;
  217. button.m_userPointer = &gSpheresNotBoxes;
  218. button.m_callback = boolPtrButtonCallback;
  219. m_guiHelper->getParameterInterface()->registerButtonParameter(button);
  220. }
  221. createSceneObjects();
  222. m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
  223. }
  224. btRigidBody* MultiThreadedDemo::localCreateRigidBody(btScalar mass, const btTransform& startTransform, btCollisionShape* shape)
  225. {
  226. btRigidBody* body = createRigidBody(mass, startTransform, shape);
  227. if (mass > 0.0f)
  228. {
  229. // prevent bodies from sleeping to make profiling/benchmarking easier
  230. body->forceActivationState(DISABLE_DEACTIVATION);
  231. }
  232. return body;
  233. }
  234. void MultiThreadedDemo::createStack(const btTransform& parentTrans, btCollisionShape* boxShape, const btVector3& halfBoxSize, int height, int width)
  235. {
  236. btTransform trans;
  237. trans.setIdentity();
  238. trans.setRotation(parentTrans.getRotation());
  239. float halfBoxHeight = halfBoxSize.y();
  240. float halfBoxWidth = halfBoxSize.x();
  241. btVector3 offset = btVector3(0, 0, -halfBoxSize.z() * (width - 1));
  242. for (int iZ = 0; iZ < width; iZ++)
  243. {
  244. offset += btVector3(0, 0, halfBoxSize.z() * 2.0f);
  245. for (int iY = 0; iY < height; iY++)
  246. {
  247. // This constructs a row, from left to right
  248. int rowSize = height - iY;
  249. for (int iX = 0; iX < rowSize; iX++)
  250. {
  251. btVector3 pos = offset + btVector3(halfBoxWidth * (1 + iX * 2 - rowSize),
  252. halfBoxHeight * (1 + iY * 2),
  253. 0.0f);
  254. trans.setOrigin(parentTrans(pos));
  255. btScalar mass = 1.f;
  256. btRigidBody* body = localCreateRigidBody(mass, trans, boxShape);
  257. body->setFriction(1.0f);
  258. body->setRollingFriction(gSliderRollingFriction);
  259. }
  260. }
  261. }
  262. }
  263. void MultiThreadedDemo::createSceneObjects()
  264. {
  265. {
  266. // create ground box
  267. m_groundStartXf.setOrigin(btVector3(0.f, -3.f, 0.f));
  268. m_groundStartXf.setRotation(getGroundRotation());
  269. //either use heightfield or triangle mesh
  270. btVector3 groundExtents(400, 400, 400);
  271. groundExtents[kUpAxis] = 3;
  272. btCollisionShape* groundShape = new btBoxShape(groundExtents);
  273. m_collisionShapes.push_back(groundShape);
  274. //create ground object
  275. m_groundBody = createKinematicBody(m_groundStartXf, groundShape);
  276. m_groundBody->forceActivationState(DISABLE_DEACTIVATION);
  277. m_groundBody->setFriction(1.0f);
  278. }
  279. {
  280. // create walls of cubes
  281. const btVector3 halfExtents = btVector3(0.5f, 0.25f, 0.5f);
  282. int numStackRows = btMax(1, int(gSliderStackRows));
  283. int numStackCols = btMax(1, int(gSliderStackColumns));
  284. int stackHeight = int(gSliderStackHeight);
  285. int stackWidth = int(gSliderStackWidth);
  286. float stackZSpacing = 2.0f + stackWidth * halfExtents.x() * 2.0f;
  287. float stackXSpacing = 20.0f;
  288. btBoxShape* boxShape = new btBoxShape(halfExtents);
  289. m_collisionShapes.push_back(boxShape);
  290. btSphereShape* sphereShape = new btSphereShape(0.5f);
  291. m_collisionShapes.push_back(sphereShape);
  292. btCollisionShape* shape = boxShape;
  293. if (gSpheresNotBoxes)
  294. {
  295. shape = sphereShape;
  296. }
  297. btTransform groundTrans;
  298. groundTrans.setIdentity();
  299. groundTrans.setRotation(getGroundRotation());
  300. for (int iX = 0; iX < numStackCols; ++iX)
  301. {
  302. for (int iZ = 0; iZ < numStackRows; ++iZ)
  303. {
  304. btVector3 center = btVector3(iX * stackXSpacing, 0.0f, (iZ - numStackRows / 2) * stackZSpacing);
  305. btTransform trans = groundTrans;
  306. trans.setOrigin(groundTrans(center));
  307. createStack(trans, shape, halfExtents, stackHeight, stackWidth);
  308. }
  309. }
  310. }
  311. #if 0
  312. if ( false )
  313. {
  314. // destroyer ball
  315. btTransform sphereTrans;
  316. sphereTrans.setIdentity();
  317. sphereTrans.setOrigin( btVector3( 0, 2, 40 ) );
  318. btSphereShape* ball = new btSphereShape( 2.f );
  319. m_collisionShapes.push_back( ball );
  320. btRigidBody* ballBody = localCreateRigidBody( 10000.f, sphereTrans, ball );
  321. ballBody->setLinearVelocity( btVector3( 0, 0, -10 ) );
  322. }
  323. #endif
  324. m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
  325. }
  326. CommonExampleInterface* MultiThreadedDemoCreateFunc(struct CommonExampleOptions& options)
  327. {
  328. return new MultiThreadedDemo(options.m_guiHelper);
  329. }