i6engine  1.0
PhysicsManager.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_MODULES_PHYSICSMANAGER_H__
26 #define __I6ENGINE_MODULES_PHYSICSMANAGER_H__
27 
29 
31 
33 
34 #include "LinearMath/btAlignedObjectArray.h"
35 
36 class btBroadphaseInterface;
37 class btCollisionDispatcher;
38 class btCollisionShape;
39 class btCompoundShape;
40 class btDefaultCollisionConfiguration;
41 class btDiscreteDynamicsWorld;
42 class btDynamicsWorld;
43 class btMotionState;
44 class btRigidBody;
45 class btSequentialImpulseConstraintSolver;
46 class btTypedConstraint;
47 struct btOverlapFilterCallback;
48 
49 namespace i6e {
50 namespace modules {
51 
52  class DebugDrawer;
53  class PhysicsController;
54 
56  public:
60  static std::map<std::pair<PhysicsNode *, PhysicsNode *>, uint64_t> _collisionPairs;
61 
65  static uint64_t _tickCount;
66 
70  explicit PhysicsManager(PhysicsController * pc);
71 
76 
80  void NewsCreate(const api::GameMessage::Ptr & msg);
81 
85  void NewsUpdate(const api::GameMessage::Ptr & msg);
86 
90  void NewsDelete(const api::GameMessage::Ptr & msg);
91 
95  void Tick();
96 
100  btCompoundShape * createCompound();
101 
107  btCollisionShape * createSphere(const api::attributeMap & params);
108 
114  btCollisionShape * createFromFile(const api::attributeMap & params);
115 
121  btCollisionShape * createBox(const api::attributeMap & params);
122 
128  btCollisionShape * createPlane(const api::attributeMap & params);
129 
133  btCollisionShape * createEmptyShape();
134 
139  void remove(btRigidBody * body);
140  void removeShape(btCollisionShape * shape);
141 
146  btDynamicsWorld * getPhysicsWorld() const;
147 
152  void setGravity(const Vec3 & gravity);
153 
158  void setGravity(const double gravityDown);
159 
163  void addRigidBody(btRigidBody * rigidBody);
164 
168  void removeRigidBody(btRigidBody * rigidBody);
169 
173  PhysicsNode * getPhysicsNode(const int64_t id);
174 
178  void addTicker(PhysicsNode * pn);
179 
183  void removeTicker(PhysicsNode * pn);
184 
185  private:
186  PhysicsController * _ctrl;
187 
191  btAlignedObjectArray<btCollisionShape *> _collisionShapes;
192 
193  btDiscreteDynamicsWorld * _dynamicsWorld;
194  btBroadphaseInterface * _broadphase;
195  btCollisionDispatcher * _dispatcher;
196  btDefaultCollisionConfiguration * _collisionConfiguration;
197  btSequentialImpulseConstraintSolver * _solver;
198 
199  std::map<int64_t, PhysicsNode *> _nodes;
200 
201  // http://www.bulletphysics.org/mediawiki-1.5.8/index.php/Collision_Things
202  uint64_t _lngTime;
203 
204  std::vector<PhysicsNode *> _tickList;
205 
206  std::map<int64_t, std::vector<std::pair<int64_t, btTypedConstraint *>>> _constraints;
207 
211  bool _paused;
212 
213  DebugDrawer * _debugDrawer;
214 
215  btOverlapFilterCallback * _filterCallback;
216 
220  PhysicsManager(const PhysicsManager &) = delete;
221 
225  PhysicsManager & operator=(const PhysicsManager &) = delete;
226 
228  };
229 
230 } /* namespace modules */
231 } /* namespace i6e */
232 
233 #endif /* __I6ENGINE_MODULES_PHYSICSMANAGER_H__ */
234 
void setGravity(const Vec3 &gravity)
Sets the gravity of the world.
btCollisionShape * createFromFile(const api::attributeMap &params)
Creates a new RigidBody from a collisionShape from a file.
void NewsUpdate(const api::GameMessage::Ptr &msg)
handles update messages for the PhysicsManager
btCollisionShape * createPlane(const api::attributeMap &params)
Creates a new RigidBody with a planes collisionShape.
void Tick()
Ticks physics stuff.
void removeRigidBody(btRigidBody *rigidBody)
Removes the body from the world to stop simulation.
boost::shared_ptr< Message > Ptr
Definition: Message.h:66
std::map< std::string, std::string > attributeMap
Definition: attributeMap.h:42
Implements 3-dimensional vectors.
Definition: i6eVector.h:48
Controls the physics world.
PhysicsManager(PhysicsController *pc)
constructor
btDynamicsWorld * getPhysicsWorld() const
static uint64_t _tickCount
counts ticks for this subsystem
void NewsCreate(const api::GameMessage::Ptr &msg)
handles create messages for the PhysicsManager
#define ASSERT_THREAD_SAFETY_HEADER
btCollisionShape * createEmptyShape()
creates an empty default shape
PhysicsNode * getPhysicsNode(const int64_t id)
returns the PhysicsNode with the given ID
btCollisionShape * createSphere(const api::attributeMap &params)
Creates a new RigidBody with a sphere collisionShape.
void addTicker(PhysicsNode *pn)
adds a ticking PhysicsNode
void NewsDelete(const api::GameMessage::Ptr &msg)
handles delete messages for the PhysicsManager
Represents the physical state of an object simulated by the Bullet library. Contains the btTransform ...
Definition: PhysicsNode.h:81
btCompoundShape * createCompound()
Creates a new CompoundShape.
void removeTicker(PhysicsNode *pn)
removes a ticking PhysicsNode
btCollisionShape * createBox(const api::attributeMap &params)
Creates a new RigidBody with a box collisionShape.
void removeShape(btCollisionShape *shape)
void addRigidBody(btRigidBody *rigidBody)
Adds the body to the world for simulation.
static std::map< std::pair< PhysicsNode *, PhysicsNode * >, uint64_t > _collisionPairs
stores all collision pairs