diff options
author | jaroWMR <jarorutjes07@gmail.com> | 2024-10-07 18:07:17 +0200 |
---|---|---|
committer | jaroWMR <jarorutjes07@gmail.com> | 2024-10-07 18:07:17 +0200 |
commit | 5f84969c851530ebc430be2cf8e99c945ff7a4a7 (patch) | |
tree | 9fcd2755dbfd456f20f0ad37c18c7281b81382b1 /src/crepe/ParticleSystem.cpp | |
parent | 5cf5cf64d6f9597849ed5558da4f1bc201165fed (diff) |
improved particle system from 90ms to ~ 0.7ms
Diffstat (limited to 'src/crepe/ParticleSystem.cpp')
-rw-r--r-- | src/crepe/ParticleSystem.cpp | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/src/crepe/ParticleSystem.cpp b/src/crepe/ParticleSystem.cpp index f3cb939..aff7a30 100644 --- a/src/crepe/ParticleSystem.cpp +++ b/src/crepe/ParticleSystem.cpp @@ -9,30 +9,32 @@ ParticleSystem::ParticleSystem() : m_elapsedTime(0.0f) {} // Initialize m_elapsedTime to 0 void ParticleSystem::update(float deltaTime, std::vector<ParticleEmitter>& emitters) { + // std::cout << "ParticleSystem update" << std::endl; for (ParticleEmitter& emitter : emitters) { float updateAmount = 1/static_cast<float>(emitter.m_emissionRate); for (float i = 0; i < deltaTime; i += updateAmount) { emitParticle(emitter); } + // std::cout << "after emit" << std::endl; //update/move particles afterwards delete if not alive. - std::vector<Particle>::iterator it = emitter.particles.begin(); - while (it != emitter.particles.end()) { - it->update(deltaTime); - if (!it->isAlive()) { - it = emitter.particles.erase(it); - } else { - ++it; + for (size_t j = 0; j < emitter.particles.size(); j++) + { + // std::cout << "update" << std::endl; + if(emitter.particles[j].active) + { + emitter.particles[j].update(deltaTime); } - } + } + } } void ParticleSystem::emitParticle(ParticleEmitter& emitter) { // std::cout << "new emitter:" << std::endl; - Particle::Position initialPosition = { emitter.m_position.x, emitter.m_position.y }; + Position initialPosition = { emitter.m_position.x, emitter.m_position.y }; float randomAngle = 0.0f; //check if value is overthe 360 degrees if(emitter.m_maxAngle < emitter.m_minAngle) @@ -49,11 +51,27 @@ void ParticleSystem::emitParticle(ParticleEmitter& emitter) { 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); - Particle::Position initialVelocity = { + Position initialVelocity = { velocityX, velocityY }; // std::cout << "emitter.m_endLifespan:" << emitter.m_endLifespan << std::endl; - - emitter.particles.emplace_back(emitter.m_endLifespan, initialPosition, initialVelocity); + for (size_t i = 0; i < emitter.particles.size(); i++) + { + if(!emitter.particles[i].active) + { + // std::cout << "active " << emitter.particles[i].active << std::endl; + // std::cout << "lifespan " << emitter.particles[i].lifespan << std::endl; + // std::cout << "timeInLife " << emitter.particles[i].timeInLife << std::endl; + // std::cout << "emitter.m_endLifespan" << emitter.m_endLifespan << std::endl; + // std::cout << "initialPositionx" << initialPosition.x << std::endl; + // std::cout << "initialPositiony" << initialPosition.y << std::endl; + // std::cout << "initialVelocityx" << initialVelocity.x << std::endl; + // std::cout << "initialVelocityy" << initialVelocity.y << std::endl; + emitter.particles[i].reset(emitter.m_endLifespan, initialPosition, initialVelocity); + break; + } + } + + //emitter.particles.emplace_back(emitter.m_endLifespan, initialPosition, initialVelocity); } |