Gamedev Framework (gf)  0.4.0
A C++11 framework for 2D games
PhysicsBody.h
1 #ifndef GF_PHYSICS_BODY_H
2 #define GF_PHYSICS_BODY_H
3 
4 #include <memory>
5 
6 #include "Circ.h"
7 #include "PhysicsGeometry.h"
8 #include "Polygon.h"
9 #include "Rect.h"
10 #include "Transform.h"
11 #include "Vector.h"
12 
13 namespace gf {
14 #ifndef DOXYGEN_SHOULD_SKIP_THIS
15 inline namespace v1 {
16 #endif
17 
18  class RenderTarget;
19  struct Penetration;
20 
21  /**
22  * @ingroup game
23  * @brief A physics body
24  *
25  * A body has several properties:
26  *
27  * - [restitution](https://en.wikipedia.org/wiki/Coefficient_of_restitution) (default: 0)
28  * - [static and dynamic friction](https://en.wikipedia.org/wiki/Friction#Coefficient_of_friction) (default: 0)
29  * - [linear damping](https://en.wikipedia.org/wiki/Damping_ratio) (default: 0)
30  * - [density](https://en.wikipedia.org/wiki/Density) (default: 1)
31  *
32  * @sa gf::PhysicsGeometry, gf::PhysicsModel
33  */
35  public:
36  /**
37  * @brief Type of body
38  */
39  enum Type {
40  Static, ///< Static body with infinite mass
41  Dynamic, ///< Dynamic bodies with finite mass
42  };
43 
44  /**
45  * @brief Constructor
46  *
47  * @param geometry The geometry of the body
48  * @param type The type of the body (default: dynamic)
49  */
50  PhysicsBody(const PhysicsGeometry& geometry, Type type = Dynamic);
51 
52  /**
53  * @brief Get the type of the body
54  *
55  * @returns The type of the body
56  */
57  Type getType() const {
58  return m_type;
59  }
60 
61  /**
62  * @brief Update the model's state
63  *
64  * @param dt The time (in seconds) since the last update
65  */
66  void update(float dt);
67 
68  /**
69  * @brief Render the body
70  *
71  * This function is for debugging purpose. The body is outlined in red.
72  *
73  * @param target The render target
74  */
75  void render(RenderTarget& target) const;
76 
77  /**
78  * @name Position, angle, velocity and acceleration
79  * @{
80  */
81 
82  /**
83  * @brief Get the position of the body
84  *
85  * @returns The current position of the body
86  */
87  Vector2f getPosition() const {
88  return m_position;
89  }
90 
91  /**
92  * @brief Set the position of the body
93  *
94  * @param position The new position of the body
95  */
96  void setPosition(Vector2f position);
97 
98  /**
99  * @brief Move the body
100  *
101  * @param offset The offset to apply to the position of the body
102  */
103  void move(Vector2f offset);
104 
105  /**
106  * @brief Get the linear velocity of the body
107  *
108  * @returns The current linear velocity
109  */
110  Vector2f getLinearVelocity() const {
111  return m_linearVelocity;
112  }
113 
114  /**
115  * @brief Set the linear velocity of the body
116  *
117  * @param velocity The new velocity
118  */
119  void setLinearVelocity(Vector2f velocity) {
120  m_linearVelocity = velocity;
121  }
122 
123  /**
124  * @brief Apply a linear impulse
125  *
126  * The impulse changes the velocity.
127  *
128  * @param impulse The impulse to apply to the velocity of the body
129  */
130  void applyLinearImpulse(Vector2f impulse);
131 
132  /**
133  * @brief Get the acceleration of the body
134  *
135  * @returns The current acceleration
136  */
137  Vector2f getAcceleration() const {
138  return m_acceleration;
139  }
140 
141  /**
142  * @brief Apply a force
143  *
144  * The force changes the acceleration (by Newton's law)
145  *
146  * @param force The force to apply to the acceleration of the body
147  */
148  void applyForce(Vector2f force);
149 
150  /**
151  * @brief Get the angle of the body
152  *
153  * @returns The angle of the body
154  */
155  float getAngle() const {
156  return m_angle;
157  }
158 
159  /**
160  * @brief Set the angle of the body
161  *
162  * @param angle The new angle
163  */
164  void setAngle(float angle) {
165  m_angle = angle;
166  }
167 
168  /**
169  * @brief Change the angle of the body
170  *
171  * @param arc The arc to add to the current angle
172  */
173  void turn(float arc) {
174  m_angle += arc;
175  }
176 
177  /**
178  * @brief Set the velocity direction from the angle of the body
179  */
180  void setVelocityFromAngle();
181 
182  /**
183  * @brief Update the internal transform
184  *
185  * Normally, you should not call this function.
186  *
187  * @sa getTransform()
188  */
189  void updateTransform();
190 
191  /**
192  * @brief Get the current transform
193  *
194  * @returns The current transform
195  * @sa updateTransform()
196  */
197  const Transform& getTransform() const;
198 
199 
200  /** @} */
201 
202  /**
203  * @name Body properties
204  * @{
205  */
206 
207  /**
208  * @brief Set the restitution of the body of the body
209  *
210  * @param restitution The new restitution
211  */
212  void setRestitution(float restitution) {
213  m_restitution = restitution;
214  }
215 
216  /**
217  * @brief Get the restitution of the body of the body
218  *
219  * @returns The current restitution
220  */
221  float getRestitution() const {
222  return m_restitution;
223  }
224 
225  /**
226  * @brief Set the static friction coefficient of the body
227  *
228  * @param friction The new friction coefficient
229  */
230  void setStaticFriction(float friction) {
231  m_staticFriction = friction;
232  }
233 
234  /**
235  * @brief Get the static friction coefficient of the body
236  *
237  * @returns The current friction coefficient
238  */
239  float getStaticFriction() const {
240  return m_staticFriction;
241  }
242 
243  /**
244  * @brief Set the dynamic friction coefficient of the body
245  *
246  * @param friction The new friction coefficient
247  */
248  void setDynamicFriction(float friction) {
249  m_dynamicFriction = friction;
250  }
251 
252  /**
253  * @brief Get the dynamic friction coefficient of the body
254  *
255  * @returns The current friction coefficient
256  */
257  float getDynamicFriction() const {
258  return m_dynamicFriction;
259  }
260 
261  /**
262  * @brief Set the linear damping of the body
263  *
264  * @param damping The new damping
265  */
266  void setLinearDamping(float damping) {
267  m_linearDamping = damping;
268  }
269 
270  /**
271  * @brief Get the linear damping of the body
272  *
273  * @returns The current damping
274  */
275  float getLinearDamping() const {
276  return m_linearDamping;
277  }
278 
279  /**
280  * @brief Set the density of the body
281  *
282  * This changes the mass of the body, according to its geometry.
283  *
284  * @param density The new density
285  */
286  void setDensity(float density);
287 
288  /**
289  * @brief Get the inverse mass of the body
290  *
291  * The inverse mass is zero for static objects.
292  *
293  * @returns The current inverse mass
294  */
295  float getInverseMass() const {
296  return m_inverseMass;
297  }
298 
299  /** @} */
300 
301  /**
302  * @brief Check if the body collides with another body
303  *
304  * @param other The other body
305  * @param p Data to fill if there is a collision
306  */
307  bool collidesWith(const PhysicsBody& other, Penetration& p) const;
308 
309  private:
310  Type m_type;
311 
312  Vector2f m_position;
313  Vector2f m_linearVelocity;
314  Vector2f m_acceleration;
315 
316  float m_angle;
317 
318  float m_inverseMass;
319 
320  float m_restitution;
321  float m_staticFriction;
322  float m_dynamicFriction;
323  float m_linearDamping;
324 
325  Transform m_transform;
326 
327  const PhysicsGeometry& m_geometry;
328  };
329 
330 #ifndef DOXYGEN_SHOULD_SKIP_THIS
331 }
332 #endif
333 }
334 
335 #endif // GF_PHYSICS_BODY_H
void move(Vector2f offset)
Move the body.
void setLinearDamping(float damping)
Set the linear damping of the body.
Definition: PhysicsBody.h:266
bool collidesWith(const PhysicsBody &other, Penetration &p) const
Check if the body collides with another body.
void setPosition(Vector2f position)
Set the position of the body.
Vector2f getPosition() const
Get the position of the body.
Definition: PhysicsBody.h:87
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:65
Dynamic bodies with finite mass.
Definition: PhysicsBody.h:41
PhysicsBody(const PhysicsGeometry &geometry, Type type=Dynamic)
Constructor.
A physics body.
Definition: PhysicsBody.h:34
void update(float dt)
Update the model's state.
A simple transformation (rotation then translation)
Definition: Transform.h:204
void render(RenderTarget &target) const
Render the body.
Type
Type of body.
Definition: PhysicsBody.h:39
void applyLinearImpulse(Vector2f impulse)
Apply a linear impulse.
Vector2f getAcceleration() const
Get the acceleration of the body.
Definition: PhysicsBody.h:137
float getDynamicFriction() const
Get the dynamic friction coefficient of the body.
Definition: PhysicsBody.h:257
void setVelocityFromAngle()
Set the velocity direction from the angle of the body.
Type getType() const
Get the type of the body.
Definition: PhysicsBody.h:57
float getInverseMass() const
Get the inverse mass of the body.
Definition: PhysicsBody.h:295
The namespace for gf classes.
Definition: Action.h:34
float getLinearDamping() const
Get the linear damping of the body.
Definition: PhysicsBody.h:275
void setRestitution(float restitution)
Set the restitution of the body of the body.
Definition: PhysicsBody.h:212
float getRestitution() const
Get the restitution of the body of the body.
Definition: PhysicsBody.h:221
Data about the collision between two objects.
Definition: Collision.h:43
void setAngle(float angle)
Set the angle of the body.
Definition: PhysicsBody.h:164
float getAngle() const
Get the angle of the body.
Definition: PhysicsBody.h:155
void turn(float arc)
Change the angle of the body.
Definition: PhysicsBody.h:173
The geometry of a physics body.
Definition: PhysicsGeometry.h:45
void setDynamicFriction(float friction)
Set the dynamic friction coefficient of the body.
Definition: PhysicsBody.h:248
#define GF_API
Definition: Portability.h:35
const Transform & getTransform() const
Get the current transform.
void updateTransform()
Update the internal transform.
float getStaticFriction() const
Get the static friction coefficient of the body.
Definition: PhysicsBody.h:239
Vector2f getLinearVelocity() const
Get the linear velocity of the body.
Definition: PhysicsBody.h:110
void setStaticFriction(float friction)
Set the static friction coefficient of the body.
Definition: PhysicsBody.h:230
void applyForce(Vector2f force)
Apply a force.
void setDensity(float density)
Set the density of the body.
void setLinearVelocity(Vector2f velocity)
Set the linear velocity of the body.
Definition: PhysicsBody.h:119
Static body with infinite mass.
Definition: PhysicsBody.h:40