InclinedPlane.cpp 11 KB

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