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