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.

123 rader
2.5 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(NULL),
  24. m_collision_configuration(NULL),
  25. m_dispatcher(NULL),
  26. m_solver(NULL),
  27. m_dynamics_world(NULL)
  28. {
  29. }
  30. ~Simulation() { }
  31. char const *GetName() { return "<Simulation>"; }
  32. #ifdef HAVE_PHYS_USE_BULLET
  33. void InitContext()
  34. {
  35. // Build the broadphase
  36. m_broadphase = new btDbvtBroadphase();
  37. // Set up the collision configuration and dispatcher
  38. m_collision_configuration = new btDefaultCollisionConfiguration();
  39. m_dispatcher = new btCollisionDispatcher(m_collision_configuration);
  40. // The actual physics solver
  41. m_solver = new btSequentialImpulseConstraintSolver;
  42. // The world.
  43. m_dynamics_world = new btDiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_solver, m_collision_configuration);
  44. }
  45. void SetGravity(vec3 &NewGravity)
  46. {
  47. if (m_dynamics_world)
  48. m_dynamics_world->setGravity(LOL2BT_VEC3(NewGravity));
  49. }
  50. void TickContext(float seconds)
  51. {
  52. //step the simulation
  53. if (m_dynamics_world)
  54. {
  55. int steps = (int)(seconds / 0.005f);
  56. for (int i = 0; i < steps; i++)
  57. m_dynamics_world->stepSimulation(seconds / steps);
  58. }
  59. }
  60. void ExitContext()
  61. {
  62. delete m_broadphase;
  63. delete m_collision_configuration;
  64. delete m_dispatcher;
  65. delete m_solver;
  66. delete m_dynamics_world;
  67. }
  68. btDiscreteDynamicsWorld* GetWorld()
  69. {
  70. return m_dynamics_world;
  71. }
  72. void AddToDynamic(EasyPhysics* dynamic_EP)
  73. {
  74. m_dynamic_list << dynamic_EP;
  75. }
  76. void AddToStatic(EasyPhysics* static_EP)
  77. {
  78. m_static_list << static_EP;
  79. }
  80. private:
  81. //broadphase
  82. btBroadphaseInterface* m_broadphase;
  83. // Set up the collision configuration and dispatc
  84. btDefaultCollisionConfiguration* m_collision_configuration;
  85. btCollisionDispatcher* m_dispatcher;
  86. // The actual physics solver
  87. btSequentialImpulseConstraintSolver* m_solver;
  88. // The world.
  89. btDiscreteDynamicsWorld* m_dynamics_world;
  90. //Easy Physics body List
  91. Array<EasyPhysics*> m_dynamic_list;
  92. Array<EasyPhysics*> m_static_list;
  93. #else
  94. void InitContext() { }
  95. void TickContext(float seconds) { }
  96. void ExitContext() { }
  97. #endif //HAVE_PHYS_USE_BULLET
  98. };
  99. } /* namespace phys */
  100. } /* namespace lol */
  101. #endif // __LOLPHYSICS_H__