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.
 
 
 

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