Advertisement
Guest User

PhysicsSystem

a guest
May 19th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include "entityx/entityx.h"
  4.  
  5. #include "btBulletDynamicsCommon.h"
  6.  
  7. #include "components/PhysicsComponent.hpp"
  8.  
  9. #include "physics/MyDebugDrawer.hpp"
  10.  
  11. namespace ex = entityx;
  12.  
  13. namespace sw {
  14.  
  15. class PhysicsSystem : public ex::System<PhysicsSystem> {
  16. public:
  17.  
  18. PhysicsSystem() {
  19. // create the collision configuration
  20. m_pCollisionConfiguration = new btDefaultCollisionConfiguration();
  21.  
  22. // Default collision dispatcher
  23. m_pDispatcher = new btCollisionDispatcher(m_pCollisionConfiguration);
  24.  
  25. // create the broadphase
  26. m_pBroadphase = new btDbvtBroadphase();
  27.  
  28. // create the constraint solver
  29. m_pSolver = new btSequentialImpulseConstraintSolver();
  30.  
  31.  
  32. // create the world
  33. m_pWorld = new btDiscreteDynamicsWorld(m_pDispatcher, m_pBroadphase, m_pSolver, m_pCollisionConfiguration);
  34.  
  35. m_pWorld->setGravity(btVector3(0, -10, 0));
  36. }
  37.  
  38.  
  39. void populateWorld(ex::Entity current_entity) {
  40.  
  41. auto physicsComponent = current_entity.component<PhysicsComponent>();
  42. auto node = current_entity.component<GraphNodeComponent>();
  43.  
  44. if(physicsComponent) {
  45. physicsComponent->body_->setActivationState(DISABLE_DEACTIVATION);
  46. m_pWorld->addRigidBody(physicsComponent->body_);
  47. }
  48.  
  49. if(!node->children_.empty()) {
  50. for(ex::Entity child : node->children_) {
  51. populateWorld(child);
  52. }
  53. }
  54.  
  55. }
  56.  
  57.  
  58.  
  59. ~PhysicsSystem() {
  60. delete m_pWorld;
  61. delete debugDrawer_;
  62. delete m_pBroadphase;
  63. delete m_pDispatcher;
  64. delete m_pCollisionConfiguration;
  65. delete m_pSolver;
  66. }
  67.  
  68.  
  69. void update(entityx::EntityManager& entityManager, entityx::EventManager& eventManager, entityx::TimeDelta dt) {
  70. if (m_pWorld) {
  71. m_pWorld->stepSimulation(dt);
  72.  
  73. }
  74.  
  75. }
  76.  
  77.  
  78.  
  79. private:
  80.  
  81. btBroadphaseInterface *m_pBroadphase;
  82.  
  83. btCollisionDispatcher *m_pDispatcher;
  84.  
  85. btSequentialImpulseConstraintSolver *m_pSolver;
  86.  
  87. btDefaultCollisionConfiguration *m_pCollisionConfiguration;
  88.  
  89. btDynamicsWorld *m_pWorld;
  90.  
  91.  
  92. };
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement