hx3d  1
2D/3D Simple Game Framework
world.hpp
1 /*
2  Physical world.
3 
4  Copyright (C) 2013 Randy Gaul http://RandyGaul.net
5  Copyright (C) 2015 Denis BOURGE
6 
7  Thanks to RandyGaul for his tutorial and source.
8  I've adapted RandyGaul's code to use with glm and my own code.
9  Then I've added some features, as the multi-gravity system.
10 
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 2.1 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
24  USA
25 */
26 
27 #ifndef HX3D_PHYSICS_2D_WORLD
28 #define HX3D_PHYSICS_2D_WORLD
29 
30 #include "hx3d/physics/2d/manifold.hpp"
31 #include "hx3d/physics/2d/collider.hpp"
32 #include "hx3d/physics/2d/collision_matrix.hpp"
33 
34 #include "hx3d/physics/2d/attractors/global_attractor.hpp"
35 #include "hx3d/physics/2d/attractors/point_attractor.hpp"
36 #include "hx3d/physics/2d/attractors/zone_attractor.hpp"
37 #include "hx3d/physics/2d/collision_listener.hpp"
38 
39 #include "hx3d/graphics/base_batch.hpp"
40 
41 #include <set>
42 
43 namespace hx3d {
44 
48 namespace physics2d {
49 
53 class World {
54 public:
62  World(const glm::vec2 globalGravity = {0, -9.81}, const unsigned int iterations = 10, const float physRatio = 10.f);
63 
69  void addAttractor(const Ptr<Attractor>& attractor);
70 
76  void addListener(const Ptr<CollisionListener>& listener);
77 
83  void addCollider(const Ptr<Collider>& collider);
84 
90  void removeCollider(const Ptr<Collider>& collider);
91 
97  void step(float dt = 1.f/60.f);
98 
104  void render(graphics::BaseBatch& batch);
105 
111  float getPhysRatio() const;
112 
119 
126 
127 private:
129  unsigned int _iterations;
131  float _physRatio;
132 
134  std::vector<Ptr<Attractor>> _attractors;
136  std::vector<Ptr<Collider>> _colliders;
138  std::vector<Ptr<CollisionListener>> _listeners;
140  std::vector<Manifold> _contacts;
141 
143  std::set<Manifold> _inContact;
145  std::set<Manifold> _inPrevContact;
146 
147  CollisionMatrix _collisionMatrix;
148 
155  void integrateForces(const Ptr<Collider>& c, float dt);
156 
163  void integrateVelocity(const Ptr<Collider>& c, float dt);
164 
172  bool prevContactExists(Manifold& m);
173 
177  void checkOldContacts();
178 };
179 
180 } /* physics2d */
181 } /* hx3d */
182 
183 #endif
void addListener(const Ptr< CollisionListener > &listener)
Add a collision listener.
Definition: world.cpp:27
World(const glm::vec2 globalGravity={0,-9.81}, const unsigned int iterations=10, const float physRatio=10.f)
Create a world with a global gravity, iterations and the physical ratio.
Definition: world.cpp:13
void addAttractor(const Ptr< Attractor > &attractor)
Add an attractor.
Definition: world.cpp:19
hx3d framework namespace
Definition: audio.hpp:26
CollisionMatrix & getCollisionMatrix()
Get the collision matrix.
Definition: world.cpp:238
void render(graphics::BaseBatch &batch)
Render a debug view of the physical simulation.
Definition: world.cpp:122
Collision matrix with masks and category.
Contact manifold definition.
Definition: manifold.hpp:33
Physical world. Manages colliders and attractors.
Definition: world.hpp:53
const Ptr< GlobalAttractor > getGlobalGravity()
Get the global attractor.
Definition: world.cpp:234
float getPhysRatio() const
Get the physical ratio.
Definition: world.cpp:230
Draw meshes and texts on screen.
Definition: base_batch.hpp:41
void removeCollider(const Ptr< Collider > &collider)
Remove a collider.
Definition: world.cpp:31
void step(float dt=1.f/60.f)
Step the physical simulation.
Definition: world.cpp:35
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34
void addCollider(const Ptr< Collider > &collider)
Add a collider.
Definition: world.cpp:23