hx3d  1
2D/3D Simple Game Framework
collider.hpp
1 /*
2  2D collider.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library 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
18  USA
19 */
20 
21 #ifndef HX3D_PHYSICS_2D_COLLIDER
22 #define HX3D_PHYSICS_2D_COLLIDER
23 
24 #include "hx3d/utils/ptr.hpp"
25 #include "hx3d/utils/object_map.hpp"
26 
27 #include "hx3d/physics/2d/mass.hpp"
28 #include "hx3d/physics/2d/material.hpp"
29 
30 #include <glm/glm.hpp>
31 
32 namespace hx3d {
33 namespace physics2d {
34 
35 struct Attractor;
36 
40 class Collider {
41 public:
42 
46  enum class Type {
48  Static,
50  Dynamic,
52  Kinematic
53  };
54 
58  enum class Shape {
60  Circle,
62  Polygon
63  };
64 
68  struct Definition {
69  Definition();
70 
72  float unitCoef;
73 
75  glm::vec2 position;
76 
79 
81  unsigned int mask;
83  unsigned int category;
84  };
85 
92  Collider(const Shape shapeType, const Type colliderType = Type::Dynamic);
93  virtual ~Collider();
94 
100  void useDefinition(const Definition& def);
101 
107  virtual void computeMass(float density) = 0;
108 
114  virtual void setOrientation(float angle) = 0;
115 
121  void applyForce(const glm::vec2& amount);
122 
129  void applyImpulse(const glm::vec2& amount, const glm::vec2& contact);
130 
136  void setDensity(float density);
137 
139 
141  glm::vec2 position;
143  glm::vec2 velocity;
145  glm::vec2 force;
146 
148  glm::vec2 gravityForce;
150  glm::vec2 gravityScale;
151 
155  float torque;
157  float orientation;
158 
161 
163  unsigned int mask;
165  unsigned int category;
166 
171 
176 
181 };
182 
183 } /* physics2d */
184 } /* hx3d */
185 
186 #endif
Physical material: friction & restitution.
Definition: material.hpp:30
Static: no forces or velocity.
Mass massData
Mass data.
Definition: collider.hpp:175
void applyForce(const glm::vec2 &amount)
Apply a force.
Definition: collider.cpp:57
void useDefinition(const Definition &def)
Apply a definition on the collider.
Definition: collider.cpp:50
unsigned int category
Category.
Definition: collider.hpp:83
bool fixedRotation
Fixed rotation ?
Definition: collider.hpp:160
void setDensity(float density)
Set collider density.
Definition: collider.cpp:66
glm::vec2 position
Position.
Definition: collider.hpp:141
unsigned int category
Category.
Definition: collider.hpp:165
Kinematic: no forces but velocity.
Type
Collider type.
Definition: collider.hpp:46
hx3d framework namespace
Definition: audio.hpp:26
Collider(const Shape shapeType, const Type colliderType=Type::Dynamic)
Create a collider.
Definition: collider.cpp:34
float orientation
Orientation.
Definition: collider.hpp:157
float unitCoef
Physical ratio.
Definition: collider.hpp:72
Map of whatever you want. Useful for user data.
Definition: object_map.hpp:34
Dynamic: forces and velocity.
glm::vec2 gravityForce
Gravity force.
Definition: collider.hpp:148
ObjectMap userData
User data.
Definition: collider.hpp:180
float angularVelocity
Angular velocity.
Definition: collider.hpp:153
glm::vec2 velocity
Velocity.
Definition: collider.hpp:143
virtual void computeMass(float density)=0
Compute the collider mass.
Shape
Collider shape.
Definition: collider.hpp:58
void applyImpulse(const glm::vec2 &amount, const glm::vec2 &contact)
Apply an impulse.
Definition: collider.cpp:61
glm::vec2 gravityScale
Gravity scale.
Definition: collider.hpp:150
unsigned int mask
Mask.
Definition: collider.hpp:163
virtual void setOrientation(float angle)=0
Set the collider orientation.
Physical collider.
Definition: collider.hpp:40
glm::vec2 force
Force.
Definition: collider.hpp:145
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34
Physical mass data.
Definition: mass.hpp:30
Material material
Material.
Definition: collider.hpp:173
Ptr< Attractor > currentAttractor
Current attractor.
Definition: collider.hpp:178