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.

132 lines
2.7 KiB

  1. //
  2. // LolPhysics
  3. //
  4. // Copyright: (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com>
  5. // (c) 2012 Sam Hocevar <sam@hocevar.net>
  6. //
  7. #if !defined __LOLPHYSICS_H__
  8. #define __LOLPHYSICS_H__
  9. #ifdef HAVE_PHYS_USE_BULLET
  10. #include <bullet/btBulletDynamicsCommon.h>
  11. #include <bullet/btBulletCollisionCommon.h>
  12. #include "LolBtPhysicsIntegration.h"
  13. #include "EasyPhysics.h"
  14. #endif
  15. namespace lol
  16. {
  17. namespace phys
  18. {
  19. class Simulation
  20. {
  21. public:
  22. Simulation() :
  23. m_broadphase(0),
  24. m_collision_configuration(0),
  25. m_dispatcher(0),
  26. m_solver(0),
  27. m_dynamics_world(0)
  28. {
  29. }
  30. ~Simulation()
  31. {
  32. ExitContext();
  33. }
  34. char const *GetName() { return "<Simulation>"; }
  35. #ifdef HAVE_PHYS_USE_BULLET
  36. void InitContext()
  37. {
  38. // Build the broadphase
  39. m_broadphase = new btDbvtBroadphase();
  40. // Set up the collision configuration and dispatcher
  41. m_collision_configuration = new btDefaultCollisionConfiguration();
  42. m_dispatcher = new btCollisionDispatcher(m_collision_configuration);
  43. // The actual physics solver
  44. m_solver = new btSequentialImpulseConstraintSolver;
  45. // The world.
  46. m_dynamics_world = new btDiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_solver, m_collision_configuration);
  47. }
  48. void SetContinuousDetection(bool ShouldUseCCD)
  49. {
  50. if (m_dynamics_world)
  51. m_dynamics_world->getDispatchInfo().m_useContinuous = ShouldUseCCD;
  52. }
  53. void SetGravity(vec3 &NewGravity)
  54. {
  55. if (m_dynamics_world)
  56. m_dynamics_world->setGravity(LOL2BT_VEC3(NewGravity));
  57. }
  58. void TickContext(float seconds)
  59. {
  60. //step the simulation
  61. if (m_dynamics_world)
  62. {
  63. int steps = (int)(seconds / 0.005f);
  64. for (int i = 0; i < steps; i++)
  65. m_dynamics_world->stepSimulation(seconds / steps);
  66. }
  67. }
  68. void ExitContext()
  69. {
  70. delete m_dynamics_world;
  71. delete m_solver;
  72. delete m_dispatcher;
  73. delete m_collision_configuration;
  74. delete m_broadphase;
  75. }
  76. btDiscreteDynamicsWorld* GetWorld()
  77. {
  78. return m_dynamics_world;
  79. }
  80. void AddToDynamic(EasyPhysics* dynamic_EP)
  81. {
  82. m_dynamic_list << dynamic_EP;
  83. }
  84. void AddToStatic(EasyPhysics* static_EP)
  85. {
  86. m_static_list << static_EP;
  87. }
  88. private:
  89. //broadphase
  90. btBroadphaseInterface* m_broadphase;
  91. // Set up the collision configuration and dispatc
  92. btDefaultCollisionConfiguration* m_collision_configuration;
  93. btCollisionDispatcher* m_dispatcher;
  94. // The actual physics solver
  95. btSequentialImpulseConstraintSolver* m_solver;
  96. // The world.
  97. btDiscreteDynamicsWorld* m_dynamics_world;
  98. //Easy Physics body List
  99. Array<EasyPhysics*> m_dynamic_list;
  100. Array<EasyPhysics*> m_static_list;
  101. #else
  102. void InitContext() { }
  103. void TickContext(float seconds) { }
  104. void ExitContext() { }
  105. #endif //HAVE_PHYS_USE_BULLET
  106. };
  107. } /* namespace phys */
  108. } /* namespace lol */
  109. #endif // __LOLPHYSICS_H__