Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12 роки тому
12 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. //broadphase
  93. btBroadphaseInterface* m_broadphase;
  94. btAxisSweep3* m_Sweep_broadphase;
  95. // Set up the collision configuration and dispatc
  96. btDefaultCollisionConfiguration* m_collision_configuration;
  97. btCollisionDispatcher* m_dispatcher;
  98. // The actual physics solver
  99. btSequentialImpulseConstraintSolver* m_solver;
  100. // The world.
  101. btDiscreteDynamicsWorld* m_dynamics_world;
  102. #else // NO PHYSIC IMPLEMENTATION
  103. public:
  104. void Init() { }
  105. void TickGame(float seconds) { }
  106. void Exit() { }
  107. private:
  108. void CustomSetContinuousDetection(bool ShouldUseCCD) { }
  109. void CustomSetGravity(vec3 &NewGravity) { }
  110. void CustomSetWorldLimit(vec3 &NewWorldMin, vec3 &NewWorldMax) { }
  111. void CustomSetTimestep(float NewTimestep) { }
  112. #endif // PHYSIC IMPLEMENTATION
  113. public:
  114. //Main logic :
  115. //The Set*() functions do the all-lib-independent data storage.
  116. //And then it calls the CustomSet*() which are the specialized versions.
  117. //Sets the continuous collision detection flag.
  118. void SetContinuousDetection(bool ShouldUseCCD)
  119. {
  120. m_using_CCD = ShouldUseCCD;
  121. CustomSetContinuousDetection(ShouldUseCCD);
  122. }
  123. //Sets the simulation gravity.
  124. void SetGravity(vec3 &NewGravity)
  125. {
  126. m_gravity = NewGravity;
  127. CustomSetGravity(NewGravity);
  128. }
  129. //Sets the simulation gravity.
  130. void SetWorldLimit(vec3 const &NewWorldMin, vec3 const &NewWorldMax)
  131. {
  132. m_world_min = NewWorldMin;
  133. m_world_max = NewWorldMax;
  134. CustomSetWorldLimit(NewWorldMin, NewWorldMax);
  135. }
  136. //Sets the simulation fixed timestep.
  137. void SetTimestep(float NewTimestep)
  138. {
  139. if (NewTimestep > .0f)
  140. {
  141. m_timestep = NewTimestep;
  142. CustomSetTimestep(NewTimestep);
  143. }
  144. }
  145. private:
  146. friend class EasyPhysic;
  147. friend class EasyConstraint;
  148. //Adds the given EasyPhysic to the correct list.
  149. void AddToDynamic(EasyPhysic* NewEPDynamic) { m_dynamic_list << NewEPDynamic; }
  150. void AddToStatic(EasyPhysic* NewEPStatic) { m_static_list << NewEPStatic; }
  151. void AddToGhost(EasyPhysic* NewEPGhost) { m_ghost_list << NewEPGhost; }
  152. void AddToConstraint(EasyConstraint* NewEC) { m_constraint_list << NewEC; }
  153. //Easy Physics body List
  154. Array<EasyPhysic*> m_dynamic_list;
  155. Array<EasyPhysic*> m_static_list;
  156. Array<EasyPhysic*> m_ghost_list;
  157. Array<EasyConstraint*> m_constraint_list;
  158. //Easy Physics data storage
  159. float m_timestep;
  160. bool m_using_CCD;
  161. vec3 m_gravity;
  162. vec3 m_world_min;
  163. vec3 m_world_max;
  164. };
  165. } /* namespace phys */
  166. } /* namespace lol */
  167. #endif // __LOLPHYSICS_H__