Unravel Engine C++ Reference
Loading...
Searching...
No Matches
physics_system.cpp
Go to the documentation of this file.
1#include "physics_system.h"
3#include <engine/events.h>
4
6#include <engine/ecs/ecs.h>
9
10#include <logging/logging.h>
11
12namespace unravel
13{
14
25
27{
28 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
29
30 auto& ev = ctx.get_cached<events>();
31 ev.on_frame_update.connect(sentinel_, this, &physics_system::on_frame_update);
32
33 ev.on_play_begin.connect(sentinel_, 10, this, &physics_system::on_play_begin);
34 ev.on_play_end.connect(sentinel_, -10, this, &physics_system::on_play_end);
35 ev.on_pause.connect(sentinel_, 10, this, &physics_system::on_pause);
36 ev.on_resume.connect(sentinel_, -10, this, &physics_system::on_resume);
37 ev.on_skip_next_frame.connect(sentinel_, -10, this, &physics_system::on_skip_next_frame);
38
39 backend_.init();
40
41 return true;
42}
43
45{
46 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
47
48 backend_.deinit();
49
50 return true;
51}
52
53void physics_system::on_play_begin(rtti::context& ctx)
54{
55 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
56
57 auto& ec = ctx.get_cached<ecs>();
58 auto& scn = ec.get_scene();
59 auto& registry = *scn.registry;
60
61 registry.ctx().emplace<physics_system*>(this);
62 backend_.on_play_begin(ctx);
63}
64
65void physics_system::on_play_end(rtti::context& ctx)
66{
67 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
68
69 backend_.on_play_end(ctx);
70
71 auto& ec = ctx.get_cached<ecs>();
72 auto& scn = ec.get_scene();
73 auto& registry = *scn.registry;
74
75 registry.ctx().erase<physics_system*>();
76}
77
78void physics_system::on_pause(rtti::context& ctx)
79{
80 backend_.on_pause(ctx);
81}
82
83void physics_system::on_resume(rtti::context& ctx)
84{
85 backend_.on_resume(ctx);
86}
87
88void physics_system::on_skip_next_frame(rtti::context& ctx)
89{
90 backend_.on_skip_next_frame(ctx);
91}
92
93void physics_system::on_frame_update(rtti::context& ctx, delta_t dt)
94{
95 APP_SCOPE_PERF("Physics/System Update");
96
97 auto& ev = ctx.get_cached<events>();
98
99 if(ev.is_playing && !ev.is_paused)
100 {
101 backend_.on_frame_update(ctx, dt);
102 }
103}
105 float explosion_force,
106 const math::vec3& explosion_position,
107 float explosion_radius,
108 float upwards_modifier,
109 force_mode mode)
110{
112 explosion_force,
113 explosion_position,
114 explosion_radius,
115 upwards_modifier,
116 mode);
117}
118
120{
121 backend_type::apply_force(comp, force, mode);
122}
123
124void physics_system::apply_torque(physics_component& comp, const math::vec3& torque, force_mode mode)
125{
126 backend_type::apply_torque(comp, torque, mode);
127}
128
133
134auto physics_system::ray_cast(const math::vec3& origin,
135 const math::vec3& direction,
136 float max_distance,
137 int layer_mask,
138 bool query_sensors) const -> hpp::optional<raycast_hit>
139{
140 return backend_type::ray_cast(origin, direction, max_distance, layer_mask, query_sensors);
141}
142
143auto physics_system::ray_cast_all(const math::vec3& origin,
144 const math::vec3& direction,
145 float max_distance,
146 int layer_mask,
148{
149 return backend_type::ray_cast_all(origin, direction, max_distance, layer_mask, query_sensors);
150}
151
152auto physics_system::sphere_cast(const math::vec3& origin,
153 const math::vec3& direction,
154 float radius,
155 float max_distance,
156 int layer_mask,
157 bool query_sensors) const -> hpp::optional<raycast_hit>
158{
159 return backend_type::sphere_cast(origin, direction, radius, max_distance, layer_mask, query_sensors);
160}
161
162auto physics_system::sphere_cast_all(const math::vec3& origin,
163 const math::vec3& direction,
164 float radius,
165 float max_distance,
166 int layer_mask,
168{
169 return backend_type::sphere_cast_all(origin, direction, radius, max_distance, layer_mask, query_sensors);
170}
171
172auto physics_system::sphere_overlap(const math::vec3& origin, float radius, int layer_mask, bool query_sensors) const
174{
175 return backend_type::sphere_overlap(origin, radius, layer_mask, query_sensors);
176}
177
178} // namespace unravel
bool query_sensors
Component that handles physics properties and behaviors.
static void on_destroy_component(entt::registry &r, entt::entity e)
Called when the component is destroyed.
static void on_create_component(entt::registry &r, entt::entity e)
Called when the component is created.
Manages the physics operations using the specified backend.
auto ray_cast(const math::vec3 &origin, const math::vec3 &direction, float max_distance, int layer_mask, bool query_sensors) const -> hpp::optional< raycast_hit >
static void on_destroy_component(entt::registry &r, entt::entity e)
Called when a physics component is destroyed.
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.
auto sphere_cast_all(const math::vec3 &origin, const math::vec3 &direction, float radius, float max_distance, int layer_mask, bool query_sensors) const -> physics_vector< raycast_hit >
static void clear_kinematic_velocities(physics_component &comp)
Clears kinematic velocities for the specified physics component.
auto deinit(rtti::context &ctx) -> bool
Deinitializes the physics system with the given context.
auto sphere_overlap(const math::vec3 &origin, float radius, int layer_mask, bool query_sensors) const -> physics_vector< entt::entity >
auto ray_cast_all(const math::vec3 &origin, const math::vec3 &direction, float max_distance, int layer_mask, bool query_sensors) const -> physics_vector< raycast_hit >
auto init(rtti::context &ctx) -> bool
Initializes the physics system with the given context.
static void on_create_component(entt::registry &r, entt::entity e)
Called when a physics component is created.
auto sphere_cast(const math::vec3 &origin, const math::vec3 &direction, float radius, float max_distance, int layer_mask, bool query_sensors) const -> hpp::optional< raycast_hit >
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)
std::chrono::duration< float > delta_t
#define APPLOG_TRACE(...)
Definition logging.h:17
hpp::small_vector< T, SmallSizeCapacity > physics_vector
#define APP_SCOPE_PERF(name)
Create a scoped performance timer that only accepts string literals.
Definition profiler.h:160
auto get_cached() -> T &
Definition context.hpp:49
void on_skip_next_frame(rtti::context &ctx)
static void on_create_component(entt::registry &r, entt::entity e)
void on_play_begin(rtti::context &ctx)
static void apply_force(physics_component &comp, const math::vec3 &force, force_mode mode)
void on_resume(rtti::context &ctx)
void on_pause(rtti::context &ctx)
static void apply_torque(physics_component &comp, const math::vec3 &toruqe, force_mode mode)
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)
static void on_destroy_component(entt::registry &r, entt::entity e)
static void clear_kinematic_velocities(physics_component &comp)
void on_frame_update(rtti::context &ctx, delta_t dt)
void on_play_end(rtti::context &ctx)
Manages the entity-component-system (ECS) operations for the ACE framework.
Definition ecs.h:12
auto get_scene() -> scene &
Gets the current scene.
Definition ecs.cpp:30
hpp::event< void(rtti::context &, delta_t)> on_frame_update
Definition events.h:16