選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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