Gamedev Framework (gf)  0.12.0
A C++14 framework for 2D games
PhysicsBody.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_PHYSICS_BODY_H
22 #define GF_PHYSICS_BODY_H
23 
24 #include <memory>
25 
26 #include "Circ.h"
27 #include "PhysicsGeometry.h"
28 #include "Polygon.h"
29 #include "Rect.h"
30 #include "Time.h"
31 #include "Transform.h"
32 #include "Vector.h"
33 
34 namespace gf {
35 #ifndef DOXYGEN_SHOULD_SKIP_THIS
36 inline namespace v1 {
37 #endif
38 
39  struct RenderStates;
40  class RenderTarget;
41  struct Penetration;
42 
56  class GF_API PhysicsBody {
57  public:
61  enum Type {
64  };
65 
72  PhysicsBody(const PhysicsGeometry& geometry, Type type = Dynamic);
73 
79  Type getType() const {
80  return m_type;
81  }
82 
88  void step(float dt);
89 
98  void render(RenderTarget& target, const RenderStates& states) const;
99 
111  return m_position;
112  }
113 
119  void setPosition(Vector2f position);
120 
126  void move(Vector2f offset);
127 
134  return m_linearVelocity;
135  }
136 
142  void setLinearVelocity(Vector2f velocity) {
143  m_linearVelocity = velocity;
144  }
145 
153  void applyLinearImpulse(Vector2f impulse);
154 
161  return m_acceleration;
162  }
163 
171  void applyForce(Vector2f force);
172 
178  float getAngle() const {
179  return m_angle;
180  }
181 
187  void setAngle(float angle) {
188  m_angle = angle;
189  }
190 
196  void turn(float arc) {
197  m_angle += arc;
198  }
199 
203  void setVelocityFromAngle();
204 
212  void updateTransform();
213 
220  const Transform& getTransform() const;
221 
222 
235  void setRestitution(float restitution) {
236  m_restitution = restitution;
237  }
238 
244  float getRestitution() const {
245  return m_restitution;
246  }
247 
253  void setStaticFriction(float friction) {
254  m_staticFriction = friction;
255  }
256 
262  float getStaticFriction() const {
263  return m_staticFriction;
264  }
265 
271  void setDynamicFriction(float friction) {
272  m_dynamicFriction = friction;
273  }
274 
280  float getDynamicFriction() const {
281  return m_dynamicFriction;
282  }
283 
289  void setLinearDamping(float damping) {
290  m_linearDamping = damping;
291  }
292 
298  float getLinearDamping() const {
299  return m_linearDamping;
300  }
301 
309  void setDensity(float density);
310 
318  float getInverseMass() const {
319  return m_inverseMass;
320  }
321 
330  bool collidesWith(const PhysicsBody& other, Penetration& p) const;
331 
332  private:
333  Type m_type;
334 
335  Vector2f m_position;
336  Vector2f m_linearVelocity;
337  Vector2f m_acceleration;
338 
339  float m_angle;
340 
341  float m_inverseMass;
342 
343  float m_restitution;
344  float m_staticFriction;
345  float m_dynamicFriction;
346  float m_linearDamping;
347 
348  Transform m_transform;
349 
350  const PhysicsGeometry& m_geometry;
351  };
352 
353 #ifndef DOXYGEN_SHOULD_SKIP_THIS
354 }
355 #endif
356 }
357 
358 #endif // GF_PHYSICS_BODY_H
Vector2f getLinearVelocity() const
Get the linear velocity of the body.
Definition: PhysicsBody.h:133
The row has a dynamic layout.
void setLinearDamping(float damping)
Set the linear damping of the body.
Definition: PhysicsBody.h:289
float getInverseMass() const
Get the inverse mass of the body.
Definition: PhysicsBody.h:318
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
Type getType() const
Get the type of the body.
Definition: PhysicsBody.h:79
Vector2f getAcceleration() const
Get the acceleration of the body.
Definition: PhysicsBody.h:160
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
A physics body.
Definition: PhysicsBody.h:56
A simple transformation (rotation then translation)
Definition: Transform.h:204
float getAngle() const
Get the angle of the body.
Definition: PhysicsBody.h:178
Type
Type of body.
Definition: PhysicsBody.h:61
float getDynamicFriction() const
Get the dynamic friction coefficient of the body.
Definition: PhysicsBody.h:280
float getLinearDamping() const
Get the linear damping of the body.
Definition: PhysicsBody.h:298
Static body with infinite mass.
Definition: PhysicsBody.h:62
The namespace for gf classes.
Definition: Action.h:35
float getStaticFriction() const
Get the static friction coefficient of the body.
Definition: PhysicsBody.h:262
float angle(Direction direction)
Get an angle from a direction.
void setRestitution(float restitution)
Set the restitution of the body of the body.
Definition: PhysicsBody.h:235
Vector2f getPosition() const
Get the position of the body.
Definition: PhysicsBody.h:110
Data about the collision between two objects.
Definition: Collision.h:43
void setAngle(float angle)
Set the angle of the body.
Definition: PhysicsBody.h:187
void turn(float arc)
Change the angle of the body.
Definition: PhysicsBody.h:196
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:271
float getRestitution() const
Get the restitution of the body of the body.
Definition: PhysicsBody.h:244
Dynamic bodies with finite mass.
Definition: PhysicsBody.h:63
void setStaticFriction(float friction)
Set the static friction coefficient of the body.
Definition: PhysicsBody.h:253
void setLinearVelocity(Vector2f velocity)
Set the linear velocity of the body.
Definition: PhysicsBody.h:142