You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BtPhysTest.cpp 9.0 KiB

12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // BtPhysTest
  3. //
  4. // Copyright: (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
  5. // (c) 2012 Sam Hocevar <sam@hocevar.net>
  6. //
  7. #if defined HAVE_CONFIG_H
  8. # include "config.h"
  9. #endif
  10. #if defined _WIN32
  11. # include <direct.h>
  12. #endif
  13. #if defined _XBOX
  14. # define _USE_MATH_DEFINES /* for M_PI */
  15. # include <xtl.h>
  16. # undef near /* Fuck Microsoft */
  17. # undef far /* Fuck Microsoft again */
  18. #elif defined _WIN32
  19. # define _USE_MATH_DEFINES /* for M_PI */
  20. # define WIN32_LEAN_AND_MEAN
  21. # include <windows.h>
  22. # undef near /* Fuck Microsoft */
  23. # undef far /* Fuck Microsoft again */
  24. #else
  25. # include <cmath>
  26. #endif
  27. #if USE_SDL && defined __APPLE__
  28. # include <SDL_main.h>
  29. #endif
  30. #include <bullet/btBulletDynamicsCommon.h>
  31. #include <bullet/btBulletCollisionCommon.h>
  32. #include "core.h"
  33. #include "loldebug.h"
  34. using namespace lol;
  35. #include "BtPhysTest.h"
  36. #define CUBE_HALF_EXTENTS .5f
  37. #define EXTRA_HEIGHT 1.f
  38. int gNumObjects = 64;
  39. BtPhysTest::BtPhysTest(bool editor)
  40. {
  41. /* Create a camera that matches the settings of XNA BtPhysTest */
  42. m_camera = new Camera(vec3(0.f, 600.f, 0.f),
  43. vec3(0.f, 0.f, 0.f),
  44. vec3(0, 1, 0));
  45. m_camera->SetRotation(quat::fromeuler_xyz(0.f, 0.f, 0.f));
  46. m_camera->SetPerspective(90.f, 1280.f, 960.f, .1f, 1000.f);
  47. //m_camera->SetOrtho(1280.f / 6, 960.f / 6, -1000.f, 1000.f);
  48. Ticker::Ref(m_camera);
  49. m_ready = false;
  50. //init Physics
  51. {
  52. m_bt_ccd_mode = USE_CCD;
  53. //collision configuration contains default setup for memory, collision setup
  54. m_bt_collision_config = new btDefaultCollisionConfiguration();
  55. //use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
  56. m_bt_dispatcher = new btCollisionDispatcher(m_bt_collision_config);
  57. m_bt_dispatcher->registerCollisionCreateFunc(BOX_SHAPE_PROXYTYPE,BOX_SHAPE_PROXYTYPE,m_bt_collision_config->getCollisionAlgorithmCreateFunc(CONVEX_SHAPE_PROXYTYPE,CONVEX_SHAPE_PROXYTYPE));
  58. m_bt_broadphase = new btDbvtBroadphase();
  59. ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
  60. m_bt_solver = new btSequentialImpulseConstraintSolver;
  61. m_bt_world = new btDiscreteDynamicsWorld(m_bt_dispatcher, m_bt_broadphase, m_bt_solver, m_bt_collision_config);
  62. //m_bt_world->setDebugDrawer(&sDebugDrawer);
  63. m_bt_world->getSolverInfo().m_splitImpulse = true;
  64. m_bt_world->getSolverInfo().m_numIterations = 20;
  65. m_bt_world->getDispatchInfo().m_useContinuous = (m_bt_ccd_mode == USE_CCD);
  66. m_bt_world->setGravity(btVector3(0,-10,0));
  67. ///create a few basic rigid bodies
  68. btBoxShape* box = new btBoxShape(btVector3(btScalar(110.),btScalar(1.),btScalar(110.)));
  69. btCollisionShape* groundShape = box;
  70. m_bt_collision_shapes << groundShape;
  71. m_ground_mesh.Compile("[sc#ddd afcb110 1 110 -1]");
  72. //m_bt_collision_shapes << new btCylinderShape(btVector3(.5f,.5f,.5f));
  73. btTransform groundTransform;
  74. groundTransform.setIdentity();
  75. //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
  76. {
  77. btScalar mass(0.);
  78. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  79. bool isDynamic = (mass != 0.f);
  80. btVector3 localInertia(0,0,0);
  81. if (isDynamic)
  82. groundShape->calculateLocalInertia(mass,localInertia);
  83. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  84. btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
  85. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
  86. btRigidBody* body = new btRigidBody(rbInfo);
  87. //add the body to the dynamics world
  88. m_bt_world->addRigidBody(body);
  89. }
  90. //Adding Shapes
  91. {
  92. //create a few dynamic rigidbodies
  93. // Re-using the same collision is better for memory usage and performance
  94. btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
  95. m_rigid_mesh[0].Compile("[sc#daa afcb2 2 2 -.1]");
  96. m_rigid_mesh[1].Compile("[sc#ada afcb2 2 2 -.1]");
  97. m_rigid_mesh[2].Compile("[sc#aad afcb2 2 2 -.1]");
  98. m_bt_collision_shapes << colShape;
  99. m_bt_dynamic_shapes << colShape;
  100. /// Create Dynamic Objects
  101. btTransform startTransform;
  102. startTransform.setIdentity();
  103. btScalar mass(1.f);
  104. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  105. bool isDynamic = (mass != 0.f);
  106. btVector3 localInertia(0,0,0);
  107. if (isDynamic)
  108. colShape->calculateLocalInertia(mass,localInertia);
  109. int i;
  110. for (i=0;i<gNumObjects;i++)
  111. {
  112. btCollisionShape* shape = colShape;
  113. btTransform trans;
  114. trans.setIdentity();
  115. //stack them
  116. int colsize = 10;
  117. int row = (i*CUBE_HALF_EXTENTS*2)/(colsize*2*CUBE_HALF_EXTENTS);
  118. int row2 = row;
  119. int col = (i)%(colsize)-colsize/2;
  120. if (col>3)
  121. {
  122. col=11;
  123. row2 |=1;
  124. }
  125. btVector3 pos(((row+col+row2) % 4)*CUBE_HALF_EXTENTS,
  126. 20.0f + row*4*CUBE_HALF_EXTENTS+CUBE_HALF_EXTENTS+EXTRA_HEIGHT,
  127. col*3*CUBE_HALF_EXTENTS + (row2%2)*CUBE_HALF_EXTENTS);
  128. trans.setOrigin(pos);
  129. float mass = 1.f;
  130. btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));
  131. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  132. bool isDynamic = (mass != 0.f);
  133. btVector3 localInertia(0,0,0);
  134. if (isDynamic)
  135. shape->calculateLocalInertia(mass,localInertia);
  136. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  137. btDefaultMotionState* myMotionState = new btDefaultMotionState(trans);
  138. btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);
  139. btRigidBody* body = new btRigidBody(cInfo);
  140. body->setContactProcessingThreshold(BT_LARGE_FLOAT);
  141. m_bt_world->addRigidBody(body);
  142. ///when using m_ccdMode
  143. if (m_bt_ccd_mode == USE_CCD)
  144. {
  145. body->setCcdMotionThreshold(CUBE_HALF_EXTENTS);
  146. body->setCcdSweptSphereRadius(0.9*CUBE_HALF_EXTENTS);
  147. }
  148. }
  149. }
  150. }
  151. }
  152. void BtPhysTest::TickGame(float seconds)
  153. {
  154. WorldEntity::TickGame(seconds);
  155. if (Input::GetButtonState(27 /*SDLK_ESCAPE*/))
  156. Ticker::Shutdown();
  157. ///step the simulation
  158. if (m_bt_world)
  159. {
  160. int steps = (int)(seconds / 0.005f);
  161. for (int i = 0; i < steps; i++)
  162. m_bt_world->stepSimulation(seconds / steps);
  163. //optional but useful: debug drawing
  164. //m_bt_world->debugDrawWorld();
  165. }
  166. }
  167. void BtPhysTest::TickDraw(float seconds)
  168. {
  169. WorldEntity::TickDraw(seconds);
  170. if (!m_ready)
  171. {
  172. m_ground_mesh.MeshConvert();
  173. m_rigid_mesh[0].MeshConvert();
  174. m_rigid_mesh[1].MeshConvert();
  175. m_rigid_mesh[2].MeshConvert();
  176. /* FIXME: this object never cleans up */
  177. m_ready = true;
  178. }
  179. Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f));
  180. vec3 BarycenterLocation = vec3(.0f);
  181. float BarycenterFactor = 0.0f;
  182. for(int i=0;i<gNumObjects;i++)
  183. {
  184. mat4 m(1.0f);
  185. btMatrix3x3 rot; rot.setIdentity();
  186. btCollisionObject* colObj = m_bt_world->getCollisionObjectArray()[i];
  187. btRigidBody* body = btRigidBody::upcast(colObj);
  188. if(body && body->getMotionState())
  189. {
  190. btDefaultMotionState* myMotionState = (btDefaultMotionState*)body->getMotionState();
  191. myMotionState->m_graphicsWorldTrans.getOpenGLMatrix(&m[0][0]);
  192. rot = myMotionState->m_graphicsWorldTrans.getBasis();
  193. }
  194. else
  195. {
  196. colObj->getWorldTransform().getOpenGLMatrix(&m[0][0]);
  197. rot = colObj->getWorldTransform().getBasis();
  198. }
  199. if (i > 0)
  200. {
  201. BarycenterLocation += m.v3.xyz;
  202. BarycenterFactor += 1.0f;
  203. }
  204. if (i == 0)
  205. m_ground_mesh.Render(m);
  206. else
  207. m_rigid_mesh[i % 3].Render(m);
  208. }
  209. if (BarycenterFactor > .0f)
  210. {
  211. BarycenterLocation /= BarycenterFactor;
  212. m_camera->SetTarget(BarycenterLocation);
  213. m_camera->SetPosition(BarycenterLocation + vec3(-15.0f, 8.0f, .0f));
  214. }
  215. }
  216. BtPhysTest::~BtPhysTest()
  217. {
  218. Ticker::Unref(m_camera);
  219. //Exit Physics
  220. {
  221. //cleanup in the reverse order of creation/initialization
  222. //remove the rigidbodies from the dynamics world and delete them
  223. for (int i = m_bt_world->getNumCollisionObjects() - 1; i >= 0 ;i--)
  224. {
  225. btCollisionObject* obj = m_bt_world->getCollisionObjectArray()[i];
  226. btRigidBody* body = btRigidBody::upcast(obj);
  227. if (body && body->getMotionState())
  228. delete body->getMotionState();
  229. m_bt_world->removeCollisionObject(obj);
  230. delete obj;
  231. }
  232. //delete collision shapes
  233. for (int j = 0; j < m_bt_collision_shapes.Count(); j++)
  234. {
  235. btCollisionShape* shape = m_bt_collision_shapes[j];
  236. delete shape;
  237. }
  238. m_bt_collision_shapes.Empty();
  239. delete m_bt_world;
  240. delete m_bt_solver;
  241. delete m_bt_broadphase;
  242. delete m_bt_dispatcher;
  243. delete m_bt_collision_config;
  244. }
  245. }
  246. int main(int argc, char **argv)
  247. {
  248. Application app("BtPhysTest", ivec2(800, 600), 60.0f);
  249. #if defined _MSC_VER && !defined _XBOX
  250. _chdir("..");
  251. #elif defined _WIN32 && !defined _XBOX
  252. _chdir("../..");
  253. #endif
  254. new BtPhysTest(argc > 1);
  255. app.ShowPointer(false);
  256. app.Run();
  257. return EXIT_SUCCESS;
  258. }