376 linhas
10 KiB

  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. #ifndef HAVE_PHYS_USE_BULLET
  36. #define HAVE_PHYS_USE_BULLET
  37. #endif /* HAVE_PHYS_USE_BULLET */
  38. #include "Physics\LolPhysics.h"
  39. #include "Physics\EasyPhysics.h"
  40. #include "PhysicObject.h"
  41. #include "BtPhysTest.h"
  42. using namespace lol::phys;
  43. #define CUBE_HALF_EXTENTS .5f
  44. #define EXTRA_HEIGHT 1.f
  45. int gNumObjects = 64;
  46. BtPhysTest::BtPhysTest(bool editor)
  47. {
  48. /* Create a camera that matches the settings of XNA BtPhysTest */
  49. m_camera = new Camera(vec3(0.f, 600.f, 0.f),
  50. vec3(0.f, 0.f, 0.f),
  51. vec3(0, 1, 0));
  52. m_camera->SetRotation(quat::fromeuler_xyz(0.f, 0.f, 0.f));
  53. m_camera->SetPerspective(90.f, 1280.f, 960.f, .1f, 1000.f);
  54. //m_camera->SetOrtho(1280.f / 6, 960.f / 6, -1000.f, 1000.f);
  55. Ticker::Ref(m_camera);
  56. m_ready = false;
  57. m_simulation = new Simulation();
  58. m_simulation->InitContext();
  59. m_simulation->SetGravity(vec3(.0f, -10.0f, .0f));
  60. m_ground_object = new PhysicsObject(m_simulation);
  61. Ticker::Ref(m_ground_object);
  62. for (int x=0; x < 10; x++)
  63. {
  64. for (int y=0; y < 10; y++)
  65. {
  66. PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 10.f, vec3(0.f, 20.f, -20.0f) + vec3(.0f, 4.f * (float)y, 4.f * (float)x));
  67. m_physobj_list << new_physobj;
  68. Ticker::Ref(new_physobj);
  69. }
  70. }
  71. #if 0
  72. //init Physics
  73. {
  74. m_bt_ccd_mode = USE_CCD;
  75. //collision configuration contains default setup for memory, collision setup
  76. m_bt_collision_config = new btDefaultCollisionConfiguration();
  77. //use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
  78. m_bt_dispatcher = new btCollisionDispatcher(m_bt_collision_config);
  79. m_bt_dispatcher->registerCollisionCreateFunc(BOX_SHAPE_PROXYTYPE,
  80. BOX_SHAPE_PROXYTYPE,
  81. m_bt_collision_config->getCollisionAlgorithmCreateFunc(CONVEX_SHAPE_PROXYTYPE,
  82. CONVEX_SHAPE_PROXYTYPE));
  83. m_bt_broadphase = new btDbvtBroadphase();
  84. ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
  85. m_bt_solver = new btSequentialImpulseConstraintSolver;
  86. m_bt_world = new btDiscreteDynamicsWorld(m_bt_dispatcher, m_bt_broadphase, m_bt_solver, m_bt_collision_config);
  87. //m_bt_world->setDebugDrawer(&sDebugDrawer);
  88. m_bt_world->getSolverInfo().m_splitImpulse = true;
  89. m_bt_world->getSolverInfo().m_numIterations = 20;
  90. m_bt_world->getDispatchInfo().m_useContinuous = (m_bt_ccd_mode == USE_CCD);
  91. m_bt_world->setGravity(btVector3(0,-10,0));
  92. ///create a few basic rigid bodies
  93. btBoxShape* box = new btBoxShape(btVector3(btScalar(110.),btScalar(1.),btScalar(110.)));
  94. btCollisionShape* groundShape = box;
  95. m_bt_collision_shapes << groundShape;
  96. m_ground_mesh.Compile("[sc#ddd afcb220 2 220 -1]");
  97. //m_bt_collision_shapes << new btCylinderShape(btVector3(.5f,.5f,.5f));
  98. btTransform groundTransform;
  99. groundTransform.setIdentity();
  100. //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
  101. {
  102. btScalar mass(0.);
  103. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  104. bool isDynamic = (mass != 0.f);
  105. btVector3 localInertia(0,0,0);
  106. if (isDynamic)
  107. groundShape->calculateLocalInertia(mass,localInertia);
  108. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  109. btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
  110. btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
  111. btRigidBody* body = new btRigidBody(rbInfo);
  112. //add the body to the dynamics world
  113. m_bt_world->addRigidBody(body);
  114. }
  115. //Adding Shapes
  116. {
  117. //create a few dynamic rigidbodies
  118. // Re-using the same collision is better for memory usage and performance
  119. btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
  120. m_rigid_mesh[0].Compile("[sc#add afcb2 2 2 -.1]");
  121. m_rigid_mesh[1].Compile("[sc#dad afcb2 2 2 -.1]");
  122. m_rigid_mesh[2].Compile("[sc#dda afcb2 2 2 -.1]");
  123. m_rigid_mesh[3].Compile("[sc#daa afcb2 2 2 -.1]");
  124. m_rigid_mesh[4].Compile("[sc#ada afcb2 2 2 -.1]");
  125. m_rigid_mesh[5].Compile("[sc#aad afcb2 2 2 -.1]");
  126. m_bt_collision_shapes << colShape;
  127. m_bt_dynamic_shapes << colShape;
  128. /// Create Dynamic Objects
  129. btTransform startTransform;
  130. startTransform.setIdentity();
  131. btScalar mass(1.f);
  132. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  133. bool isDynamic = (mass != 0.f);
  134. btVector3 localInertia(0,0,0);
  135. if (isDynamic)
  136. colShape->calculateLocalInertia(mass,localInertia);
  137. int i;
  138. for (i=0;i<gNumObjects;i++)
  139. {
  140. btCollisionShape* shape = colShape;
  141. btTransform trans;
  142. trans.setIdentity();
  143. //stack them
  144. int colsize = 10;
  145. int row = int(((float)i*CUBE_HALF_EXTENTS*2.0f)/((float)colsize*2.0f*CUBE_HALF_EXTENTS));
  146. int row2 = row;
  147. int col = (i)%(colsize)-colsize/2;
  148. if (col>3)
  149. {
  150. col=11;
  151. row2 |=1;
  152. }
  153. btVector3 pos(((row+col+row2) % 4)*CUBE_HALF_EXTENTS,
  154. 20.0f + row*8*CUBE_HALF_EXTENTS+CUBE_HALF_EXTENTS+EXTRA_HEIGHT,
  155. col*8*CUBE_HALF_EXTENTS + 2 * (row2%2)*CUBE_HALF_EXTENTS);
  156. trans.setOrigin(pos);
  157. float mass = 1.f;
  158. btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));
  159. //rigidbody is dynamic if and only if mass is non zero, otherwise static
  160. bool isDynamic = (mass != 0.f);
  161. btVector3 localInertia(0,0,0);
  162. if (isDynamic)
  163. shape->calculateLocalInertia(mass,localInertia);
  164. //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
  165. btDefaultMotionState* myMotionState = new btDefaultMotionState(trans);
  166. btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);
  167. btRigidBody* body = new btRigidBody(cInfo);
  168. body->setContactProcessingThreshold(BT_LARGE_FLOAT);
  169. m_bt_world->addRigidBody(body);
  170. ///when using m_ccdMode
  171. if (m_bt_ccd_mode == USE_CCD)
  172. {
  173. body->setCcdMotionThreshold(CUBE_HALF_EXTENTS);
  174. body->setCcdSweptSphereRadius(0.9*CUBE_HALF_EXTENTS);
  175. }
  176. }
  177. }
  178. }
  179. #endif
  180. }
  181. void BtPhysTest::TickGame(float seconds)
  182. {
  183. WorldEntity::TickGame(seconds);
  184. if (Input::GetButtonState(27 /*SDLK_ESCAPE*/))
  185. Ticker::Shutdown();
  186. m_simulation->TickContext(seconds);
  187. m_camera->SetTarget(vec3(.0f));
  188. m_camera->SetPosition(vec3(-30.0f, 10.0f, .0f));
  189. #if 0
  190. ///step the simulation
  191. if (m_bt_world)
  192. {
  193. //int steps = (int)(seconds / 0.005f);
  194. //for (int i = 0; i < steps; i++)
  195. m_bt_world->stepSimulation(seconds /*/ steps*/);
  196. //optional but useful: debug drawing
  197. //m_bt_world->debugDrawWorld();
  198. }
  199. #endif
  200. }
  201. void BtPhysTest::TickDraw(float seconds)
  202. {
  203. WorldEntity::TickDraw(seconds);
  204. if (!m_ready)
  205. {
  206. #if 0
  207. m_ground_mesh.MeshConvert();
  208. m_rigid_mesh[0].MeshConvert();
  209. m_rigid_mesh[1].MeshConvert();
  210. m_rigid_mesh[2].MeshConvert();
  211. m_rigid_mesh[3].MeshConvert();
  212. m_rigid_mesh[4].MeshConvert();
  213. m_rigid_mesh[5].MeshConvert();
  214. #endif
  215. /* FIXME: this object never cleans up */
  216. m_ready = true;
  217. }
  218. Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f));
  219. #if 0
  220. vec3 BarycenterLocation = vec3(.0f);
  221. float BarycenterFactor = 0.0f;
  222. for(int i=0;i<gNumObjects;i++)
  223. {
  224. mat4 m(1.0f);
  225. btMatrix3x3 rot; rot.setIdentity();
  226. btCollisionObject* colObj = m_bt_world->getCollisionObjectArray()[i];
  227. btRigidBody* body = btRigidBody::upcast(colObj);
  228. if(body && body->getMotionState())
  229. {
  230. btDefaultMotionState* myMotionState = (btDefaultMotionState*)body->getMotionState();
  231. myMotionState->m_graphicsWorldTrans.getOpenGLMatrix(&m[0][0]);
  232. rot = myMotionState->m_graphicsWorldTrans.getBasis();
  233. }
  234. else
  235. {
  236. colObj->getWorldTransform().getOpenGLMatrix(&m[0][0]);
  237. rot = colObj->getWorldTransform().getBasis();
  238. }
  239. if (i > 0)
  240. {
  241. BarycenterLocation += m.v3.xyz;
  242. BarycenterFactor += 1.0f;
  243. }
  244. if (i == 0)
  245. m_ground_mesh.Render(m);
  246. else
  247. m_rigid_mesh[i % 6].Render(m);
  248. }
  249. if (BarycenterFactor > .0f)
  250. {
  251. BarycenterLocation /= BarycenterFactor;
  252. m_camera->SetTarget(BarycenterLocation);
  253. m_camera->SetPosition(BarycenterLocation + vec3(-20.0f, 8.0f, .0f));
  254. }
  255. #endif
  256. }
  257. BtPhysTest::~BtPhysTest()
  258. {
  259. Ticker::Unref(m_camera);
  260. #if 0
  261. //Exit Physics
  262. {
  263. //cleanup in the reverse order of creation/initialization
  264. //remove the rigidbodies from the dynamics world and delete them
  265. for (int i = m_bt_world->getNumCollisionObjects() - 1; i >= 0 ;i--)
  266. {
  267. btCollisionObject* obj = m_bt_world->getCollisionObjectArray()[i];
  268. btRigidBody* body = btRigidBody::upcast(obj);
  269. if (body && body->getMotionState())
  270. delete body->getMotionState();
  271. m_bt_world->removeCollisionObject(obj);
  272. delete obj;
  273. }
  274. //delete collision shapes
  275. for (int j = 0; j < m_bt_collision_shapes.Count(); j++)
  276. {
  277. btCollisionShape* shape = m_bt_collision_shapes[j];
  278. delete shape;
  279. }
  280. m_bt_collision_shapes.Empty();
  281. delete m_bt_world;
  282. delete m_bt_solver;
  283. delete m_bt_broadphase;
  284. delete m_bt_dispatcher;
  285. delete m_bt_collision_config;
  286. }
  287. #endif
  288. }
  289. int main(int argc, char **argv)
  290. {
  291. Application app("BtPhysTest", ivec2(1280, 720), 60.0f);
  292. #if defined _MSC_VER && !defined _XBOX
  293. _chdir("..");
  294. #elif defined _WIN32 && !defined _XBOX
  295. _chdir("../..");
  296. #endif
  297. new BtPhysTest(argc > 1);
  298. app.ShowPointer(false);
  299. app.Run();
  300. return EXIT_SUCCESS;
  301. }