Platformer
Platformer
PhysicsComponent.hpp
1 #ifndef PHYSICS_COMPONENT_HPP
2 #define PHYSICS_COMPONENT_HPP
3 
4 #include "Component.hpp"
5 #include "Vector2.hpp"
6 
7 #include <Box2D/Box2D.h>
8 #include <vector>
9 
11 enum class BodyType { DYNAMIC, STATIC, KINEMATIC };
12 
14 struct b2BodyWrapper {
17  b2Body* body);
20 
22  b2Body* body;
23 };
24 
26 class PhysicsComponent : public Component {
27  public:
30  BodyType type,
32  std::vector<Vector2> points,
34  float32 density,
36  float32 friction,
38  float32 restitution,
40  bool managedByPhysics);
41 
44  BodyType type,
46  float32 width,
48  float32 height,
50  float32 density,
52  float32 friction,
54  float32 restitution,
56  bool managedByPhysics);
57 
58  void Init() override;
59 
60  void Update() override;
61 
63  void ApplyForce(
64  Vector2 force,
66  Vector2 point);
67 
69  void ApplyForceToCenter(
70  Vector2 force);
71 
77 
81  void SetLinearVelocity(
82  Vector2 linear);
83 
85  void SetActive(
86  bool active);
87 
88 
91  bool managed);
92 
93  private:
95  void SetupBodyWithShape(
96  BodyType type,
98  b2Shape* shape,
100  float32 density,
102  float32 friction,
104  float32 restitution);
105 
107  std::shared_ptr<b2BodyWrapper> body;
108 
111 
113  const float physicsScale = 200.0;
114 };
115 
116 #endif
b2Body * body
The Box2D body of this wrapper.
Definition: PhysicsComponent.hpp:22
const float physicsScale
The scale of the physics relative to the world size.
Definition: PhysicsComponent.hpp:113
void Update() override
Updates this component.
Definition: PhysicsComponent.cpp:69
void SetActive(bool active)
Sets the active state of the body of this physics component.
Definition: PhysicsComponent.cpp:94
void ApplyForceToCenter(Vector2 force)
Applies the given force to the center of this object.
Definition: PhysicsComponent.cpp:81
void Init() override
Initializes this component.
Definition: PhysicsComponent.cpp:64
void ApplyForce(Vector2 force, Vector2 point)
Applies the given force to the given point relative to this object.
Definition: PhysicsComponent.cpp:76
void SetLinearVelocity(Vector2 linear)
Sets the linear velocity of this object.
Definition: PhysicsComponent.cpp:90
void SetupBodyWithShape(BodyType type, b2Shape *shape, float32 density, float32 friction, float32 restitution)
Sets up the Box2D body of this physics component with the given characteristics.
Definition: PhysicsComponent.cpp:12
Basic class representation of a vector 2 with an x and y coordinate.
Definition: Vector2.hpp:8
Class representing a component of a GameObject.
Definition: Component.hpp:12
b2BodyWrapper(b2Body *body)
Constructs a Box2D body wrapper.
Definition: PhysicsComponent.cpp:6
~b2BodyWrapper()
Destructs a Box2D body wrapper.
Definition: PhysicsComponent.cpp:8
std::shared_ptr< b2BodyWrapper > body
The Box2D body of this physics component.
Definition: PhysicsComponent.hpp:107
Vector2 GetLinearVelocity()
Gets the linear velocity of this object.
Definition: PhysicsComponent.cpp:85
PhysicsComponent(BodyType type, std::vector< Vector2 > points, float32 density, float32 friction, float32 restitution, bool managedByPhysics)
Constructs a new physics component.
Definition: PhysicsComponent.cpp:37
A wrapper class for a Box2D Body.
Definition: PhysicsComponent.hpp:14
void SetManagedByPhysics(bool managed)
Sets whether this physics component is managed by physics.
Definition: PhysicsComponent.cpp:96
A component that generates physics for a GameObject.
Definition: PhysicsComponent.hpp:26
bool managedByPhysics
Whether or not this physics component is managed by physics.
Definition: PhysicsComponent.hpp:110