Tinman  1.0
physics.h
Go to the documentation of this file.
1 // Tinman author: Isaac Lacoba Molina
2 // Copyright (C) 2014 ISAAC LACOBA MOLINA
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #ifndef PHYSICS_H
19 #define PHYSICS_H
20 #include <OgreSceneNode.h>
21 #include <btBulletDynamicsCommon.h>
22 
23 #include "motionstate.h"
24 #include "meshstrider.h"
25 
26 float radians_to_angle(float radians);
27 float degree_to_radians(float degrees);
28 
29 class Physics {
30  public:
31  typedef std::pair<const btCollisionObject*, const btCollisionObject*> CollisionPair;
32  typedef std::map<CollisionPair, std::function<void()>> Triggers;
33  typedef std::shared_ptr<Physics> shared;
34  btDiscreteDynamicsWorld* dynamics_world_;
35 
36  const btScalar PI = 3.14159265359;
37 
38  Physics();
39  virtual ~Physics();
40  btRigidBody* create_rigid_body(const btTransform &world_transform,
41  Ogre::SceneNode* node,btCollisionShape* shape, btScalar mass);
42  void add_rigid_body(btRigidBody* body_);
43  btRigidBody* create_rigid_body(const btTransform &world_transform,
44  btCollisionShape* shape, btScalar mass);
45  void remove_rigid_body(btRigidBody* body);
46 
47  btCollisionShape* create_shape(btVector3 halfExtent);
48  btCollisionShape* create_shape(float radius);
49  btCollisionShape* create_shape(MeshStrider* strider);
50  btCollisionShape* create_shape(btVector3 coordinates, btScalar distance_to_origin);
51  btCompoundShape* create_compound_shape(btVector3 origin, btCollisionShape* child);
52 
53  void step_simulation(float deltaT, int maxSubSteps);
54 
55  void load_mesh(std::string file);
56  void check_collision();
57 
58  void clear_triggers();
59  void add_collision_hooks(CollisionPair key, std::function<void()> callback);
60 
61  void set_position(btRigidBody* body, btVector3 new_position);
62  void set_transform(btRigidBody* body, btTransform transform);
63 
64 
65  btCollisionWorld::ClosestRayResultCallback raytest(btVector3 position,
66  btVector3 next_destination);
67  void print_vector(std::string message, btVector3 vector3);
68  btVector3 rotate_vector(btVector3 vector, btVector3 axis, btScalar angle);
69 
70  btScalar get_angle(btVector3 origin, btVector3 destiny);
71 
72  private:
73  const btVector3 gravity_ = btVector3(0, -40, 0);
74 
75  btBroadphaseInterface* broadphase_;
76  btSequentialImpulseConstraintSolver* solver_;
77  btDefaultCollisionConfiguration* collision_configuration_;
78  btCollisionDispatcher* dispatcher_;
79 
80  Triggers triggers_;
81 };
82 
83 #endif
void remove_rigid_body(btRigidBody *body)
Definition: physics.cpp:92
void clear_triggers()
Definition: physics.cpp:165
btCollisionWorld::ClosestRayResultCallback raytest(btVector3 position, btVector3 next_destination)
Definition: physics.cpp:182
btBroadphaseInterface * broadphase_
Definition: physics.h:75
Physics()
Definition: physics.cpp:20
btCollisionDispatcher * dispatcher_
Definition: physics.h:78
std::shared_ptr< Physics > shared
Definition: physics.h:33
btSequentialImpulseConstraintSolver * solver_
Definition: physics.h:76
void print_vector(std::string message, btVector3 vector3)
Definition: physics.cpp:190
btCollisionShape * create_shape(btVector3 halfExtent)
Definition: physics.cpp:97
const btScalar PI
Definition: physics.h:36
btVector3 rotate_vector(btVector3 vector, btVector3 axis, btScalar angle)
Definition: physics.cpp:198
void set_position(btRigidBody *body, btVector3 new_position)
Definition: physics.cpp:170
btScalar get_angle(btVector3 origin, btVector3 destiny)
Definition: physics.cpp:203
btDiscreteDynamicsWorld * dynamics_world_
Definition: physics.h:34
void load_mesh(std::string file)
btDefaultCollisionConfiguration * collision_configuration_
Definition: physics.h:77
virtual ~Physics()
Definition: physics.cpp:37
void add_collision_hooks(CollisionPair key, std::function< void()> callback)
Definition: physics.cpp:157
Triggers triggers_
Definition: physics.h:80
Shares vertices/indexes between Ogre and Bullet.
Definition: meshstrider.h:12
Definition: physics.h:29
btRigidBody * create_rigid_body(const btTransform &world_transform, Ogre::SceneNode *node, btCollisionShape *shape, btScalar mass)
Definition: physics.cpp:46
void check_collision()
Definition: physics.cpp:136
float radians_to_angle(float radians)
Definition: physics.cpp:212
void set_transform(btRigidBody *body, btTransform transform)
Definition: physics.cpp:177
std::pair< const btCollisionObject *, const btCollisionObject * > CollisionPair
Definition: physics.h:31
void step_simulation(float deltaT, int maxSubSteps)
Definition: physics.cpp:130
float degree_to_radians(float degrees)
Definition: physics.cpp:217
btCompoundShape * create_compound_shape(btVector3 origin, btCollisionShape *child)
Definition: physics.cpp:118
const btVector3 gravity_
Definition: physics.h:73
std::map< CollisionPair, std::function< void()> > Triggers
Definition: physics.h:32
void add_rigid_body(btRigidBody *body_)
Definition: physics.cpp:87