Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

126 rader
2.6 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 SetGravity(vec3 &NewGravity)
  49. {
  50. if (m_dynamics_world)
  51. m_dynamics_world->setGravity(LOL2BT_VEC3(NewGravity));
  52. }
  53. void TickContext(float seconds)
  54. {
  55. //step the simulation
  56. if (m_dynamics_world)
  57. {
  58. int steps = (int)(seconds / 0.005f);
  59. for (int i = 0; i < steps; i++)
  60. m_dynamics_world->stepSimulation(seconds / steps);
  61. }
  62. }
  63. void ExitContext()
  64. {
  65. delete m_dynamics_world;
  66. delete m_solver;
  67. delete m_dispatcher;
  68. delete m_collision_configuration;
  69. delete m_broadphase;
  70. }
  71. btDiscreteDynamicsWorld* GetWorld()
  72. {
  73. return m_dynamics_world;
  74. }
  75. void AddToDynamic(EasyPhysics* dynamic_EP)
  76. {
  77. m_dynamic_list << dynamic_EP;
  78. }
  79. void AddToStatic(EasyPhysics* static_EP)
  80. {
  81. m_static_list << static_EP;
  82. }
  83. private:
  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. //Easy Physics body List
  94. Array<EasyPhysics*> m_dynamic_list;
  95. Array<EasyPhysics*> m_static_list;
  96. #else
  97. void InitContext() { }
  98. void TickContext(float seconds) { }
  99. void ExitContext() { }
  100. #endif //HAVE_PHYS_USE_BULLET
  101. };
  102. } /* namespace phys */
  103. } /* namespace lol */
  104. #endif // __LOLPHYSICS_H__