Unravel Engine C++ Reference
Loading...
Searching...
No Matches
physics_component.h
Go to the documentation of this file.
1#pragma once
2#include <engine/engine_export.h>
3
7#include <hpp/variant.hpp>
8#include <math/math.h>
9
10
11#include <bitset>
12
13namespace unravel
14{
15
21{
22 friend auto operator==(const physics_box_shape& lhs, const physics_box_shape& rhs) -> bool = default;
23
24 math::vec3 center{};
25 math::vec3 extends{1.0f, 1.0f, 1.0f};
26};
27
33{
34 friend auto operator==(const physics_sphere_shape& lhs, const physics_sphere_shape& rhs) -> bool = default;
35
36 math::vec3 center{};
37 float radius{0.5f};
38};
39
45{
46 friend auto operator==(const physics_capsule_shape& lhs, const physics_capsule_shape& rhs) -> bool = default;
47
48 math::vec3 center{};
49 float radius{0.5f};
50 float length{1.0f};
51};
52
58{
59 friend auto operator==(const physics_cylinder_shape& lhs, const physics_cylinder_shape& rhs) -> bool = default;
60
61 math::vec3 center{};
62 float radius{0.5f};
63 float length{1.0f};
64};
65
71{
72 friend auto operator==(const physics_compound_shape& lhs, const physics_compound_shape& rhs) -> bool = default;
73
74 using shape_t =
75 hpp::variant<physics_box_shape, physics_sphere_shape, physics_capsule_shape, physics_cylinder_shape>;
76
78};
79
84enum class physics_property : uint8_t
85{
86 gravity,
87 kind,
88 mass,
90 shape,
91 sensor,
95 layer,
96 count
97};
98
99enum class force_mode : uint8_t
100{
101 // Interprets the input as torque (measured in Newton-metres), and changes the angular velocity by the value of
102 // torque * DT / mass. The effect depends on the simulation step length and the mass of the body.
103 force,
104
105 // Interprets the parameter as angular acceleration (measured in degrees per second squared), and changes the
106 // angular velocity by the value of torque * DT. The effect depends on the simulation step length but does not
107 // depend on the mass of the body.
109
110 // Interprets the parameter as an angular momentum (measured in kilogram-meters-squared per second), and changes the
111 // angular velocity by the value of torque / mass. The effect depends on the mass of the body but doesn't depend on
112 // the simulation step length.
113 impulse,
114
115 //: Interprets the parameter as a direct angular velocity change (measured in degrees per second), and changes the
116 //: angular velocity by the value of torque. The effect doesn't depend on the mass of the body and the simulation
117 //: step length.
119};
120
122{
123 math::vec3 a{};
124 math::vec3 b{};
125 math::vec3 normal_on_a{};
126 math::vec3 normal_on_b{};
127 float distance{};
128 float impulse{};
129};
130
132{
133 entt::entity entity{};
134 math::vec3 point{};
135 math::vec3 normal{};
136 float distance{};
137};
138
143class physics_component : public component_crtp<physics_component, owned_component>
144{
145public:
151 static void on_create_component(entt::registry& r, entt::entity e);
152
158 static void on_destroy_component(entt::registry& r, entt::entity e);
159
164 void set_is_using_gravity(bool use_gravity);
165
170 auto is_using_gravity() const noexcept -> bool;
171
176 void set_is_kinematic(bool kinematic);
177
182 auto is_kinematic() const noexcept -> bool;
183
188 void set_is_autoscaled(bool autoscaled);
189
194 auto is_autoscaled() const noexcept -> bool;
195
200 void set_mass(float mass);
201
206 auto get_mass() const noexcept -> float;
207
212 void set_is_sensor(bool sensor);
213
218 auto is_sensor() const noexcept -> bool;
219
225 auto is_dirty(uint8_t id) const noexcept -> bool;
226
232 void set_dirty(uint8_t id, bool dirty) noexcept;
233
239 auto is_property_dirty(physics_property prop) const noexcept -> bool;
240
245 auto are_any_properties_dirty() const noexcept -> bool;
246
251 auto are_all_properties_dirty() const noexcept -> bool;
252
258 void set_property_dirty(physics_property prop, bool dirty) noexcept;
259
264 auto get_shapes_count() const -> size_t;
265
271 auto get_shape_by_index(size_t index) const -> const physics_compound_shape&;
272
278 void set_shape_by_index(size_t index, const physics_compound_shape& shape);
279
284 auto get_shapes() const -> const std::vector<physics_compound_shape>&;
285
290 void set_shapes(const std::vector<physics_compound_shape>& shape);
291
296 auto get_material() const -> const asset_handle<physics_material>&;
297
303
304 void apply_explosion_force(float explosion_force,
305 const math::vec3& explosion_position,
306 float explosion_radius,
307 float upwards_modifier = 0.0f,
309
310 void apply_force(const math::vec3& force, force_mode mode = force_mode::force);
311
316 void apply_torque(const math::vec3& torque, force_mode mode = force_mode::force);
317
318 void set_freeze_rotation(const math::bvec3& xyz);
319 void set_freeze_position(const math::bvec3& xyz);
320
321 auto get_freeze_rotation() const -> const math::bvec3&;
322 auto get_freeze_position() const -> const math::bvec3&;
323
324 auto get_velocity() const -> const math::vec3&;
325 void set_velocity(const math::vec3& velocity);
326
327 auto get_angular_velocity() const -> const math::vec3&;
328 void set_angular_velocity(const math::vec3& velocity);
329
332
335
336 auto get_collision_mask() const -> layer_mask;
337
342
343private:
347 void on_change_gravity();
348
352 void on_change_mass();
353
357 void on_change_kind();
358
362 void on_change_shape();
363
367 void on_change_material();
368
372 void on_change_sensor();
373
375 bool is_kinematic_{};
377 bool is_using_gravity_{};
379 bool is_sensor_{};
381 bool is_autoscaled_{true};
383 float mass_{1};
384
385 layer_mask collision_include_mask_{layer_reserved::everything_layer};
386
387 layer_mask collision_exclude_mask_{layer_reserved::nothing_layer};
388
390 math::vec3 velocity_{};
391
393 math::vec3 angular_velocity_{};
394
396 math::bvec3 freeze_position_xyz_{};
397
399 math::bvec3 freeze_rotation_xyz_{};
400
403
405 std::vector<physics_compound_shape> compound_shape_{};
406
408 using underlying_t = std::underlying_type_t<physics_property>;
409 std::bitset<static_cast<underlying_t>(physics_property::count)> dirty_properties_;
410
412 std::bitset<8> dirty_;
413};
414
415} // namespace unravel
int layer_mask
Base class for materials used in rendering.
Definition material.h:32
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.
@ everything_layer
Definition layer_mask.h:16
@ nothing_layer
Definition layer_mask.h:13
physics_property
Enum for different physics properties.
Represents a handle to an asset, providing access and management functions.
CRTP (Curiously Recurring Template Pattern) base structure for components.
Represents a box shape for physics calculations.
friend auto operator==(const physics_box_shape &lhs, const physics_box_shape &rhs) -> bool=default
math::vec3 extends
Extents of the box.
math::vec3 center
Center of the box.
Represents a capsule shape for physics calculations.
friend auto operator==(const physics_capsule_shape &lhs, const physics_capsule_shape &rhs) -> bool=default
float length
Length of the capsule.
math::vec3 center
Center of the capsule.
float radius
Radius of the capsule.
Represents a compound shape that can contain multiple types of shapes.
shape_t shape
The shape contained in the compound shape.
friend auto operator==(const physics_compound_shape &lhs, const physics_compound_shape &rhs) -> bool=default
hpp::variant< physics_box_shape, physics_sphere_shape, physics_capsule_shape, physics_cylinder_shape > shape_t
Represents a cylinder shape for physics calculations.
friend auto operator==(const physics_cylinder_shape &lhs, const physics_cylinder_shape &rhs) -> bool=default
float length
Length of the cylinder.
math::vec3 center
Center of the cylinder.
float radius
Radius of the cylinder.
Represents a sphere shape for physics calculations.
float radius
Radius of the sphere.
friend auto operator==(const physics_sphere_shape &lhs, const physics_sphere_shape &rhs) -> bool=default
math::vec3 center
Center of the sphere.