Unravel Engine C++ Reference
Loading...
Searching...
No Matches
physics_component.cpp
Go to the documentation of this file.
1#include "physics_component.h"
2#include <cstdint>
5
6namespace unravel
7{
8
9void physics_component::on_create_component(entt::registry& r, entt::entity e)
10{
11 entt::handle entity(r, e);
12
13 auto& component = entity.get<physics_component>();
14 component.set_owner(entity);
15 component.dirty_.set();
16 component.dirty_properties_.set();
17}
18
19void physics_component::on_destroy_component(entt::registry& r, entt::entity e)
20{
21}
22
24{
25 if(is_kinematic_ == kinematic)
26 {
27 return;
28 }
29
30 is_kinematic_ = kinematic;
31
32 on_change_kind();
33}
34auto physics_component::is_kinematic() const noexcept -> bool
35{
36 return is_kinematic_;
37}
38
39void physics_component::on_change_kind()
40{
41 dirty_.set();
43}
44
46{
47 is_autoscaled_ = autoscaled;
48}
49
50auto physics_component::is_autoscaled() const noexcept -> bool
51{
52 return is_autoscaled_;
53}
54
56{
57 if(is_using_gravity_ == use_gravity)
58 {
59 return;
60 }
61
62 is_using_gravity_ = use_gravity;
63
64 on_change_gravity();
65}
66
67auto physics_component::is_using_gravity() const noexcept -> bool
68{
69 return is_using_gravity_;
70}
71
72void physics_component::on_change_gravity()
73{
74 dirty_.set();
76}
77
79{
80 if(math::epsilonEqual(mass_, mass, math::epsilon<float>()))
81 {
82 return;
83 }
84
85 // if(mass <= EDYN_EPSILON && mass >= edyn::large_scalar)
86 // {
87 // return;
88 // }
89
90 mass_ = mass;
91
92 on_change_mass();
93}
94auto physics_component::get_mass() const noexcept -> float
95{
96 return mass_;
97}
98
99void physics_component::on_change_mass()
100{
101 dirty_.set();
103}
104
106{
107 if(is_sensor_ == sensor)
108 {
109 return;
110 }
111
112 is_sensor_ = sensor;
113
114 on_change_sensor();
115}
116auto physics_component::is_sensor() const noexcept -> bool
117{
118 return is_sensor_;
119}
120
121void physics_component::on_change_sensor()
122{
123 dirty_.set();
125}
126
127auto physics_component::is_dirty(uint8_t id) const noexcept -> bool
128{
129 return dirty_[id];
130}
131
132void physics_component::set_dirty(uint8_t id, bool dirty) noexcept
133{
134 dirty_.set(id, dirty);
135
136 if(!dirty)
137 {
138 dirty_properties_ = {};
139 }
140}
141
143{
144 return dirty_properties_[static_cast<std::underlying_type_t<physics_property>>(prop)];
145}
146
148{
149 return dirty_properties_.any();
150}
151
153{
154 return dirty_properties_.all();
155}
156
158{
159 dirty_properties_[static_cast<std::underlying_type_t<physics_property>>(prop)] = dirty;
160}
161
163{
164 return compound_shape_.size();
165}
167{
168 return compound_shape_.at(index);
169}
171{
172 compound_shape_.at(index) = shape;
173}
174
176{
177 return compound_shape_;
178}
179void physics_component::set_shapes(const std::vector<physics_compound_shape>& shape)
180{
181 compound_shape_ = shape;
182
183 on_change_shape();
184}
185
186void physics_component::on_change_shape()
187{
188 dirty_.set();
190}
191
193{
194 return material_;
195}
197{
198 if(material_ == material)
199 {
200 return;
201 }
202 material_ = material;
203
204 on_change_material();
205}
206
207void physics_component::on_change_material()
208{
209 dirty_.set();
211}
212
214 const math::vec3& explosion_position,
215 float explosion_radius,
216 float upwards_modifier,
217 force_mode mode)
218{
220 explosion_force,
221 explosion_position,
222 explosion_radius,
223 upwards_modifier,
224 mode);
225}
226
228{
230}
231
232void physics_component::apply_torque(const math::vec3& torque, force_mode mode)
233{
234 physics_system::apply_torque(*this, torque, mode);
235}
236
241
242void physics_component::set_freeze_rotation(const math::bvec3& xyz)
243{
244 if(freeze_rotation_xyz_ == xyz)
245 {
246 return;
247 }
248
249 freeze_rotation_xyz_ = xyz;
250
251 dirty_.set();
253}
254void physics_component::set_freeze_position(const math::bvec3& xyz)
255{
256 if(freeze_position_xyz_ == xyz)
257 {
258 return;
259 }
260 freeze_position_xyz_ = xyz;
261
262 dirty_.set();
264}
265
266auto physics_component::get_freeze_rotation() const -> const math::bvec3&
267{
268 return freeze_rotation_xyz_;
269}
270auto physics_component::get_freeze_position() const -> const math::bvec3&
271{
272 return freeze_position_xyz_;
273}
274
275auto physics_component::get_velocity() const -> const math::vec3&
276{
277 return velocity_;
278}
280{
281 velocity_ = velocity;
282 dirty_.set();
284}
285
287{
288 return angular_velocity_;
289}
291{
292 angular_velocity_ = velocity;
293
294 dirty_.set();
296}
297
299{
300 return collision_include_mask_;
301}
303{
304 dirty_.set();
306 collision_include_mask_ = group;
307}
308
310{
311 return collision_exclude_mask_;
312}
314{
315 dirty_.set();
317 collision_exclude_mask_ = mask;
318}
319
321{
322 return layer_mask{collision_include_mask_.mask & ~collision_exclude_mask_.mask};
323}
324
325} // namespace unravel
Base class for materials used in rendering.
Definition material.h:32
void set_owner(entt::handle owner)
Sets the owner of the component.
Component that handles physics properties and behaviors.
auto get_angular_velocity() const -> const math::vec3 &
void apply_torque(const math::vec3 &torque, force_mode mode=force_mode::force)
Applies a torque impulse to the component.
void set_shape_by_index(size_t index, const physics_compound_shape &shape)
Sets a shape by its index.
void apply_force(const math::vec3 &force, force_mode mode=force_mode::force)
auto get_shapes() const -> const std::vector< physics_compound_shape > &
Gets all shapes.
void set_is_using_gravity(bool use_gravity)
Sets whether the component uses gravity.
auto is_using_gravity() const noexcept -> bool
Checks if the component uses gravity.
void set_dirty(uint8_t id, bool dirty) noexcept
Sets the dirty flag for a specific property.
void set_is_autoscaled(bool autoscaled)
Sets whether to autoscale the physics shape.
void set_collision_include_mask(layer_mask group)
auto is_autoscaled() const noexcept -> bool
Checks if the physics shape is autoscaled.
void apply_explosion_force(float explosion_force, const math::vec3 &explosion_position, float explosion_radius, float upwards_modifier=0.0f, force_mode mode=force_mode::force)
auto get_collision_mask() const -> layer_mask
auto get_mass() const noexcept -> float
Gets the mass of the component.
void set_property_dirty(physics_property prop, bool dirty) noexcept
Sets the dirty flag for a specific physics property.
auto get_collision_include_mask() const -> layer_mask
auto get_freeze_position() const -> const math::bvec3 &
auto are_all_properties_dirty() const noexcept -> bool
Checks if all properties are dirty.
void set_collision_exclude_mask(layer_mask group)
auto get_velocity() const -> const math::vec3 &
void set_freeze_position(const math::bvec3 &xyz)
void set_shapes(const std::vector< physics_compound_shape > &shape)
Sets the shapes.
void set_velocity(const math::vec3 &velocity)
auto are_any_properties_dirty() const noexcept -> bool
Checks if any properties are dirty.
auto get_collision_exclude_mask() const -> layer_mask
void set_material(const asset_handle< physics_material > &material)
Sets the material of the component.
void set_angular_velocity(const math::vec3 &velocity)
static void on_destroy_component(entt::registry &r, entt::entity e)
Called when the component is destroyed.
void clear_kinematic_velocities()
Clears kinematic velocities.
auto get_freeze_rotation() const -> const math::bvec3 &
static void on_create_component(entt::registry &r, entt::entity e)
Called when the component is created.
auto is_kinematic() const noexcept -> bool
Checks if the component is kinematic.
auto is_sensor() const noexcept -> bool
Checks if the component is a sensor.
auto is_property_dirty(physics_property prop) const noexcept -> bool
Checks if a specific physics property is dirty.
auto get_material() const -> const asset_handle< physics_material > &
Gets the material of the component.
void set_mass(float mass)
Sets the mass of the component.
auto get_shape_by_index(size_t index) const -> const physics_compound_shape &
Gets a shape by its index.
auto get_shapes_count() const -> size_t
Gets the count of shapes.
void set_freeze_rotation(const math::bvec3 &xyz)
void set_is_kinematic(bool kinematic)
Sets whether the component is kinematic.
auto is_dirty(uint8_t id) const noexcept -> bool
Checks if a specific property is dirty.
void set_is_sensor(bool sensor)
Sets whether the component is a sensor.
static void apply_force(physics_component &comp, const math::vec3 &force, force_mode mode)
static void apply_torque(physics_component &comp, const math::vec3 &torque, force_mode mode)
Applies a torque to the specified physics component.
static void clear_kinematic_velocities(physics_component &comp)
Clears kinematic velocities for the specified physics component.
static void apply_explosion_force(physics_component &comp, float explosion_force, const math::vec3 &explosion_position, float explosion_radius, float upwards_modifier, force_mode mode)
Definition bbox.cpp:5
physics_property
Enum for different physics properties.
entt::entity entity
Represents a handle to an asset, providing access and management functions.
Represents a compound shape that can contain multiple types of shapes.
Represents the physical properties of a material.