From 3b34a718a9f110f5e41a83fdd8d1d7675a14bb12 Mon Sep 17 00:00:00 2001 From: Sebastian Gaida <gaida@ca-digit.com> Date: Mon, 14 Jun 2021 18:14:59 +0200 Subject: [PATCH] [#69] random paricles are fun --- projects/particle_simulation/src/Particle.cpp | 8 ++++++++ projects/particle_simulation/src/Particle.hpp | 4 ++++ .../particle_simulation/src/ParticleSystem.cpp | 5 +++++ .../particle_simulation/src/ParticleSystem.hpp | 5 +++++ projects/particle_simulation/src/main.cpp | 14 ++++++++++---- 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/projects/particle_simulation/src/Particle.cpp b/projects/particle_simulation/src/Particle.cpp index cdb69f7b..3a051c5b 100644 --- a/projects/particle_simulation/src/Particle.cpp +++ b/projects/particle_simulation/src/Particle.cpp @@ -19,6 +19,14 @@ void Particle::setPosition( const glm::vec3 pos ){ m_position = pos; } +const glm::vec3& Particle::getVelocity()const{ + return m_velocity; +} + +void Particle::setVelocity( const glm::vec3 vel ){ + m_velocity = vel; +} + void Particle::update( const float delta ){ m_position += m_velocity * delta; } diff --git a/projects/particle_simulation/src/Particle.hpp b/projects/particle_simulation/src/Particle.hpp index 9c08631e..bdac2b29 100644 --- a/projects/particle_simulation/src/Particle.hpp +++ b/projects/particle_simulation/src/Particle.hpp @@ -11,6 +11,10 @@ public: void setPosition( const glm::vec3 pos ); + const glm::vec3& Particle::getVelocity()const; + + void setVelocity( const glm::vec3 vel ); + void update( const float delta ); const bool isAlive()const; diff --git a/projects/particle_simulation/src/ParticleSystem.cpp b/projects/particle_simulation/src/ParticleSystem.cpp index 4c3a9286..4eb9d8b1 100644 --- a/projects/particle_simulation/src/ParticleSystem.cpp +++ b/projects/particle_simulation/src/ParticleSystem.cpp @@ -1,5 +1,9 @@ #include "ParticleSystem.hpp" +ParticleSystem::ParticleSystem(){ + m_rdmVel = std::uniform_real_distribution<float> (-0.1f,0.1f); +} + const std::vector<Particle>& ParticleSystem::getParticles() const{ return m_particles; } @@ -15,6 +19,7 @@ void ParticleSystem::updateParticles( const float deltaTime ){ for(Particle& particle :m_particles){ bool alive = particle.isAlive(); particle.setPosition( particle.getPosition() * static_cast<float>(alive) + static_cast<float>(!alive) * m_respawnPos ); + particle.setVelocity( particle.getVelocity() * static_cast<float>(alive) + static_cast<float>(!alive) * glm::vec3(m_rdmVel(m_rdmEngine), m_rdmVel(m_rdmEngine),m_rdmVel(m_rdmEngine))); particle.setLifeTime( (particle.getLifeTime() * alive + !alive * m_maxLifeTime ) - deltaTime ); particle.update(deltaTime); } diff --git a/projects/particle_simulation/src/ParticleSystem.hpp b/projects/particle_simulation/src/ParticleSystem.hpp index e4ec3b36..4eb83ebb 100644 --- a/projects/particle_simulation/src/ParticleSystem.hpp +++ b/projects/particle_simulation/src/ParticleSystem.hpp @@ -2,10 +2,12 @@ #include <vector> #include "Particle.hpp" +#include <random> class ParticleSystem { public: + ParticleSystem(); const std::vector<Particle> &getParticles() const; void addParticle( const Particle particle ); void addParticles( const std::vector<Particle> particles ); @@ -17,4 +19,7 @@ private: std::vector<Particle> m_particles; glm::vec3 m_respawnPos = glm::vec3(0.f); float m_maxLifeTime = 1.f; + + std::uniform_real_distribution<float> m_rdmVel; + std::default_random_engine m_rdmEngine; }; \ No newline at end of file diff --git a/projects/particle_simulation/src/main.cpp b/projects/particle_simulation/src/main.cpp index ef718ceb..3d4e33c5 100644 --- a/projects/particle_simulation/src/main.cpp +++ b/projects/particle_simulation/src/main.cpp @@ -4,6 +4,7 @@ #include <vkcv/camera/CameraManager.hpp> #include <chrono> #include "ParticleSystem.hpp" +#include <random> int main(int argc, const char** argv) { const char* applicationName = "Particlesystem"; @@ -126,13 +127,18 @@ int main(int argc, const char** argv) { pos = glm::vec2(static_cast<float>(offsetX), static_cast<float>(offsetY)); }); + std::uniform_real_distribution<float> rdmVel(-0.1f,0.1f); + std::default_random_engine rdmEngine; + ParticleSystem particleSystem; particleSystem.setMaxLifeTime(3.f); - glm::vec3 vel = glm::vec3(0.f , 0.1f, 0.0f); + glm::vec3 vel0 = glm::vec3(rdmVel(rdmEngine) , rdmVel(rdmEngine), 0.0f); + glm::vec3 vel1 = glm::vec3(rdmVel(rdmEngine) , rdmVel(rdmEngine), 0.0f); + glm::vec3 vel2 = glm::vec3(rdmVel(rdmEngine) , rdmVel(rdmEngine), 0.0f); particleSystem.addParticles({ - Particle(glm::vec3(0.f, 1.f, 0.f), vel, 1.f), - Particle(glm::vec3( 0.2f, 0.1f, 0.f), vel, 2.f), - Particle(glm::vec3(0.15f, 0.f, 0.1f), vel, 3.f)}); + Particle(glm::vec3(0.f, 1.f, 0.f), vel0, 1.f), + Particle(glm::vec3( 0.2f, 0.1f, 0.f), vel1, 2.f), + Particle(glm::vec3(0.15f, 0.f, 0.1f), vel2, 3.f)}); std::vector<glm::mat4> modelMatrices; std::vector<vkcv::DrawcallInfo> drawcalls; -- GitLab