galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
RigidBody.cpp
Go to the documentation of this file.
1
7
8#include <box2d/b2_fixture.h>
9#include <box2d/b2_polygon_shape.h>
10#include <magic_enum/magic_enum.hpp>
11#include <nlohmann/json.hpp>
12
13#include "galaxy/core/ServiceLocator.hpp"
14
15#include "RigidBody.hpp"
16
17namespace galaxy
18{
19 namespace components
20 {
22 : Serializable {}
23 , m_shape {0, 0}
24 , m_type {b2_staticBody}
25 , m_density {1.0f}
26 , m_friction {0.0f}
27 , m_restitution {0.0f}
28 , m_restitution_threshold {0.0f}
29 , m_bullet {false}
30 , m_fixed_rotation {true}
31 , m_body {nullptr}
32 , m_world {nullptr}
33 {
34 }
35
36 RigidBody::RigidBody(const nlohmann::json& json)
37 : Serializable {}
38 {
39 deserialize(json);
40 }
41
43 : Serializable {}
44 {
45 this->m_shape = std::move(rb.m_shape);
46 this->m_type = rb.m_type;
47 this->m_density = rb.m_density;
48 this->m_friction = rb.m_friction;
49 this->m_restitution = rb.m_restitution;
50 this->m_restitution_threshold = rb.m_restitution_threshold;
51 this->m_bullet = rb.m_bullet;
52 this->m_fixed_rotation = rb.m_fixed_rotation;
53 this->m_body = rb.m_body;
54 this->m_world = rb.m_world;
55
56 rb.m_body = nullptr;
57 rb.m_world = nullptr;
58 }
59
61 {
62 if (this != &rb)
63 {
64 this->m_shape = std::move(rb.m_shape);
65 this->m_type = rb.m_type;
66 this->m_density = rb.m_density;
67 this->m_friction = rb.m_friction;
68 this->m_restitution = rb.m_restitution;
69 this->m_restitution_threshold = rb.m_restitution_threshold;
70 this->m_bullet = rb.m_bullet;
71 this->m_fixed_rotation = rb.m_fixed_rotation;
72 this->m_body = rb.m_body;
73 this->m_world = rb.m_world;
74
75 rb.m_body = nullptr;
76 rb.m_world = nullptr;
77 }
78
79 return *this;
80 }
81
83 {
84 // b2Body cleaned up by entt.
85 }
86
87 void RigidBody::set_shape(const float hw, const float hh)
88 {
89 m_shape.x = hw;
90 m_shape.y = hh;
91
92 auto fixture = m_body->GetFixtureList();
93 auto shape = dynamic_cast<b2PolygonShape*>(fixture->GetShape());
94 shape->SetAsBox(m_shape.x, m_shape.y);
95 }
96
97 void RigidBody::set_type(const b2BodyType type)
98 {
99 m_body->SetType(type);
100 }
101
102 void RigidBody::set_density(const float density)
103 {
104 m_density = density;
105
106 auto fixture = m_body->GetFixtureList();
107 fixture->SetDensity(m_density);
108
109 m_body->ResetMassData();
110 }
111
112 void RigidBody::set_friction(const float friction)
113 {
114 m_friction = friction;
115
116 auto fixture = m_body->GetFixtureList();
117 fixture->SetFriction(m_friction);
118 }
119
120 void RigidBody::set_restitution(const float restitution)
121 {
122 m_restitution = restitution;
123
124 auto fixture = m_body->GetFixtureList();
125 fixture->SetRestitution(m_restitution);
126 }
127
128 void RigidBody::set_restitution_threshold(const float restitution_threshold)
129 {
130 m_restitution_threshold = restitution_threshold;
131
132 auto fixture = m_body->GetFixtureList();
133 fixture->SetRestitutionThreshold(m_restitution_threshold);
134 }
135
136 void RigidBody::set_bullet(const bool is_bullet)
137 {
138 m_body->SetBullet(is_bullet);
139 }
140
141 void RigidBody::set_fixed_rotation(const bool fixed_rotation)
142 {
143 m_body->SetFixedRotation(fixed_rotation);
144 }
145
146 const glm::vec2& RigidBody::get_shape() const
147 {
148 return m_shape;
149 }
150
151 b2BodyType RigidBody::get_type() const
152 {
153 return m_type;
154 }
155
157 {
158 return m_density;
159 }
160
162 {
163 return m_friction;
164 }
165
167 {
168 return m_restitution;
169 }
170
175
177 {
178 return m_bullet;
179 }
180
182 {
183 return m_fixed_rotation;
184 }
185
186 nlohmann::json RigidBody::serialize()
187 {
188 nlohmann::json json = "{}"_json;
189 json["shape"]["hw"] = m_shape.x;
190 json["shape"]["hh"] = m_shape.y;
191 json["type"] = magic_enum::enum_name(m_type);
192 json["density"] = m_density;
193 json["friction"] = m_friction;
194 json["restitution"] = m_restitution;
195 json["restitution_threshold"] = m_restitution_threshold;
196 json["bullet"] = m_bullet;
197 json["fixed_rotation"] = m_fixed_rotation;
198
199 return json;
200 }
201
202 void RigidBody::deserialize(const nlohmann::json& json)
203 {
204 const auto& shape = json.at("shape");
205 m_shape.x = shape.at("hw");
206 m_shape.y = shape.at("hh");
207
208 const auto type = magic_enum::enum_cast<b2BodyType>(json.at("type").get<std::string>());
209 if (type.has_value())
210 {
211 m_type = type.value();
212 }
213
214 m_density = json.at("density");
215 m_friction = json.at("friction");
216 m_restitution = json.at("restitution");
217 m_restitution_threshold = json.at("restitution_threshold");
218 m_bullet = json.at("bullet");
219 m_fixed_rotation = json.at("fixed_rotation");
220 }
221 } // namespace components
222} // namespace galaxy
Box2D physics body.
Definition RigidBody.hpp:34
float m_friction
How slippery it is.
float get_friction() const
Get body friction.
void set_friction(const float friction)
Set friction of body.
float m_restitution
How bouncy the fixture is.
b2BodyType m_type
Body type.
b2Body * m_body
Pointer to Box2D body.
bool m_fixed_rotation
Can this body rotate?
void set_type(const b2BodyType type)
Set body type.
Definition RigidBody.cpp:97
b2BodyType get_type() const
Get body type.
void set_restitution_threshold(const float restitution_threshold)
Set restitution threshold.
glm::vec2 m_shape
Box2D body shape.
void set_restitution(const float restitution)
Set restitution of body.
void set_fixed_rotation(const bool fixed_rotation)
Set rotation status of body.
void set_bullet(const bool is_bullet)
Set if body is a bullet type.
bool is_bullet() const
Is this a bullet style body.
const glm::vec2 & get_shape() const
Get body shape.
float get_density() const
Get body density.
void set_density(const float density)
Set body density.
float get_restitution_threshold() const
Get restitution threshold.
void deserialize(const nlohmann::json &json) override
Deserializes from object.
bool m_bullet
Is this a bullet? Does continous collision checking.
float m_density
How heavy it is in relation to its area.
float get_restitution() const
Get body restitution.
virtual ~RigidBody()
Destructor.
Definition RigidBody.cpp:82
b2World * m_world
Pointer to the Box2D world.
void set_shape(const float hw, const float hh)
Set new box2d collider shape.
Definition RigidBody.cpp:87
RigidBody & operator=(RigidBody &&)
Move assignment operator.
Definition RigidBody.cpp:60
float m_restitution_threshold
Restitution velocity threshold.
bool is_rotation_fixed() const
Get rotation status.
nlohmann::json serialize() override
Serializes object.
Timer.hpp galaxy.
Definition Async.hpp:17