aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-12-07 22:44:35 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-12-07 22:44:35 +0100
commit8c81bf4a33a13fc21dca7e3fe78a6dc334ac964b (patch)
tree591cf1d62731c6f206a9a54d011ddf462631c41a
parent31e4ba33bec0cc5c33d9668cd0244972523bf8ad (diff)
changed spawnrate of particles (bound to delta time)
-rw-r--r--src/crepe/Particle.cpp9
-rw-r--r--src/crepe/Particle.h7
-rw-r--r--src/crepe/api/ParticleEmitter.h2
-rw-r--r--src/crepe/system/ParticleSystem.cpp26
-rw-r--r--src/crepe/system/ParticleSystem.h11
5 files changed, 21 insertions, 34 deletions
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<ParticleEmitter> emitters = mgr.get_components_by_type<ParticleEmitter>();
+ 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<Transform>(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<int>(1.0 / fractional_part);
- return (count % denominator == 0) ? 1 : 0;
- }
-
- return static_cast<int>(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.
*
@@ -32,16 +33,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.
*