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.
 
 
 

179 lines
4.0 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 : public Entity
  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. m_timestep(1.f/60.f)
  29. {
  30. }
  31. ~Simulation()
  32. {
  33. Exit();
  34. }
  35. char const *GetName() { return "<Simulation>"; }
  36. #ifdef HAVE_PHYS_USE_BULLET
  37. public:
  38. void Init()
  39. {
  40. // Build the broadphase
  41. m_broadphase = new btDbvtBroadphase();
  42. // Set up the collision configuration and dispatcher
  43. m_collision_configuration = new btDefaultCollisionConfiguration();
  44. m_dispatcher = new btCollisionDispatcher(m_collision_configuration);
  45. // The actual physics solver
  46. m_solver = new btSequentialImpulseConstraintSolver;
  47. // The world.
  48. m_dynamics_world = new btDiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_solver, m_collision_configuration);
  49. }
  50. virtual void TickGame(float seconds)
  51. {
  52. //step the simulation
  53. if (m_dynamics_world)
  54. {
  55. //the "+1" is to have at least one Timestep and to ensure float to int .5f conversion.
  56. int steps = (int)(seconds / m_timestep) + 1;
  57. m_dynamics_world->stepSimulation(seconds, steps, m_timestep);
  58. }
  59. }
  60. void Exit()
  61. {
  62. delete m_dynamics_world;
  63. delete m_solver;
  64. delete m_dispatcher;
  65. delete m_collision_configuration;
  66. delete m_broadphase;
  67. }
  68. btDiscreteDynamicsWorld* GetWorld()
  69. {
  70. return m_dynamics_world;
  71. }
  72. private:
  73. void CustomSetContinuousDetection(bool ShouldUseCCD)
  74. {
  75. if (m_dynamics_world)
  76. m_dynamics_world->getDispatchInfo().m_useContinuous = ShouldUseCCD;
  77. }
  78. void CustomSetGravity(vec3 &NewGravity)
  79. {
  80. if (m_dynamics_world)
  81. m_dynamics_world->setGravity(LOL2BT_VEC3(NewGravity * LOL2BT_UNIT));
  82. }
  83. void CustomSetTimestep(float NewTimestep) { }
  84. //broadphase
  85. btBroadphaseInterface* m_broadphase;
  86. // Set up the collision configuration and dispatc
  87. btDefaultCollisionConfiguration* m_collision_configuration;
  88. btCollisionDispatcher* m_dispatcher;
  89. // The actual physics solver
  90. btSequentialImpulseConstraintSolver* m_solver;
  91. // The world.
  92. btDiscreteDynamicsWorld* m_dynamics_world;
  93. #else // NO PHYSIC IMPLEMENTATION
  94. public:
  95. void Init() { }
  96. void TickGame(float seconds) { }
  97. void Exit() { }
  98. private:
  99. void CustomSetContinuousDetection(bool ShouldUseCCD) { }
  100. void CustomSetGravity(vec3 &NewGravity) { }
  101. void CustomSetTimestep(float NewTimestep) { }
  102. #endif // PHYSIC IMPLEMENTATION
  103. public:
  104. //Main logic :
  105. //The Set*() functions do the all-lib-independent data storage.
  106. //And then it calls the CustomSet*() which are the specialized versions.
  107. //Sets the continuous collision detection flag.
  108. void SetContinuousDetection(bool ShouldUseCCD)
  109. {
  110. m_using_CCD = ShouldUseCCD;
  111. CustomSetContinuousDetection(ShouldUseCCD);
  112. }
  113. //Sets the simulation gravity.
  114. void SetGravity(vec3 &NewGravity)
  115. {
  116. m_gravity = NewGravity;
  117. CustomSetGravity(NewGravity);
  118. }
  119. //Sets the simulation fixed timestep.
  120. void SetTimestep(float NewTimestep)
  121. {
  122. if (NewTimestep > .0f)
  123. {
  124. m_timestep = NewTimestep;
  125. CustomSetTimestep(NewTimestep);
  126. }
  127. }
  128. private:
  129. friend class EasyPhysic;
  130. friend class EasyConstraint;
  131. //Adds the given EasyPhysic to the correct list.
  132. void AddToDynamic(EasyPhysic* NewEPDynamic) { m_dynamic_list << NewEPDynamic; }
  133. void AddToStatic(EasyPhysic* NewEPStatic) { m_static_list << NewEPStatic; }
  134. void AddToConstraint(EasyConstraint* NewEC) { m_constraint_list << NewEC; }
  135. //Easy Physics body List
  136. Array<EasyPhysic*> m_dynamic_list;
  137. Array<EasyPhysic*> m_static_list;
  138. Array<EasyConstraint*> m_constraint_list;
  139. //Easy Physics data storage
  140. float m_timestep;
  141. bool m_using_CCD;
  142. vec3 m_gravity;
  143. };
  144. } /* namespace phys */
  145. } /* namespace lol */
  146. #endif // __LOLPHYSICS_H__