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/Particle.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/crepe/Particle.h') diff --git a/src/crepe/Particle.h b/src/crepe/Particle.h index d0397c9..4fa93a1 100644 --- a/src/crepe/Particle.h +++ b/src/crepe/Particle.h @@ -24,13 +24,13 @@ public: //! Accumulated force affecting the particle over time. vec2 force_over_time; //! Total lifespan of the particle in milliseconds. - uint32_t lifespan; + unsigned int 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. - uint32_t time_in_life = 0; + unsigned int time_in_life = 0; //! The angle at which the particle is oriented or moving. - double angle = 0; + float angle = 0; /** * \brief Resets the particle with new properties. @@ -43,7 +43,7 @@ public: * \param velocity The initial velocity of the particle. * \param angle The angle of the particle's trajectory or orientation. */ - void reset(uint32_t lifespan, const vec2 & position, const vec2 & velocity, double angle); + void reset(unsigned int lifespan, const vec2 & position, const vec2 & velocity, float angle); /** * \brief Updates the particle's state. * -- 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/Particle.h') 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 5ad9f5c81ae20f4b7aa2c1773bce8906096754f3 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 13 Dec 2024 18:49:51 +0100 Subject: friend explained --- src/crepe/Particle.h | 2 -- src/crepe/api/ParticleEmitter.h | 10 ++++++---- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'src/crepe/Particle.h') diff --git a/src/crepe/Particle.h b/src/crepe/Particle.h index 49fec1f..0170117 100644 --- a/src/crepe/Particle.h +++ b/src/crepe/Particle.h @@ -14,8 +14,6 @@ namespace crepe { * can also be reset or stopped. */ class Particle { - // TODO: add friend particleSsytem and rendersystem. Unit test will fail. - public: //! Position of the particle in 2D space. vec2 position; diff --git a/src/crepe/api/ParticleEmitter.h b/src/crepe/api/ParticleEmitter.h index e0b117a..5b8e8e3 100644 --- a/src/crepe/api/ParticleEmitter.h +++ b/src/crepe/api/ParticleEmitter.h @@ -54,11 +54,11 @@ public: //! maximum number of particles const unsigned int max_particles = 256; //! rate of particle emission per second - float emission_rate = 1; + float emission_rate = 50; //! min speed of the particles - float min_speed = 1; + float min_speed = 100; //! min speed of the particles - float max_speed = 2; + float max_speed = 100; //! min angle of particle emission float min_angle = 0; //! max angle of particle emission @@ -84,9 +84,11 @@ public: //! Configuration data for particle emission settings. Data data; private: - //! Saves time left over from last update event. + //! 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; //! collection of particles std::vector particles; -- 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/Particle.h') 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