From 289ecc3a3829e9b3acff0b1778f75bc526173977 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 7 Dec 2024 19:14:34 +0100 Subject: changed double to float --- src/crepe/system/ParticleSystem.cpp | 38 ++++++++++++++++++------------------- src/crepe/system/ParticleSystem.h | 6 +++--- 2 files changed, 22 insertions(+), 22 deletions(-) (limited to 'src/crepe/system') diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index b14c52f..596b4b0 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -21,9 +21,9 @@ void ParticleSystem::update() { = mgr.get_components_by_id(emitter.game_object_id).front().get(); // Emit particles based on emission_rate - int updates = calculate_update(this->update_count, emitter.data.emission_rate); + int updates = this->calculate_update(this->update_count, emitter.data.emission_rate); for (size_t i = 0; i < updates; i++) { - emit_particle(emitter, transform); + this->emit_particle(emitter, transform); } // Update all particles @@ -34,7 +34,7 @@ void ParticleSystem::update() { } // Check if within boundary - check_bounds(emitter, transform); + this->check_bounds(emitter, transform); } this->update_count = (this->update_count + 1) % this->MAX_UPDATE_COUNT; @@ -61,9 +61,9 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & } } -int ParticleSystem::calculate_update(int count, double emission) const { - double integer_part = std::floor(emission); - double fractional_part = emission - integer_part; +int ParticleSystem::calculate_update(int count, float emission) const { + float integer_part = std::floor(emission); + float fractional_part = emission - integer_part; if (fractional_part > 0) { int denominator = static_cast(1.0 / fractional_part); @@ -75,13 +75,13 @@ int ParticleSystem::calculate_update(int count, double emission) const { void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & transform) { vec2 offset = emitter.data.boundary.offset + transform.position + emitter.data.position; - double half_width = emitter.data.boundary.width / 2.0; - double half_height = emitter.data.boundary.height / 2.0; + float half_width = emitter.data.boundary.width / 2.0; + float half_height = emitter.data.boundary.height / 2.0; - const double LEFT = offset.x - half_width; - const double RIGHT = offset.x + half_width; - const double TOP = offset.y - half_height; - const double BOTTOM = offset.y + half_height; + const float LEFT = offset.x - half_width; + const float RIGHT = offset.x + half_width; + const float TOP = offset.y - half_height; + const float BOTTOM = offset.y + half_height; for (Particle & particle : emitter.data.particles) { const vec2 & position = particle.position; @@ -102,25 +102,25 @@ void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & t } } -double ParticleSystem::generate_random_angle(double min_angle, double max_angle) const { +float ParticleSystem::generate_random_angle(float min_angle, float max_angle) const { if (min_angle == max_angle) { return min_angle; } else if (min_angle < max_angle) { return min_angle - + static_cast(std::rand() % static_cast(max_angle - min_angle)); + + static_cast(std::rand() % static_cast(max_angle - min_angle)); } else { - double angle_offset = (360 - min_angle) + max_angle; - double random_angle - = min_angle + static_cast(std::rand() % static_cast(angle_offset)); + float angle_offset = (360 - min_angle) + max_angle; + float random_angle + = min_angle + static_cast(std::rand() % static_cast(angle_offset)); return (random_angle >= 360) ? random_angle - 360 : random_angle; } } -double ParticleSystem::generate_random_speed(double min_speed, double max_speed) const { +float ParticleSystem::generate_random_speed(float min_speed, float max_speed) const { if (min_speed == max_speed) { return min_speed; } else { return min_speed - + static_cast(std::rand() % static_cast(max_speed - min_speed)); + + static_cast(std::rand() % static_cast(max_speed - min_speed)); } } diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index 068f01c..2adb0f0 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -39,7 +39,7 @@ private: * \param emission Emission rate. * \return The number of particles to emit. */ - int calculate_update(int count, double emission) const; + int calculate_update(int count, float emission) const; /** * \brief Checks whether particles are within the emitter’s boundary, resets or stops @@ -57,7 +57,7 @@ private: * \param max_angle Maximum emission angle in degrees. * \return Random angle in degrees. */ - double generate_random_angle(double min_angle, double max_angle) const; + float generate_random_angle(float min_angle, float max_angle) const; /** * \brief Generates a random speed for particle emission within the specified range. @@ -66,7 +66,7 @@ private: * \param max_speed Maximum emission speed. * \return Random speed. */ - double generate_random_speed(double min_speed, double max_speed) const; + float generate_random_speed(float min_speed, float max_speed) const; private: //! Counter to count updates to determine how many times emit_particle is -- cgit v1.2.3 From 8c81bf4a33a13fc21dca7e3fe78a6dc334ac964b Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 7 Dec 2024 22:44:35 +0100 Subject: changed spawnrate of particles (bound to delta time) --- src/crepe/Particle.cpp | 9 +++++---- src/crepe/Particle.h | 7 ++++--- src/crepe/api/ParticleEmitter.h | 2 ++ src/crepe/system/ParticleSystem.cpp | 26 +++++++++----------------- src/crepe/system/ParticleSystem.h | 11 +---------- 5 files changed, 21 insertions(+), 34 deletions(-) (limited to 'src/crepe/system') diff --git a/src/crepe/Particle.cpp b/src/crepe/Particle.cpp index ce000a1..b340826 100644 --- a/src/crepe/Particle.cpp +++ b/src/crepe/Particle.cpp @@ -15,16 +15,17 @@ void Particle::reset(unsigned int lifespan, const vec2 & position, const vec2 & this->force_over_time = {0, 0}; } -void Particle::update() { +void Particle::update(double dt) { // Deactivate particle if it has exceeded its lifespan - if (++time_in_life >= lifespan) { + time_in_life += dt; + if (time_in_life >= lifespan) { this->active = false; return; } // Update velocity based on accumulated force and update position - this->velocity += force_over_time; - this->position += velocity; + this->velocity += force_over_time * dt; + this->position += velocity * dt; } void Particle::stop_movement() { diff --git a/src/crepe/Particle.h b/src/crepe/Particle.h index 4fa93a1..49fec1f 100644 --- a/src/crepe/Particle.h +++ b/src/crepe/Particle.h @@ -24,11 +24,11 @@ public: //! Accumulated force affecting the particle over time. vec2 force_over_time; //! Total lifespan of the particle in milliseconds. - unsigned int lifespan; + float lifespan; //! Active state of the particle; true if it is in use, false otherwise. bool active = false; //! The time the particle has been alive, in milliseconds. - unsigned int time_in_life = 0; + float time_in_life = 0; //! The angle at which the particle is oriented or moving. float angle = 0; @@ -49,8 +49,9 @@ public: * * Advances the particle's position based on its velocity and applies accumulated forces. * Deactivates the particle if its lifespan has expired. + * \param dt The amount of fixed delta time that has passed. */ - void update(); + void update(double dt); /** * \brief Stops the particle's movement. * diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index e8fa15e..48c7625 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -52,6 +52,8 @@ public: const unsigned int max_particles = 256; //! rate of particle emission per update (Lowest value = 0.001 any lower is ignored) float emission_rate = 1; + //! Saves time left over from last update event. + float spawn_accumulator = 0; //! min speed of the particles float min_speed = 1; //! min speed of the particles diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 596b4b0..db4bcaf 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -12,8 +12,10 @@ using namespace crepe; void ParticleSystem::update() { // Get all emitters + ComponentManager & mgr = this->mediator.component_manager; RefVector emitters = mgr.get_components_by_type(); + double dt = LoopTimer::get_instance().get_fixed_delta_time(); for (ParticleEmitter & emitter : emitters) { // Get transform linked to emitter @@ -21,15 +23,17 @@ void ParticleSystem::update() { = mgr.get_components_by_id(emitter.game_object_id).front().get(); // Emit particles based on emission_rate - int updates = this->calculate_update(this->update_count, emitter.data.emission_rate); - for (size_t i = 0; i < updates; i++) { - this->emit_particle(emitter, transform); - } + int spawn_amount = emitter.data.emission_rate * dt; + while (emitter.data.spawn_accumulator >= 1.0) { + emit_particle(emitter, transform); + emitter.data.spawn_accumulator -= 1.0; + } + // Update all particles for (Particle & particle : emitter.data.particles) { if (particle.active) { - particle.update(); + particle.update(dt); } } @@ -61,18 +65,6 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & } } -int ParticleSystem::calculate_update(int count, float emission) const { - float integer_part = std::floor(emission); - float fractional_part = emission - integer_part; - - if (fractional_part > 0) { - int denominator = static_cast(1.0 / fractional_part); - return (count % denominator == 0) ? 1 : 0; - } - - return static_cast(emission); -} - void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & transform) { vec2 offset = emitter.data.boundary.offset + transform.position + emitter.data.position; float half_width = emitter.data.boundary.width / 2.0; diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index 2adb0f0..454b65f 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -23,6 +23,7 @@ public: void update() override; private: + /** * \brief Emits a particle from the specified emitter based on its emission properties. * @@ -31,16 +32,6 @@ private: */ void emit_particle(ParticleEmitter & emitter, const Transform & transform); - /** - * \brief Calculates the number of times particles should be emitted based on emission rate - * and update count. - * - * \param count Current update count. - * \param emission Emission rate. - * \return The number of particles to emit. - */ - int calculate_update(int count, float emission) const; - /** * \brief Checks whether particles are within the emitter’s boundary, resets or stops * particles if they exit. -- cgit v1.2.3 From dd7d5cf6b01b8a6a4238b66c27861ee76522067e Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 7 Dec 2024 22:48:05 +0100 Subject: added this --- src/crepe/system/ParticleSystem.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'src/crepe/system') diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index db4bcaf..3befb03 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -23,13 +23,12 @@ void ParticleSystem::update() { = mgr.get_components_by_id(emitter.game_object_id).front().get(); // Emit particles based on emission_rate - int spawn_amount = emitter.data.emission_rate * dt; + emitter.data.spawn_accumulator = emitter.data.emission_rate * dt; while (emitter.data.spawn_accumulator >= 1.0) { - emit_particle(emitter, transform); + this->emit_particle(emitter, transform); emitter.data.spawn_accumulator -= 1.0; } - // Update all particles for (Particle & particle : emitter.data.particles) { if (particle.active) { @@ -48,9 +47,9 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & constexpr float DEG_TO_RAD = M_PI / 180.0; vec2 initial_position = emitter.data.position + transform.position; - float random_angle = generate_random_angle(emitter.data.min_angle, emitter.data.max_angle); + float random_angle = this->generate_random_angle(emitter.data.min_angle, emitter.data.max_angle); - float random_speed = generate_random_speed(emitter.data.min_speed, emitter.data.max_speed); + float random_speed = this->generate_random_speed(emitter.data.min_speed, emitter.data.max_speed); float angle_radians = random_angle * DEG_TO_RAD; vec2 velocity @@ -79,7 +78,6 @@ void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & t const vec2 & position = particle.position; bool within_bounds = (position.x >= LEFT && position.x <= RIGHT && position.y >= TOP && position.y <= BOTTOM); - if (!within_bounds) { if (emitter.data.boundary.reset_on_exit) { particle.active = false; -- cgit v1.2.3 From afd81007153f05c8f8b42bcf08a8cdf8ce13a98c Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 15:32:07 +0100 Subject: updated particle emitter --- src/crepe/api/LoopManager.cpp | 1 + src/crepe/api/ParticleEmitter.h | 7 +++++-- src/crepe/system/ParticleSystem.cpp | 16 ++++++++++------ src/crepe/system/RenderSystem.cpp | 2 +- src/example/game.cpp | 21 +++++++++++++++++++++ src/example/rendering_particle.cpp | 23 +---------------------- 6 files changed, 39 insertions(+), 31 deletions(-) (limited to 'src/crepe/system') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index b5e5ff7..7a78019 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -65,6 +65,7 @@ void LoopManager::fixed_update() { this->get_system().update(); this->event_manager.dispatch_events(); this->get_system().update(); + this->get_system().update(); this->get_system().update(); this->get_system().update(); this->get_system().update(); diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index 48c7625..cdea69a 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -5,6 +5,7 @@ #include "Component.h" #include "Particle.h" +#include "system/ParticleSystem.h" #include "types.h" namespace crepe { @@ -52,8 +53,6 @@ public: const unsigned int max_particles = 256; //! rate of particle emission per update (Lowest value = 0.001 any lower is ignored) float emission_rate = 1; - //! Saves time left over from last update event. - float spawn_accumulator = 0; //! min speed of the particles float min_speed = 1; //! min speed of the particles @@ -85,6 +84,10 @@ public: public: //! Configuration data for particle emission settings. Data data; +private: + //! Saves time left over from last update event. + friend ParticleSystem; + float spawn_accumulator = 0; }; } // namespace crepe diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 3befb03..941e502 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,6 +6,7 @@ #include "../api/ParticleEmitter.h" #include "../api/Transform.h" #include "../manager/ComponentManager.h" +#include "../manager/LoopTimerManager.h" #include "ParticleSystem.h" @@ -12,10 +14,12 @@ using namespace crepe; void ParticleSystem::update() { // Get all emitters - - ComponentManager & mgr = this->mediator.component_manager; + const Mediator & mediator = this->mediator; + LoopTimerManager & loop_timer = mediator.loop_timer; + ComponentManager & mgr = mediator.component_manager; + float dt = std::chrono::duration(loop_timer.get_scaled_fixed_delta_time()).count(); + RefVector emitters = mgr.get_components_by_type(); - double dt = LoopTimer::get_instance().get_fixed_delta_time(); for (ParticleEmitter & emitter : emitters) { // Get transform linked to emitter @@ -23,10 +27,10 @@ void ParticleSystem::update() { = mgr.get_components_by_id(emitter.game_object_id).front().get(); // Emit particles based on emission_rate - emitter.data.spawn_accumulator = emitter.data.emission_rate * dt; - while (emitter.data.spawn_accumulator >= 1.0) { + emitter.spawn_accumulator = emitter.data.emission_rate * dt; + while (emitter.spawn_accumulator >= 1.0) { this->emit_particle(emitter, transform); - emitter.data.spawn_accumulator -= 1.0; + emitter.spawn_accumulator -= 1.0; } // Update all particles diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index afd9548..cc633e8 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -83,7 +83,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) bool rendering_particles = false; for (const ParticleEmitter & em : emitters) { - if (&em.data.sprite != &sprite) continue; + if (&em.sprite != &sprite) continue; rendering_particles = true; if (!em.active) continue; diff --git a/src/example/game.cpp b/src/example/game.cpp index 5361f3a..279648e 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -1,4 +1,5 @@ #include "api/CircleCollider.h" +#include "api/ParticleEmitter.h" #include "api/Scene.h" #include "manager/ComponentManager.h" #include "manager/Mediator.h" @@ -258,6 +259,26 @@ public: }) .active = false; + Asset img5{"asset/texture/square.png"}; + + + + GameObject particle = mgr.new_object( + "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); + auto & particle_image = particle.add_component(img5, Sprite::Data{.size = {5, 5},}); + auto & test = particle.add_component(particle_image,ParticleEmitter::Data{ + .position = {0, 0}, + .max_particles = 256, + .emission_rate = 50, + .min_speed = 10, + .max_speed = 20, + .min_angle = -20, + .max_angle = 20, + .begin_lifespan = 0, + .end_lifespan = 5, + } + ); + } string get_name() const { return "scene1"; } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 13e625f..2b5c041 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -17,28 +17,7 @@ using namespace crepe; using namespace std; - -/* - auto & test = game_object.add_component(ParticleEmitter::Data{ - .position = {0, 0}, - .max_particles = 10, - .emission_rate = 0.1, - .min_speed = 6, - .max_speed = 20, - .min_angle = -20, - .max_angle = 20, - .begin_lifespan = 0, - .end_lifespan = 60, - .force_over_time = vec2{0, 0}, - .boundary{ - .width = 1000, - .height = 1000, - .offset = vec2{0, 0}, - .reset_on_exit = false, - }, - .sprite = test_sprite, - }); - */ + class TestScene : public Scene { public: -- cgit v1.2.3 From 5b1c45c2c3bd695ee8d8b5baa4b1988e24596559 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 15:42:03 +0100 Subject: restore file --- src/crepe/system/CollisionSystem.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'src/crepe/system') diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 0483693..952ed0a 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -290,6 +290,7 @@ vec2 CollisionSystem::get_circle_box_resolution(const CircleCollider & circle_co // Compute penetration depth float penetration_depth = circle_collider.radius - distance; + // Compute the resolution vector vec2 resolution = collision_normal * penetration_depth; -- cgit v1.2.3 From d4b4a54f919872ef1295890cd36ac46d89a4426d Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 15:51:29 +0100 Subject: removed old code --- src/crepe/system/ParticleSystem.cpp | 2 -- src/crepe/system/ParticleSystem.h | 8 -------- 2 files changed, 10 deletions(-) (limited to 'src/crepe/system') diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 941e502..204afaf 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -43,8 +43,6 @@ void ParticleSystem::update() { // Check if within boundary this->check_bounds(emitter, transform); } - - this->update_count = (this->update_count + 1) % this->MAX_UPDATE_COUNT; } void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & transform) { diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index 454b65f..95a2bd5 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -58,14 +58,6 @@ private: * \return Random speed. */ float generate_random_speed(float min_speed, float max_speed) const; - -private: - //! Counter to count updates to determine how many times emit_particle is - // called. - unsigned int update_count = 0; - //! Determines the lowest amount of emission rate (1000 = 0.001 = 1 particle per 1000 - // updates). - static constexpr unsigned int MAX_UPDATE_COUNT = 100; }; } // namespace crepe -- cgit v1.2.3 From bef438ff7790acbe2dc376097e1782e60356bf45 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 18:25:11 +0100 Subject: changed get dt --- src/crepe/system/ParticleSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/crepe/system') diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 204afaf..a58edf0 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -17,7 +17,7 @@ void ParticleSystem::update() { const Mediator & mediator = this->mediator; LoopTimerManager & loop_timer = mediator.loop_timer; ComponentManager & mgr = mediator.component_manager; - float dt = std::chrono::duration(loop_timer.get_scaled_fixed_delta_time()).count(); + float dt = loop_timer.get_scaled_fixed_delta_time(); RefVector emitters = mgr.get_components_by_type(); -- cgit v1.2.3 From feb27ebdc4086dec62e45e3a6c83bdd021568dd5 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 18:47:18 +0100 Subject: reverted dt --- src/crepe/system/ParticleSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/crepe/system') diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index a58edf0..85b8248 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -17,7 +17,7 @@ void ParticleSystem::update() { const Mediator & mediator = this->mediator; LoopTimerManager & loop_timer = mediator.loop_timer; ComponentManager & mgr = mediator.component_manager; - float dt = loop_timer.get_scaled_fixed_delta_time(); + float dt = loop_timer.get_scaled_fixed_delta_time().count(); RefVector emitters = mgr.get_components_by_type(); -- cgit v1.2.3 From 927aab8cf424d1f3bf25056ce4b4126ce9f4edab Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 18:49:50 +0100 Subject: reverted --- src/crepe/system/CollisionSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/crepe/system') diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 952ed0a..44a0431 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -290,7 +290,7 @@ vec2 CollisionSystem::get_circle_box_resolution(const CircleCollider & circle_co // Compute penetration depth float penetration_depth = circle_collider.radius - distance; - + // Compute the resolution vector vec2 resolution = collision_normal * penetration_depth; -- cgit v1.2.3 From d3a04fb1e8b119017375caab74c43674006a7348 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 12 Dec 2024 22:04:49 +0100 Subject: broken particle test --- src/crepe/api/ParticleEmitter.cpp | 2 +- src/crepe/api/ParticleEmitter.h | 10 ++++++---- src/crepe/system/ParticleSystem.cpp | 6 +++--- src/crepe/system/RenderSystem.cpp | 2 +- src/test/ParticleTest.cpp | 37 +++++++++++++++++++++---------------- 5 files changed, 32 insertions(+), 25 deletions(-) (limited to 'src/crepe/system') diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp index 1cfdceb..1e9cfaa 100644 --- a/src/crepe/api/ParticleEmitter.cpp +++ b/src/crepe/api/ParticleEmitter.cpp @@ -7,6 +7,6 @@ ParticleEmitter::ParticleEmitter(game_object_id_t game_object_id, const Sprite & : Component(game_object_id), sprite(sprite), data(data) { for (size_t i = 0; i < this->data.max_particles; i++) { - this->data.particles.emplace_back(); + this->particles.emplace_back(); } } diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index cc54ffb..e0b117a 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -3,9 +3,11 @@ #include #include +#include "system/ParticleSystem.h" +#include "system/RenderSystem.h" + #include "Component.h" #include "Particle.h" -#include "system/ParticleSystem.h" #include "types.h" namespace crepe { @@ -69,9 +71,6 @@ public: vec2 force_over_time; //! particle boundary Boundary boundary; - //! collection of particles - std::vector particles; - }; public: @@ -87,7 +86,10 @@ public: private: //! Saves time left over from last update event. friend ParticleSystem; + friend RenderSystem; float spawn_accumulator = 0; + //! collection of particles + std::vector particles; }; } // namespace crepe diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 85b8248..a56de60 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -34,7 +34,7 @@ void ParticleSystem::update() { } // Update all particles - for (Particle & particle : emitter.data.particles) { + for (Particle & particle : emitter.particles) { if (particle.active) { particle.update(dt); } @@ -57,7 +57,7 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & vec2 velocity = {random_speed * std::cos(angle_radians), random_speed * std::sin(angle_radians)}; - for (Particle & particle : emitter.data.particles) { + for (Particle & particle : emitter.particles) { if (!particle.active) { particle.reset(emitter.data.end_lifespan, initial_position, velocity, random_angle); @@ -76,7 +76,7 @@ void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & t const float TOP = offset.y - half_height; const float BOTTOM = offset.y + half_height; - for (Particle & particle : emitter.data.particles) { + for (Particle & particle : emitter.particles) { const vec2 & position = particle.position; bool within_bounds = (position.x >= LEFT && position.x <= RIGHT && position.y >= TOP && position.y <= BOTTOM); diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index cc633e8..505433a 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -87,7 +87,7 @@ bool RenderSystem::render_particle(const Sprite & sprite, const double & scale) rendering_particles = true; if (!em.active) continue; - for (const Particle & p : em.data.particles) { + for (const Particle & p : em.particles) { if (!p.active) continue; ctx.draw(SDLContext::RenderContext{ diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 9112a3f..70534f3 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -11,10 +11,14 @@ #include #include +#define protected public +#define private public + using namespace std; using namespace std::chrono_literals; using namespace crepe; + class ParticlesTest : public ::testing::Test { Mediator m; @@ -57,6 +61,7 @@ public: }, .sprite = test_sprite, }); + } transforms = mgr.get_components_by_id(0); Transform & transform = transforms.front().get(); @@ -76,7 +81,7 @@ public: emitter.data.end_lifespan = 0; emitter.data.force_over_time = vec2{0, 0}; emitter.data.boundary = {0, 0, vec2{0, 0}, false}; - for (auto & particle : emitter.data.particles) { + for (auto & particle : emitter.particles) { particle.active = false; } } @@ -95,19 +100,19 @@ TEST_F(ParticlesTest, spawnParticle) { emitter.data.max_angle = 10; particle_system.update(); //check if nothing happend - EXPECT_EQ(emitter.data.particles[0].active, false); + EXPECT_EQ(emitter.particles[0].active, false); emitter.data.emission_rate = 1; //check particle spawnes particle_system.update(); - EXPECT_EQ(emitter.data.particles[0].active, true); + EXPECT_EQ(emitter.particles[0].active, true); particle_system.update(); - EXPECT_EQ(emitter.data.particles[1].active, true); + EXPECT_EQ(emitter.particles[1].active, true); particle_system.update(); - EXPECT_EQ(emitter.data.particles[2].active, true); + EXPECT_EQ(emitter.particles[2].active, true); particle_system.update(); - EXPECT_EQ(emitter.data.particles[3].active, true); + EXPECT_EQ(emitter.particles[3].active, true); - for (auto & particle : emitter.data.particles) { + for (auto & particle : emitter.particles) { // Check velocity range EXPECT_GE(particle.velocity.x, emitter.data.min_speed); // Speed should be greater than or equal to min_speed @@ -139,7 +144,7 @@ TEST_F(ParticlesTest, moveParticleHorizontal) { emitter.data.emission_rate = 1; for (int a = 1; a < emitter.data.boundary.width / 2; a++) { particle_system.update(); - EXPECT_EQ(emitter.data.particles[0].position.x, a); + EXPECT_EQ(emitter.particles[0].position.x, a); } } @@ -157,7 +162,7 @@ TEST_F(ParticlesTest, moveParticleVertical) { emitter.data.emission_rate = 1; for (int a = 1; a < emitter.data.boundary.width / 2; a++) { particle_system.update(); - EXPECT_EQ(emitter.data.particles[0].position.y, a); + EXPECT_EQ(emitter.particles[0].position.y, a); } } @@ -177,7 +182,7 @@ TEST_F(ParticlesTest, boundaryParticleReset) { for (int a = 0; a < emitter.data.boundary.width / 2 + 1; a++) { particle_system.update(); } - EXPECT_EQ(emitter.data.particles[0].active, false); + EXPECT_EQ(emitter.particles[0].active, false); } TEST_F(ParticlesTest, boundaryParticleStop) { @@ -197,12 +202,12 @@ TEST_F(ParticlesTest, boundaryParticleStop) { particle_system.update(); } const double TOLERANCE = 0.01; - EXPECT_NEAR(emitter.data.particles[0].velocity.x, 0, TOLERANCE); - EXPECT_NEAR(emitter.data.particles[0].velocity.y, 0, TOLERANCE); - if (emitter.data.particles[0].velocity.x != 0) - EXPECT_NEAR(std::abs(emitter.data.particles[0].position.x), + EXPECT_NEAR(emitter.particles[0].velocity.x, 0, TOLERANCE); + EXPECT_NEAR(emitter.particles[0].velocity.y, 0, TOLERANCE); + if (emitter.particles[0].velocity.x != 0) + EXPECT_NEAR(std::abs(emitter.particles[0].position.x), emitter.data.boundary.height / 2, TOLERANCE); - if (emitter.data.particles[0].velocity.y != 0) - EXPECT_NEAR(std::abs(emitter.data.particles[0].position.y), + if (emitter.particles[0].velocity.y != 0) + EXPECT_NEAR(std::abs(emitter.particles[0].position.y), emitter.data.boundary.width / 2, TOLERANCE); } -- cgit v1.2.3 From 79741613fdf915bfb8b7517826205d2cb1715971 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 18:54:30 +0100 Subject: removed some not needed const --- src/crepe/system/ParticleSystem.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'src/crepe/system') diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index a56de60..f98f245 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -71,24 +71,25 @@ void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & t float half_width = emitter.data.boundary.width / 2.0; float half_height = emitter.data.boundary.height / 2.0; - const float LEFT = offset.x - half_width; - const float RIGHT = offset.x + half_width; - const float TOP = offset.y - half_height; - const float BOTTOM = offset.y + half_height; + float left = offset.x - half_width; + float right = offset.x + half_width; + float top = offset.y - half_height; + float bottom = offset.y + half_height; for (Particle & particle : emitter.particles) { const vec2 & position = particle.position; - bool within_bounds = (position.x >= LEFT && position.x <= RIGHT && position.y >= TOP - && position.y <= BOTTOM); + bool within_bounds = (position.x >= left && position.x <= right && position.y >= top + && position.y <= bottom); + //if not within bounds do a reset or stop velocity if (!within_bounds) { if (emitter.data.boundary.reset_on_exit) { particle.active = false; } else { particle.velocity = {0, 0}; - if (position.x < LEFT) particle.position.x = LEFT; - else if (position.x > RIGHT) particle.position.x = RIGHT; - if (position.y < TOP) particle.position.y = TOP; - else if (position.y > BOTTOM) particle.position.y = BOTTOM; + if (position.x < left) particle.position.x = left; + else if (position.x > right) particle.position.x = right; + if (position.y < top) particle.position.y = top; + else if (position.y > bottom) particle.position.y = bottom; } } } -- cgit v1.2.3 From 512aa7f54b88994d3095971b2001930b4e612947 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 20:44:32 +0100 Subject: make format --- src/crepe/Particle.h | 3 ++- src/crepe/api/ParticleEmitter.cpp | 6 +++-- src/crepe/api/ParticleEmitter.h | 5 ++-- src/crepe/system/ParticleSystem.cpp | 10 ++++---- src/crepe/system/ParticleSystem.h | 1 - src/example/game.cpp | 31 ++++++++++++------------ src/example/rendering_particle.cpp | 1 - src/test/ParticleTest.cpp | 48 ++++++++++++++++++------------------- 8 files changed, 53 insertions(+), 52 deletions(-) (limited to 'src/crepe/system') diff --git a/src/crepe/Particle.h b/src/crepe/Particle.h index 0170117..ee0cd66 100644 --- a/src/crepe/Particle.h +++ b/src/crepe/Particle.h @@ -41,7 +41,8 @@ public: * \param velocity The initial velocity of the particle. * \param angle The angle of the particle's trajectory or orientation. */ - void reset(unsigned int lifespan, const vec2 & position, const vec2 & velocity, float angle); + void reset(unsigned int lifespan, const vec2 & position, const vec2 & velocity, + float angle); /** * \brief Updates the particle's state. * diff --git a/src/crepe/api/ParticleEmitter.cpp b/src/crepe/api/ParticleEmitter.cpp index 1e9cfaa..4f54bbd 100644 --- a/src/crepe/api/ParticleEmitter.cpp +++ b/src/crepe/api/ParticleEmitter.cpp @@ -3,9 +3,11 @@ using namespace crepe; -ParticleEmitter::ParticleEmitter(game_object_id_t game_object_id, const Sprite & sprite, const Data & data) +ParticleEmitter::ParticleEmitter(game_object_id_t game_object_id, const Sprite & sprite, + const Data & data) : Component(game_object_id), - sprite(sprite), data(data) { + sprite(sprite), + data(data) { for (size_t i = 0; i < this->data.max_particles; i++) { this->particles.emplace_back(); } diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index 5b8e8e3..be970f5 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -78,18 +78,19 @@ public: * \param game_object_id Identifier for the game object using this emitter. * \param data Configuration data defining particle properties. */ - ParticleEmitter(game_object_id_t game_object_id, const Sprite & sprite,const Data & data); + ParticleEmitter(game_object_id_t game_object_id, const Sprite & sprite, const Data & data); public: //! Configuration data for particle emission settings. Data data; + private: //! Only ParticleSystem can move and read particles friend ParticleSystem; //! Only RenderSystem can read particles friend RenderSystem; //! Saves time left over from last update event. - float spawn_accumulator = 0; + float spawn_accumulator = 0; //! collection of particles std::vector particles; }; diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index f98f245..31a4e9e 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -31,8 +31,8 @@ void ParticleSystem::update() { while (emitter.spawn_accumulator >= 1.0) { this->emit_particle(emitter, transform); emitter.spawn_accumulator -= 1.0; - } - + } + // Update all particles for (Particle & particle : emitter.particles) { if (particle.active) { @@ -49,9 +49,11 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & constexpr float DEG_TO_RAD = M_PI / 180.0; vec2 initial_position = emitter.data.position + transform.position; - float random_angle = this->generate_random_angle(emitter.data.min_angle, emitter.data.max_angle); + float random_angle + = this->generate_random_angle(emitter.data.min_angle, emitter.data.max_angle); - float random_speed = this->generate_random_speed(emitter.data.min_speed, emitter.data.max_speed); + float random_speed + = this->generate_random_speed(emitter.data.min_speed, emitter.data.max_speed); float angle_radians = random_angle * DEG_TO_RAD; vec2 velocity diff --git a/src/crepe/system/ParticleSystem.h b/src/crepe/system/ParticleSystem.h index 95a2bd5..154521d 100644 --- a/src/crepe/system/ParticleSystem.h +++ b/src/crepe/system/ParticleSystem.h @@ -23,7 +23,6 @@ public: void update() override; private: - /** * \brief Emits a particle from the specified emitter based on its emission properties. * diff --git a/src/example/game.cpp b/src/example/game.cpp index 279648e..2d25153 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -261,24 +261,23 @@ public: = false; Asset img5{"asset/texture/square.png"}; - - GameObject particle = mgr.new_object( "Name", "Tag", vec2{screen_size_width / 2, screen_size_height / 2}, 0, 1); - auto & particle_image = particle.add_component(img5, Sprite::Data{.size = {5, 5},}); - auto & test = particle.add_component(particle_image,ParticleEmitter::Data{ - .position = {0, 0}, - .max_particles = 256, - .emission_rate = 50, - .min_speed = 10, - .max_speed = 20, - .min_angle = -20, - .max_angle = 20, - .begin_lifespan = 0, - .end_lifespan = 5, - } - ); - + auto & particle_image = particle.add_component(img5, Sprite::Data{ + .size = {5, 5}, + }); + auto & test + = particle.add_component(particle_image, ParticleEmitter::Data{ + .position = {0, 0}, + .max_particles = 256, + .emission_rate = 50, + .min_speed = 10, + .max_speed = 20, + .min_angle = -20, + .max_angle = 20, + .begin_lifespan = 0, + .end_lifespan = 5, + }); } string get_name() const { return "scene1"; } diff --git a/src/example/rendering_particle.cpp b/src/example/rendering_particle.cpp index 2b5c041..add43f4 100644 --- a/src/example/rendering_particle.cpp +++ b/src/example/rendering_particle.cpp @@ -17,7 +17,6 @@ using namespace crepe; using namespace std; - class TestScene : public Scene { public: diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index a9a26c6..8ffb140 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -1,6 +1,4 @@ #include "api/Asset.h" -#include -#include #include #include #include @@ -8,18 +6,18 @@ #include #include #include +#include +#include #define protected public #define private public #include #include #include - using namespace std; using namespace std::chrono_literals; using namespace crepe; - class ParticlesTest : public ::testing::Test { Mediator m; @@ -44,25 +42,25 @@ public: .size = {10, 10}, }); - game_object.add_component(test_sprite,ParticleEmitter::Data{ - .position = {0, 0}, - .max_particles = 100, - .emission_rate = 0, - .min_speed = 0, - .max_speed = 0, - .min_angle = 0, - .max_angle = 0, - .begin_lifespan = 0, - .end_lifespan = 0, - .force_over_time = vec2{0, 0}, - .boundary{ - .width = 0, - .height = 0, - .offset = vec2{0, 0}, - .reset_on_exit = false, - }, - }); - + game_object.add_component(test_sprite, + ParticleEmitter::Data{ + .position = {0, 0}, + .max_particles = 100, + .emission_rate = 0, + .min_speed = 0, + .max_speed = 0, + .min_angle = 0, + .max_angle = 0, + .begin_lifespan = 0, + .end_lifespan = 0, + .force_over_time = vec2{0, 0}, + .boundary{ + .width = 0, + .height = 0, + .offset = vec2{0, 0}, + .reset_on_exit = false, + }, + }); } transforms = mgr.get_components_by_id(0); Transform & transform = transforms.front().get(); @@ -209,6 +207,6 @@ TEST_F(ParticlesTest, boundaryParticleStop) { EXPECT_NEAR(std::abs(emitter.particles[0].position.x), emitter.data.boundary.height / 2, TOLERANCE); if (emitter.particles[0].velocity.y != 0) - EXPECT_NEAR(std::abs(emitter.particles[0].position.y), - emitter.data.boundary.width / 2, TOLERANCE); + EXPECT_NEAR(std::abs(emitter.particles[0].position.y), emitter.data.boundary.width / 2, + TOLERANCE); } -- cgit v1.2.3 From ff2f5120b53792fe5e8cfd45a4335680baf487cf Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 14 Dec 2024 20:00:40 +0100 Subject: updated offset --- src/crepe/system/ParticleSystem.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/crepe/system') diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index 31a4e9e..e61f0ce 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -48,7 +48,7 @@ void ParticleSystem::update() { void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & transform) { constexpr float DEG_TO_RAD = M_PI / 180.0; - vec2 initial_position = emitter.data.position + transform.position; + vec2 initial_position = emitter.data.offset + transform.position; float random_angle = this->generate_random_angle(emitter.data.min_angle, emitter.data.max_angle); @@ -69,7 +69,7 @@ void ParticleSystem::emit_particle(ParticleEmitter & emitter, const Transform & } void ParticleSystem::check_bounds(ParticleEmitter & emitter, const Transform & transform) { - vec2 offset = emitter.data.boundary.offset + transform.position + emitter.data.position; + vec2 offset = emitter.data.boundary.offset + transform.position + emitter.data.offset; float half_width = emitter.data.boundary.width / 2.0; float half_height = emitter.data.boundary.height / 2.0; -- cgit v1.2.3 From 20d19e3c714d3e8ca3e35c170c07c563ecc719bb Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 14 Dec 2024 21:30:32 +0100 Subject: fixed bug with lower than one particle per second --- src/crepe/system/ParticleSystem.cpp | 2 +- src/example/game.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/crepe/system') diff --git a/src/crepe/system/ParticleSystem.cpp b/src/crepe/system/ParticleSystem.cpp index e61f0ce..35a1d41 100644 --- a/src/crepe/system/ParticleSystem.cpp +++ b/src/crepe/system/ParticleSystem.cpp @@ -27,7 +27,7 @@ void ParticleSystem::update() { = mgr.get_components_by_id(emitter.game_object_id).front().get(); // Emit particles based on emission_rate - emitter.spawn_accumulator = emitter.data.emission_rate * dt; + emitter.spawn_accumulator += emitter.data.emission_rate * dt; while (emitter.spawn_accumulator >= 1.0) { this->emit_particle(emitter, transform); emitter.spawn_accumulator -= 1.0; diff --git a/src/example/game.cpp b/src/example/game.cpp index ed6afac..db0d04b 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -271,7 +271,7 @@ public: = particle.add_component(particle_image, ParticleEmitter::Data{ .offset = {0, 0}, .max_particles = 256, - .emission_rate = 50, + .emission_rate = 1, .min_speed = 10, .max_speed = 20, .min_angle = -20, -- cgit v1.2.3