From 68761c9291359dd830a58b929aaa862cf717dc84 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Mon, 28 Apr 2014 19:12:51 +0200 Subject: [PATCH] Angular velocity support --- src/Components/Particle.h | 2 +- src/Components/ParticleEmitter.h | 2 +- src/GameWorld.cpp | 6 ++-- src/Systems/ParticleSystem.cpp | 53 +++++++++++++++++++++----------- src/Systems/ParticleSystem.h | 3 ++ 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/Components/Particle.h b/src/Components/Particle.h index dfa3075..a4725b3 100644 --- a/src/Components/Particle.h +++ b/src/Components/Particle.h @@ -15,7 +15,7 @@ namespace Components std::vector ScaleSpectrum; double LifeTime; std::vector VelocitySpectrum; - std::vector AngularVelocitySpectrum; + std::vector AngularVelocitySpectrum; virtual Particle* Clone() const override { return new Particle(*this); } }; diff --git a/src/Components/ParticleEmitter.h b/src/Components/ParticleEmitter.h index afb5622..6ddd302 100755 --- a/src/Components/ParticleEmitter.h +++ b/src/Components/ParticleEmitter.h @@ -29,7 +29,7 @@ struct ParticleEmitter : Component float SpreadAngle; double LifeTime; std::vector VelocitySpectrum; - std::vector AngularVelocitySpectrum; + std::vector AngularVelocitySpectrum; virtual ParticleEmitter* Clone() const override { return new ParticleEmitter(*this); } diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 8d75f10..f63c86a 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -101,9 +101,9 @@ void GameWorld::Initialize() transform->Position = glm::vec3(i * 10, 20, 0); auto emitter = AddComponent(ent, "ParticleEmitter"); emitter->LifeTime = 4; - emitter->SpawnCount = 4; - emitter->SpreadAngle = glm::pi()/4; - emitter->SpawnFrequency = 0.08; + emitter->SpawnCount = 1; + emitter->SpreadAngle = glm::pi()/20; + emitter->SpawnFrequency = 0.008; auto model = AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index e274e3f..5ee695c 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -47,19 +47,18 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID } else { - float timeProgress = timeLived / particleComponent->LifeTime; - // The difference between the start and end value - /*float deltaColor = glm::abs(particleComponent->ColorSpectrum[0].r - particleComponent->ColorSpectrum[1].r); - it->color.r = particleComponent->ColorSpectrum[0].r + deltaColor * timeProgress; - deltaColor = glm::abs(particleComponent->ColorSpectrum[0].g - particleComponent->ColorSpectrum[1].g); - it->color.g = particleComponent->ColorSpectrum[0].g + deltaColor * timeProgress; - deltaColor = glm::abs(particleComponent->ColorSpectrum[0].b - particleComponent->ColorSpectrum[1].b); - it->color.b = particleComponent->ColorSpectrum[0].b + deltaColor * timeProgress;*/ - - ScaleInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale); - VelocityInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity); - + //FIX: calculate once + float timeProgress = timeLived / particleComponent->LifeTime; + //ColorInterpolation(timeProgress, particleComponent->ColorSpectrum, color); + if(particleComponent->ScaleSpectrum.size() > 1) + ScaleInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale); + if(particleComponent->VelocitySpectrum.size() > 1) + VelocityInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity); + if(particleComponent->AngularVelocitySpectrum.size() > 1) + AngularVelocityInterpolation(timeProgress, particleComponent->AngularVelocitySpectrum, it->AngularVelocity); + + transformComponent->Orientation *= glm::angleAxis(it->AngularVelocity, glm::vec3(0, 0, 1)); transformComponent->Position += transformComponent->Velocity * (float)dt; it++; @@ -103,12 +102,12 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) auto particle = m_World->AddComponent(ent, "Particle"); particle->LifeTime = emitterComponent->LifeTime; - particle->ScaleSpectrum.push_back(4); //TEMP - particle->ScaleSpectrum.push_back(0); //TEMP + particle->ScaleSpectrum.push_back(1); //TEMP + particle->ScaleSpectrum.push_back(1); //TEMP particle->VelocitySpectrum.push_back(particleTransform->Velocity); //TEMP particle->VelocitySpectrum.push_back(testVel); //TEMP -// particle->AngularVelocitySpectrum.push_back(); -// particle->AngularVelocitySpectrum.push_back(); + //particle->AngularVelocitySpectrum.push_back(0.f); + particle->AngularVelocitySpectrum.push_back(-glm::pi()/10); // Color startColor = {.4f, .45f, .2f}; // particle->ColorSpectrum.push_back(startColor); @@ -120,8 +119,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) ParticleData data; data.ParticleID = ent; data.SpawnTime = glfwGetTime(); -// data.color = particle->ColorSpectrum[0]; -// data.Scale = particle->ScaleSpectrum[0]; + data.AngularVelocity = particle->AngularVelocitySpectrum[0]; m_ParticleEmitter[emitterID].push_back(data); } @@ -157,4 +155,23 @@ void Systems::ParticleSystem::VelocityInterpolation(double timeProgress, std::ve if(velocitySpectrum[0].z > velocitySpectrum[1].z) deltaVelocity *= -1; velocity.z = velocitySpectrum[0].z + deltaVelocity * timeProgress; +} + +void Systems::ParticleSystem::ColorInterpolation(double timeProgress, std::vector colorSpectrum, Color &color) +{ + float deltaColor = glm::abs(colorSpectrum[0].r - colorSpectrum[1].r); + color.r = colorSpectrum[0].r + deltaColor * timeProgress; + deltaColor = glm::abs(colorSpectrum[0].g - colorSpectrum[1].g); + color.g = colorSpectrum[0].g + deltaColor * timeProgress; + deltaColor = glm::abs(colorSpectrum[0].b - colorSpectrum[1].b); + color.b = colorSpectrum[0].b + deltaColor * timeProgress; +} + +void Systems::ParticleSystem::AngularVelocityInterpolation(double timeProgress, std::vector spectrum, float &angularVelocity) +{ + float deltaAngularVelocity = glm::abs(spectrum[0] - spectrum[1]); + if(spectrum[0] > spectrum[1]) + deltaAngularVelocity *= -1; + angularVelocity = spectrum[0] + deltaAngularVelocity * timeProgress; + } \ No newline at end of file diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 33d14ae..9e10b99 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -20,6 +20,7 @@ namespace Systems EntityID ParticleID; double SpawnTime; float Scale; + float AngularVelocity; Color color; }; @@ -35,6 +36,8 @@ private: float RandomizeAngle(float spreadAngle); void ScaleInterpolation(double timeProgress, std::vector scaleSpectrum, glm::vec3 &scale); void VelocityInterpolation(double timeProgress, std::vector velocitySpectrum, glm::vec3 &velocity); + void ColorInterpolation(double timeProgress, std::vector colorSpectrum, Color &color); + void AngularVelocityInterpolation(double timeProgress, std::vector spectrum, float &angularVelocity); void Billboard(); std::map> m_ParticleEmitter; std::map m_TimeSinceLastSpawn;