Gamedev Framework (gf)  0.4.0
A C++11 framework for 2D games
PhysicsModel.h
1 #ifndef GF_PHYSICS_MODEL_H
2 #define GF_PHYSICS_MODEL_H
3 
4 #include <vector>
5 
6 #include "Model.h"
7 #include "Vector.h"
8 
9 namespace gf {
10 #ifndef DOXYGEN_SHOULD_SKIP_THIS
11 inline namespace v1 {
12 #endif
13 
14  class PhysicsBody;
15 
16  /**
17  * @ingroup game
18  * @brief A model for physics simulation
19  *
20  * @sa gf::PhysicsBody, gf::FixedTimestepModel
21  */
22  class GF_API PhysicsModel : public gf::Model {
23  public:
24  /**
25  * @brief Constructor
26  *
27  * @param gravity The gravity of the simulation (default: no gravity)
28  */
29  PhysicsModel(Vector2f gravity = { 0.0f, 0.0f });
30 
31  /**
32  * @brief Set the gravity of the simulation
33  *
34  * @param gravity The new gravity
35  */
36  void setGravity(Vector2f gravity) noexcept {
37  m_gravity = gravity;
38  }
39 
40  /**
41  * @brief Get the gravity of the simulation
42  *
43  * @returns The current gravity
44  */
45  Vector2f getGravity() const noexcept {
46  return m_gravity;
47  }
48 
49  /**
50  * @brief Add a body to the simulation
51  *
52  * @param body A physics body
53  */
54  void addBody(PhysicsBody& body);
55 
56  /**
57  * @brief Remove a body from the simulation
58  *
59  * @param body A physics body
60  */
61  void removeBody(PhysicsBody& body);
62 
63  /**
64  * @brief Remove all bodies from the simulation
65  */
66  void clear();
67 
68  virtual void update(float dt) override;
69 
70  private:
71  Vector2f m_gravity;
72  std::vector<PhysicsBody*> m_staticBodies;
73  std::vector<PhysicsBody*> m_dynamicBodies;
74  };
75 
76 #ifndef DOXYGEN_SHOULD_SKIP_THIS
77 }
78 #endif
79 }
80 
81 #endif // GF_PHYSICS_MODEL_H
void clear()
Remove all bodies from the simulation.
PhysicsModel(Vector2f gravity={0.0f, 0.0f})
Constructor.
A physics body.
Definition: PhysicsBody.h:34
constexpr Vector(T x, T y)
Constructor that takes 2 components.
Definition: Vector.h:348
virtual void update(float dt) override
Update the model's state.
A game object that can be updated.
Definition: Model.h:45
void setGravity(Vector2f gravity) noexcept
Set the gravity of the simulation.
Definition: PhysicsModel.h:36
The namespace for gf classes.
Definition: Action.h:34
A model for physics simulation.
Definition: PhysicsModel.h:22
void addBody(PhysicsBody &body)
Add a body to the simulation.
Vector2f getGravity() const noexcept
Get the gravity of the simulation.
Definition: PhysicsModel.h:45
#define GF_API
Definition: Portability.h:35
void removeBody(PhysicsBody &body)
Remove a body from the simulation.