InclinedPlane.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 "InclinedPlane.h"
  14. #include "btBulletDynamicsCommon.h"
  15. #include "LinearMath/btVector3.h"
  16. #include "LinearMath/btAlignedObjectArray.h"
  17. #include "../CommonInterfaces/CommonRigidBodyBase.h"
  18. #include "../CommonInterfaces/CommonParameterInterface.h"
  19. static btScalar gTilt = 20.0f / 180.0f * SIMD_PI; // tilt the ramp 20 degrees
  20. static btScalar gRampFriction = 1; // set ramp friction to 1
  21. static btScalar gRampRestitution = 0; // set ramp restitution to 0 (no restitution)
  22. static btScalar gBoxFriction = 1; // set box friction to 1
  23. static btScalar gBoxRestitution = 0; // set box restitution to 0
  24. static btScalar gSphereFriction = 1; // set sphere friction to 1
  25. static btScalar gSphereRollingFriction = 1; // set sphere rolling friction to 1
  26. static btScalar gSphereSpinningFriction = 0.3; // set sphere spinning friction to 0.3
  27. static btScalar gSphereRestitution = 0; // set sphere restitution to 0
  28. // handles for changes
  29. static btRigidBody* ramp = NULL;
  30. static btRigidBody* gBox = NULL;
  31. static btRigidBody* gSphere = NULL;
  32. struct InclinedPlaneExample : public CommonRigidBodyBase
  33. {
  34. InclinedPlaneExample(struct GUIHelperInterface* helper)
  35. : CommonRigidBodyBase(helper)
  36. {
  37. }
  38. virtual ~InclinedPlaneExample() {}
  39. virtual void initPhysics();
  40. virtual void resetScene();
  41. virtual void renderScene();
  42. virtual void stepSimulation(float deltaTime);
  43. virtual bool keyboardCallback(int key, int state);
  44. void resetCamera()
  45. {
  46. float dist = 41;
  47. float pitch = -35;
  48. float yaw = 52;
  49. float targetPos[3] = {0, 0.46, 0};
  50. m_guiHelper->resetCamera(dist, yaw, pitch, targetPos[0], targetPos[1], targetPos[2]);
  51. }
  52. };
  53. void onBoxFrictionChanged(float friction, void* userPtr);
  54. void onBoxRestitutionChanged(float restitution, void* userPtr);
  55. void onSphereFrictionChanged(float friction, void* userPtr);
  56. void onSphereRestitutionChanged(float restitution, void* userPtr);
  57. void onRampInclinationChanged(float inclination, void* userPtr);
  58. void onRampFrictionChanged(float friction, void* userPtr);
  59. void onRampRestitutionChanged(float restitution, void* userPtr);
  60. void InclinedPlaneExample::initPhysics()
  61. {
  62. { // create slider to change the ramp tilt
  63. SliderParams slider("Ramp Tilt", &gTilt);
  64. slider.m_minVal = 0;
  65. slider.m_maxVal = SIMD_PI / 2.0f;
  66. slider.m_clampToNotches = false;
  67. slider.m_callback = onRampInclinationChanged;
  68. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  69. }
  70. { // create slider to change the ramp friction
  71. SliderParams slider("Ramp Friction", &gRampFriction);
  72. slider.m_minVal = 0;
  73. slider.m_maxVal = 10;
  74. slider.m_clampToNotches = false;
  75. slider.m_callback = onRampFrictionChanged;
  76. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  77. }
  78. { // create slider to change the ramp restitution
  79. SliderParams slider("Ramp Restitution", &gRampRestitution);
  80. slider.m_minVal = 0;
  81. slider.m_maxVal = 1;
  82. slider.m_clampToNotches = false;
  83. slider.m_callback = onRampRestitutionChanged;
  84. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  85. }
  86. { // create slider to change the box friction
  87. SliderParams slider("Box Friction", &gBoxFriction);
  88. slider.m_minVal = 0;
  89. slider.m_maxVal = 10;
  90. slider.m_clampToNotches = false;
  91. slider.m_callback = onBoxFrictionChanged;
  92. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  93. }
  94. { // create slider to change the box restitution
  95. SliderParams slider("Box Restitution", &gBoxRestitution);
  96. slider.m_minVal = 0;
  97. slider.m_maxVal = 1;
  98. slider.m_clampToNotches = false;
  99. slider.m_callback = onBoxRestitutionChanged;
  100. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  101. }
  102. { // create slider to change the sphere friction
  103. SliderParams slider("Sphere Friction", &gSphereFriction);
  104. slider.m_minVal = 0;
  105. slider.m_maxVal = 10;
  106. slider.m_clampToNotches = false;
  107. slider.m_callback = onSphereFrictionChanged;
  108. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  109. }
  110. { // create slider to change the sphere rolling friction
  111. SliderParams slider("Sphere Rolling Friction", &gSphereRollingFriction);
  112. slider.m_minVal = 0;
  113. slider.m_maxVal = 10;
  114. slider.m_clampToNotches = false;
  115. slider.m_callback = onSphereRestitutionChanged;
  116. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  117. }
  118. { // create slider to change the sphere rolling friction
  119. SliderParams slider("Sphere Spinning", &gSphereSpinningFriction);
  120. slider.m_minVal = 0;
  121. slider.m_maxVal = 2;
  122. slider.m_clampToNotches = false;
  123. slider.m_callback = onSphereRestitutionChanged;
  124. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  125. }
  126. { // create slider to change the sphere restitution
  127. SliderParams slider("Sphere Restitution", &gSphereRestitution);
  128. slider.m_minVal = 0;
  129. slider.m_maxVal = 1;
  130. slider.m_clampToNotches = false;
  131. m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
  132. }
  133. m_guiHelper->setUpAxis(1); // set Y axis as up axis
  134. createEmptyDynamicsWorld();
  135. // create debug drawer
  136. m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
  137. if (m_dynamicsWorld->getDebugDrawer())
  138. m_dynamicsWorld->getDebugDrawer()->setDebugMode(btIDebugDraw::DBG_DrawWireframe + btIDebugDraw::DBG_DrawContactPoints);
  139. { // create a static ground
  140. btBoxShape* groundShape = createBoxShape(btVector3(btScalar(50.), btScalar(50.), btScalar(50.)));
  141. m_collisionShapes.push_back(groundShape);
  142. btTransform groundTransform;
  143. groundTransform.setIdentity();
  144. groundTransform.setOrigin(btVector3(0, -50, 0));
  145. btScalar mass(0.);
  146. createRigidBody(mass, groundTransform, groundShape, btVector4(0, 0, 1, 1));
  147. }
  148. { //create a static inclined plane
  149. btBoxShape* inclinedPlaneShape = createBoxShape(btVector3(btScalar(20.), btScalar(1.), btScalar(10.)));
  150. m_collisionShapes.push_back(inclinedPlaneShape);
  151. btTransform startTransform;
  152. startTransform.setIdentity();
  153. // position the inclined plane above ground
  154. startTransform.setOrigin(btVector3(
  155. btScalar(0),
  156. btScalar(15),
  157. btScalar(0)));
  158. btQuaternion incline;
  159. incline.setRotation(btVector3(0, 0, 1), gTilt);
  160. startTransform.setRotation(incline);
  161. btScalar mass(0.);
  162. ramp = createRigidBody(mass, startTransform, inclinedPlaneShape);
  163. ramp->setFriction(gRampFriction);
  164. ramp->setRestitution(gRampRestitution);
  165. }
  166. { //create a cube above the inclined plane
  167. btBoxShape* boxShape = createBoxShape(btVector3(1, 1, 1));
  168. m_collisionShapes.push_back(boxShape);
  169. btTransform startTransform;
  170. startTransform.setIdentity();
  171. btScalar boxMass(1.f);
  172. startTransform.setOrigin(
  173. btVector3(btScalar(0), btScalar(20), btScalar(2)));
  174. gBox = createRigidBody(boxMass, startTransform, boxShape);
  175. gBox->forceActivationState(DISABLE_DEACTIVATION); // to prevent the box on the ramp from disabling
  176. gBox->setFriction(gBoxFriction);
  177. gBox->setRestitution(gBoxRestitution);
  178. }
  179. { //create a sphere above the inclined plane
  180. btSphereShape* sphereShape = new btSphereShape(btScalar(1));
  181. m_collisionShapes.push_back(sphereShape);
  182. btTransform startTransform;
  183. startTransform.setIdentity();
  184. btScalar sphereMass(1.f);
  185. startTransform.setOrigin(
  186. btVector3(btScalar(0), btScalar(20), btScalar(4)));
  187. gSphere = createRigidBody(sphereMass, startTransform, sphereShape);
  188. gSphere->forceActivationState(DISABLE_DEACTIVATION); // to prevent the sphere on the ramp from disabling
  189. gSphere->setFriction(gSphereFriction);
  190. gSphere->setRestitution(gSphereRestitution);
  191. gSphere->setRollingFriction(gSphereRollingFriction);
  192. gSphere->setSpinningFriction(gSphereSpinningFriction);
  193. }
  194. m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
  195. }
  196. void InclinedPlaneExample::resetScene()
  197. {
  198. { //reset a cube above the inclined plane
  199. btTransform startTransform;
  200. startTransform.setIdentity();
  201. startTransform.setOrigin(
  202. btVector3(btScalar(0), btScalar(20), btScalar(2)));
  203. gBox->setWorldTransform(startTransform);
  204. btVector3 zero(0, 0, 0);
  205. gBox->setAngularVelocity(zero);
  206. gBox->setLinearVelocity(zero);
  207. gBox->clearForces();
  208. }
  209. { //reset a sphere above the inclined plane
  210. btTransform startTransform;
  211. startTransform.setIdentity();
  212. startTransform.setOrigin(
  213. btVector3(btScalar(0), btScalar(20), btScalar(4)));
  214. gSphere->setWorldTransform(startTransform);
  215. btVector3 zero(0, 0, 0);
  216. gSphere->setAngularVelocity(zero);
  217. gSphere->setLinearVelocity(zero);
  218. gSphere->clearForces();
  219. }
  220. }
  221. void InclinedPlaneExample::stepSimulation(float deltaTime)
  222. {
  223. if (m_dynamicsWorld)
  224. {
  225. m_dynamicsWorld->stepSimulation(deltaTime);
  226. }
  227. }
  228. void InclinedPlaneExample::renderScene()
  229. {
  230. CommonRigidBodyBase::renderScene();
  231. }
  232. bool InclinedPlaneExample::keyboardCallback(int key, int state)
  233. {
  234. // b3Printf("Key pressed: %d in state %d \n",key,state);
  235. switch (key)
  236. {
  237. case 32 /*ASCII for space*/:
  238. {
  239. resetScene();
  240. break;
  241. }
  242. }
  243. return false;
  244. }
  245. // GUI parameter modifiers
  246. void onBoxFrictionChanged(float friction, void*)
  247. {
  248. if (gBox)
  249. {
  250. gBox->setFriction(friction);
  251. // b3Printf("Friction of box changed to %f",friction );
  252. }
  253. }
  254. void onBoxRestitutionChanged(float restitution, void*)
  255. {
  256. if (gBox)
  257. {
  258. gBox->setRestitution(restitution);
  259. //b3Printf("Restitution of box changed to %f",restitution);
  260. }
  261. }
  262. void onSphereFrictionChanged(float friction, void*)
  263. {
  264. if (gSphere)
  265. {
  266. gSphere->setFriction(friction);
  267. //b3Printf("Friction of sphere changed to %f",friction );
  268. }
  269. }
  270. void onSphereRestitutionChanged(float restitution, void*)
  271. {
  272. if (gSphere)
  273. {
  274. gSphere->setRestitution(restitution);
  275. //b3Printf("Restitution of sphere changed to %f",restitution);
  276. }
  277. }
  278. void onRampInclinationChanged(float inclination, void*)
  279. {
  280. if (ramp)
  281. {
  282. btTransform startTransform;
  283. startTransform.setIdentity();
  284. // position the inclined plane above ground
  285. startTransform.setOrigin(
  286. btVector3(btScalar(0), btScalar(15), btScalar(0)));
  287. btQuaternion incline;
  288. incline.setRotation(btVector3(0, 0, 1), gTilt);
  289. startTransform.setRotation(incline);
  290. ramp->setWorldTransform(startTransform);
  291. //b3Printf("Inclination of ramp changed to %f",inclination );
  292. }
  293. }
  294. void onRampFrictionChanged(float friction, void*)
  295. {
  296. if (ramp)
  297. {
  298. ramp->setFriction(friction);
  299. //b3Printf("Friction of ramp changed to %f \n",friction );
  300. }
  301. }
  302. void onRampRestitutionChanged(float restitution, void*)
  303. {
  304. if (ramp)
  305. {
  306. ramp->setRestitution(restitution);
  307. //b3Printf("Restitution of ramp changed to %f \n",restitution);
  308. }
  309. }
  310. CommonExampleInterface* ET_InclinedPlaneCreateFunc(CommonExampleOptions& options)
  311. {
  312. return new InclinedPlaneExample(options.m_guiHelper);
  313. }