aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjaroWMR <jarorutjes07@gmail.com>2024-10-07 16:39:26 +0200
committerjaroWMR <jarorutjes07@gmail.com>2024-10-07 16:39:26 +0200
commit5cf5cf64d6f9597849ed5558da4f1bc201165fed (patch)
treeb0b02e8b71c5e8e5afba4212daa999c5cc04bd26 /src
parent096e0d0a7199ec9a4059fd5ef8a06a3cf8fcae83 (diff)
updated system
Diffstat (limited to 'src')
-rw-r--r--src/crepe/Particle.cpp3
-rw-r--r--src/crepe/ParticleEmitter.cpp4
-rw-r--r--src/crepe/ParticleEmitter.hpp4
-rw-r--r--src/crepe/ParticleSystem.cpp21
-rw-r--r--src/crepe/main.cpp37
5 files changed, 42 insertions, 27 deletions
diff --git a/src/crepe/Particle.cpp b/src/crepe/Particle.cpp
index f6bbd34..3254b5b 100644
--- a/src/crepe/Particle.cpp
+++ b/src/crepe/Particle.cpp
@@ -1,5 +1,5 @@
#include "Particle.hpp"
-
+#include <iostream>
Particle::Particle(float lifespan, Position position, Position velocity)
: lifespan(lifespan), position(position), velocity(velocity), timeInLife(0.0f) {}
@@ -11,5 +11,6 @@ void Particle::update(float deltaTime) {
}
bool Particle::isAlive() const {
+ std::cout << "lifespan" << lifespan << std::endl;
return timeInLife < lifespan;
}
diff --git a/src/crepe/ParticleEmitter.cpp b/src/crepe/ParticleEmitter.cpp
index 69f5ace..f5c18db 100644
--- a/src/crepe/ParticleEmitter.cpp
+++ b/src/crepe/ParticleEmitter.cpp
@@ -1,8 +1,8 @@
#include "ParticleEmitter.hpp"
#include <ctime>
-ParticleEmitter::ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset)
- : m_maxParticles(maxParticles), m_emissionRate(emissionRate), m_speed(speed), m_speedOffset(speedOffset), m_position{0, 0} {
+ParticleEmitter::ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset, float m_beginLifespan, float m_endLifespan)
+ : m_maxParticles(maxParticles), m_emissionRate(emissionRate), m_speed(speed), m_speedOffset(speedOffset), m_position{0, 0}, m_beginLifespan(m_beginLifespan),m_endLifespan(m_endLifespan) {
std::srand(static_cast<unsigned int>(std::time(nullptr))); // initialize random seed
m_minAngle = (360 + angle - (angleOffset % 360)) % 360; // calculate minAngle
diff --git a/src/crepe/ParticleEmitter.hpp b/src/crepe/ParticleEmitter.hpp
index 6826531..f647c27 100644
--- a/src/crepe/ParticleEmitter.hpp
+++ b/src/crepe/ParticleEmitter.hpp
@@ -5,7 +5,7 @@
class ParticleEmitter {
public:
- ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset);
+ ParticleEmitter(unsigned int maxParticles, unsigned int emissionRate, unsigned int speed, unsigned int speedOffset, unsigned int angle, unsigned int angleOffset,float m_beginLifespan,float m_endLifespan);
struct Position { //struct to hold position
float x;
float y;
@@ -18,6 +18,8 @@ public:
unsigned int m_speedOffset; //offset for random speed variation
unsigned int m_minAngle; //min angle of particle emission
unsigned int m_maxAngle; //max angle of particle emission
+ float m_beginLifespan; //begin Lifespan of particle (only visual)
+ float m_endLifespan; //begin Lifespan of particle
std::vector<Particle> particles; //collection of particles
};
diff --git a/src/crepe/ParticleSystem.cpp b/src/crepe/ParticleSystem.cpp
index 367f56c..f3cb939 100644
--- a/src/crepe/ParticleSystem.cpp
+++ b/src/crepe/ParticleSystem.cpp
@@ -10,13 +10,12 @@ ParticleSystem::ParticleSystem() : m_elapsedTime(0.0f) {} // Initialize m_elaps
void ParticleSystem::update(float deltaTime, std::vector<ParticleEmitter>& emitters) {
for (ParticleEmitter& emitter : emitters) {
- m_elapsedTime = deltaTime;
-
- //spawn new particles if enough time passed and the max is not achieved
- while (m_elapsedTime >= (1.0f / emitter.m_emissionRate) && emitter.particles.size() < emitter.m_maxParticles) {
- m_elapsedTime -= (1.0f / emitter.m_emissionRate);
+ float updateAmount = 1/static_cast<float>(emitter.m_emissionRate);
+ for (float i = 0; i < deltaTime; i += updateAmount)
+ {
emitParticle(emitter);
}
+
//update/move particles afterwards delete if not alive.
std::vector<Particle>::iterator it = emitter.particles.begin();
while (it != emitter.particles.end()) {
@@ -32,7 +31,7 @@ void ParticleSystem::update(float deltaTime, std::vector<ParticleEmitter>& emitt
}
void ParticleSystem::emitParticle(ParticleEmitter& emitter) {
- std::cout << "new emitter:" << std::endl;
+ // std::cout << "new emitter:" << std::endl;
Particle::Position initialPosition = { emitter.m_position.x, emitter.m_position.y };
float randomAngle = 0.0f;
//check if value is overthe 360 degrees
@@ -45,18 +44,16 @@ void ParticleSystem::emitParticle(ParticleEmitter& emitter) {
randomAngle = emitter.m_minAngle + (std::rand() % (static_cast<int>(emitter.m_maxAngle - emitter.m_minAngle + 1)));
}
- std::cout << "randomAngle:" << randomAngle << std::endl;
+
float angleInRadians = randomAngle * (M_PI / 180.0f);
float randomSpeedOffset = (static_cast<float>(std::rand()) / RAND_MAX) * (2 * emitter.m_speedOffset) - emitter.m_speedOffset;
float velocityX = (emitter.m_speed + randomSpeedOffset) * std::cos(angleInRadians);
float velocityY = (emitter.m_speed + randomSpeedOffset) * std::sin(angleInRadians);
- std::cout << "velocityX:" << velocityX << std::endl;
- std::cout << "velocityY:" << velocityY << std::endl;
Particle::Position initialVelocity = {
velocityX,
velocityY
};
- std::cout << "initialVelocityX:" << initialVelocity.x << std::endl;
- std::cout << "initialVelocityY:" << initialVelocity.y << std::endl;
- emitter.particles.emplace_back(2.0f, initialPosition, initialVelocity);
+ // std::cout << "emitter.m_endLifespan:" << emitter.m_endLifespan << std::endl;
+
+ emitter.particles.emplace_back(emitter.m_endLifespan, initialPosition, initialVelocity);
}
diff --git a/src/crepe/main.cpp b/src/crepe/main.cpp
index 40eb04f..314dbed 100644
--- a/src/crepe/main.cpp
+++ b/src/crepe/main.cpp
@@ -5,6 +5,7 @@
#include "ParticleEmitter.hpp"
#include "ParticleSystem.hpp"
#include "Particle.hpp"
+#include <chrono>
const int WINDOW_WIDTH = 800;
const int WINDOW_HEIGHT = 600;
@@ -19,29 +20,43 @@ int main(int argc, char* argv[]) {
ParticleSystem particleSystem;
- unsigned int maxParticles = 100; // maximum number of particles
- unsigned int emissionRate = 10; // particles created per second
+ unsigned int maxParticles = 10; // maximum number of particles
+ unsigned int emissionRate = 1; // particles created per second
unsigned int speed = 50; // base speed of particles
unsigned int speedOffset = 10; // random offset for particle speed
unsigned int angle = 90; // base angle of particle emission
unsigned int angleOffset = 30; // random offset for particle angle
-
- ParticleEmitter emitter1(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset);
- emitter1.m_position = {200, 200}; // set the position of the first emitter
-
- ParticleEmitter emitter2(maxParticles, emissionRate, speed, speedOffset, angle - 90, angleOffset); // Another emitter
- emitter2.m_position = {200, 150}; // set the position of the second emitter
-
- std::vector<ParticleEmitter> emitters = { emitter2 }; // array of emitters
-
+ float beginLifespan = 0.0f; // beginning lifespan of particles
+ float endLifespan = 2.0f; // ending lifespan of particles
+
+ // Vector to hold all the emitters
+ std::vector<ParticleEmitter> emitters;
+
+ // Loop to create 1000 emitters
+ for (unsigned int i = 0; i < 100; ++i) {
+ ParticleEmitter emitter(maxParticles, emissionRate, speed, speedOffset, angle, angleOffset, beginLifespan, endLifespan);
+
+ // Set a position for each emitter, modifying the position for demonstration
+ emitter.m_position = {static_cast<float>(200 + (i % 100)), static_cast<float>(200 + (i / 100) * 10)}; // Adjust position for each emitter
+
+ emitters.push_back(emitter); // Add the emitter to the vector
+ }
float deltaTime = 0.1f;
bool running = true;
while (running) {
app.handleEvents(running);
+ // Start timing
+ auto start = std::chrono::high_resolution_clock::now();
+
particleSystem.update(deltaTime, emitters); // update particle system with delta time and emitters
+ // End timing
+ auto end = std::chrono::high_resolution_clock::now();
+ std::chrono::duration<float, std::milli> duration = end - start; // get duration in milliseconds
+
+ std::cout << "Update took " << duration.count() << " ms" << std::endl;
app.clearScreen();
// render particles using the drawSquare method from SDLApp