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#include <engine/play_mode.h>
5
7#include <engine/ecs/ecs.h>
11
12#include <logging/logging.h>
13
14namespace unravel
15{
16
27
29{
30 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
31
32 auto& ev = ctx.get_cached<events>();
33 ev.on_frame_update.connect(sentinel_, this, &physics_system::on_frame_update);
34
35 ev.on_play_begin.connect(sentinel_, 10, this, &physics_system::on_play_begin);
36 ev.on_play_end.connect(sentinel_, -10, this, &physics_system::on_play_end);
37 ev.on_pause.connect(sentinel_, 10, this, &physics_system::on_pause);
38 ev.on_resume.connect(sentinel_, -10, this, &physics_system::on_resume);
39 ev.on_skip_next_frame.connect(sentinel_, -10, this, &physics_system::on_skip_next_frame);
40
41 backend_.init();
42
43 return true;
44}
45
47{
48 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
49
50 backend_.deinit();
51
52 return true;
53}
54
55void physics_system::on_play_begin(rtti::context& ctx)
56{
57 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
58
59 auto& ec = ctx.get_cached<ecs>();
60 auto& scn = ec.get_scene();
61 auto& registry = *scn.registry;
62
63 registry.ctx().emplace<physics_system*>(this);
64 backend_.on_play_begin(ctx);
65}
66
67void physics_system::on_play_end(rtti::context& ctx)
68{
69 APPLOG_TRACE("{}::{}", hpp::type_name_str(*this), __func__);
70
71 backend_.on_play_end(ctx);
72
73 auto& ec = ctx.get_cached<ecs>();
74 auto& scn = ec.get_scene();
75 auto& registry = *scn.registry;
76
77 registry.ctx().erase<physics_system*>();
78}
79
80void physics_system::on_pause(rtti::context& ctx)
81{
82 backend_.on_pause(ctx);
83}
84
85void physics_system::on_resume(rtti::context& ctx)
86{
87 backend_.on_resume(ctx);
88}
89
90void physics_system::on_skip_next_frame(rtti::context& ctx)
91{
92 backend_.on_skip_next_frame(ctx);
93}
94
95void physics_system::on_frame_update(rtti::context& ctx, delta_t dt)
96{
97 APP_SCOPE_PERF("Physics/System Update");
98
99 auto& play = ctx.get_cached<play_mode>();
100
101 if(play.is_simulation_running() && !play.is_paused())
102 {
103 // First sim frame (incl. mid-frame enter_running): do not accumulate
104 // a hitch into fixed steps.
105 if(play.frames_running() == 0)
106 {
107 dt = {};
108 }
109 backend_.on_frame_update(ctx, dt);
110 }
111}
113 float explosion_force,
114 const math::vec3& explosion_position,
115 float explosion_radius,
116 float upwards_modifier,
117 force_mode mode)
118{
120 explosion_force,
121 explosion_position,
122 explosion_radius,
123 upwards_modifier,
124 mode);
125}
126
128{
129 backend_type::apply_force(comp, force, mode);
130}
131
132void physics_system::apply_torque(physics_component& comp, const math::vec3& torque, force_mode mode)
133{
134 backend_type::apply_torque(comp, torque, mode);
135}
136
141
147
153
154void physics_system::move_character(character_controller_component& comp, const math::vec3& displacement)
155{
156 backend_type::move_character(comp, displacement);
157}
158
160{
161 backend_type::jump_character(comp, direction);
162}
163
168
173
178
179auto physics_system::ray_cast(const math::vec3& origin,
180 const math::vec3& direction,
181 float max_distance,
182 int layer_mask,
183 bool query_sensors) const -> hpp::optional<raycast_hit>
184{
185 return backend_type::ray_cast(origin, direction, max_distance, layer_mask, query_sensors);
186}
187
188auto physics_system::ray_cast_all(const math::vec3& origin,
189 const math::vec3& direction,
190 float max_distance,
191 int layer_mask,
193{
194 return backend_type::ray_cast_all(origin, direction, max_distance, layer_mask, query_sensors);
195}
196
197auto physics_system::sphere_cast(const math::vec3& origin,
198 const math::vec3& direction,
199 float radius,
200 float max_distance,
201 int layer_mask,
202 bool query_sensors) const -> hpp::optional<raycast_hit>
203{
204 return backend_type::sphere_cast(origin, direction, radius, max_distance, layer_mask, query_sensors);
205}
206
207auto physics_system::sphere_cast_all(const math::vec3& origin,
208 const math::vec3& direction,
209 float radius,
210 float max_distance,
211 int layer_mask,
213{
214 return backend_type::sphere_cast_all(origin, direction, radius, max_distance, layer_mask, query_sensors);
215}
216
217auto physics_system::sphere_overlap(const math::vec3& origin, float radius, int layer_mask, bool query_sensors) const
219{
220 return backend_type::sphere_overlap(origin, radius, layer_mask, query_sensors);
221}
222
223} // namespace unravel
bool query_sensors
Component for character controller physics with sweep-based movement.
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.
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.
static void jump_character(character_controller_component &comp, const math::vec3 &direction)
static void warp_character(character_controller_component &comp, const math::vec3 &position)
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 >
static void apply_impulse_character(character_controller_component &comp, const math::vec3 &impulse)
static void move_character(character_controller_component &comp, const math::vec3 &displacement)
Moves a character controller by a displacement.
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.
static void set_character_linear_velocity(character_controller_component &comp, const math::vec3 &velocity)
static void on_create_cc_component(entt::registry &r, entt::entity e)
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 on_destroy_cc_component(entt::registry &r, entt::entity e)
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
math::vec3 position
Definition defaults.cpp:52
#define APPLOG_TRACE(...)
Definition logging.h:17
hpp::small_vector< T, SmallSizeCapacity > physics_vector
#define APP_SCOPE_PERF(name_literal)
Create a scoped performance timer that records to the timeline profiler. Only accepts string literals...
Definition profiler.h:675
auto get_cached() -> T &
Definition context.hpp:49
void on_skip_next_frame(rtti::context &ctx)
static void on_create_cc_component(entt::registry &r, entt::entity e)
static void on_create_component(entt::registry &r, entt::entity e)
static void set_character_linear_velocity(character_controller_component &comp, const math::vec3 &velocity)
static void warp_character(character_controller_component &comp, const math::vec3 &position)
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)
static void on_destroy_cc_component(entt::registry &r, entt::entity e)
void on_pause(rtti::context &ctx)
static void apply_impulse_character(character_controller_component &comp, const math::vec3 &impulse)
static void move_character(character_controller_component &comp, const math::vec3 &displacement)
static void jump_character(character_controller_component &comp, const math::vec3 &direction)
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