From 28ac8980f2fdc0edc4daa295dd324745353cd657 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Sun, 27 Apr 2014 03:20:14 +0200 Subject: [PATCH] tested and fixed scale and velocity interpolation. Fix: could not go from lesser value to larger. Added methods for interpolation. --- src/GameWorld.cpp | 4 +- src/Systems/ParticleSystem.cpp | 94 ++++++++++++++++++---------------- src/Systems/ParticleSystem.h | 3 +- 3 files changed, 53 insertions(+), 48 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 3edaf44..d4bba0f 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -97,12 +97,12 @@ void GameWorld::Initialize() // Particle emitter auto ent = CreateEntity(); auto transform = AddComponent(ent, "Transform"); - transform->Orientation = glm::angleAxis(glm::pi()/4, glm::vec3(0,1,0)); + transform->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); transform->Position = glm::vec3(i * 10, 20, 0); auto emitter = AddComponent(ent, "ParticleEmitter"); emitter->LifeTime = 4; emitter->SpawnCount = 4; - emitter->SpreadAngle = glm::pi()/6; + emitter->SpreadAngle = glm::pi()/4; emitter->SpawnFrequency = 0.08; 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 a5479f2..b833de5 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -37,10 +37,8 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID { EntityID particleID = (it)->ParticleID; auto transformComponent = m_World->GetComponent(particleID, "Transform"); - - transformComponent->Position += transformComponent->Velocity; - auto particleComponent = m_World->GetComponent(particleID, "Particle"); + double timeLived = glfwGetTime() - it->SpawnTime; if(timeLived > particleComponent->LifeTime) { @@ -54,28 +52,20 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID it++; } - // Interpolates the color for each color channel by the start and end value. Decides how much the color should be interpolated based on time. - /*// How big fraction the color is multiplied with 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); + /*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; + it->color.b = particleComponent->ColorSpectrum[0].b + deltaColor * timeProgress;*/ - //Interpolates the scale of the particle - float deltaScale = glm::abs(particleComponent->ScaleSpectrum[0] - particleComponent->ScaleSpectrum[1]); - it->Scale = particleComponent->ScaleSpectrum[0] + deltaScale * timeProgress; + + ScaleInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale); + VelocityInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity); - //Interpolates the velocity of the particle - float deltaVelocity = glm::abs(particleComponent->VelocitySpectrum[0].x - particleComponent->VelocitySpectrum[1].x); - it->Velocity.x = particleComponent->VelocitySpectrum[0].x + deltaVelocity * timeProgress; - deltaVelocity = glm::abs(particleComponent->VelocitySpectrum[0].y - particleComponent->VelocitySpectrum[1].y); - it->Velocity.y = particleComponent->VelocitySpectrum[0].y + deltaVelocity * timeProgress; - deltaVelocity = glm::abs(particleComponent->VelocitySpectrum[0].z - particleComponent->VelocitySpectrum[1].z); - it->Velocity.z = particleComponent->VelocitySpectrum[0].z + deltaVelocity * timeProgress;*/ + transformComponent->Position += transformComponent->Velocity; } } } @@ -90,8 +80,10 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, { auto emitterTransform = m_World->GetComponent(emitterID, "Transform"); glm::quat emitterOrientation = emitterTransform->Orientation; - float tempSpeed = 5 * dt; + + float tempSpeed = 4 * dt; glm::vec3 speed = glm::vec3(tempSpeed); + for(int i = 0; i < spawnCount; i++) { auto ent = m_World->CreateEntity(); @@ -104,41 +96,26 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, //The emitter's orientation as "start value" times the default direction for quaternion. Times the speed, and then rotate on x and y axis with the randomized spread angle. particleTransform->Velocity = emitterOrientation * glm::vec3(0, 0, -1) * speed * glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(1, 0, 0))) * - glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 1, 0))); + glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 1, 0))) * + glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 0, 1))); + glm::vec3 testVel = glm::vec3(particleTransform->Velocity.x, -particleTransform->Velocity.y * 1.5, particleTransform->Velocity.z); //TEMP auto particle = m_World->AddComponent(ent, "Particle"); particle->LifeTime = lifeTime; - - /*Color startColor = {.4f, .45f, .2f}; - particle->ColorSpectrum.push_back(startColor); - Color endColor = {0.f, 45.f, 23.f}; - particle->ColorSpectrum.push_back(endColor); - particle->ScaleSpectrum.push_back(1); - particle->ScaleSpectrum.push_back(30); - particle->VelocitySpectrum.push_back(glm::vec3(0, -.2, 0)); - particle->VelocitySpectrum.push_back(glm::vec3(0, -3, 0));*/ + particle->ScaleSpectrum.push_back(4); //TEMP + particle->ScaleSpectrum.push_back(1); //TEMP + particle->VelocitySpectrum.push_back(particleTransform->Velocity); //TEMP + particle->VelocitySpectrum.push_back(testVel); //TEMP + +// Color startColor = {.4f, .45f, .2f}; +// particle->ColorSpectrum.push_back(startColor); +// Color endColor = {0.f, 45.f, 23.f}; +// particle->ColorSpectrum.push_back(endColor); auto model = m_World->AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; - /*auto light = m_World->AddComponent(ent, "PointLight"); - light->Specular = glm::vec3(0.1f, 0.1f, 0.1f); - light->Diffuse = glm::vec3(1.f, 1.f, 0.f); - light->constantAttenuation = 0.03f; - light->linearAttenuation = 0.00009f; - light->quadraticAttenuation = 0.07f; - light->spotExponent = 0.0f;*/ - - - /*auto physics = m_World->AddComponent(ent, "Physics"); - physics->Mass = 1; - - auto physicShape = m_World->AddComponent(ent, "Box"); - physicShape->Width = 0.5; - physicShape->Height = 0.5; - physicShape->Depth = 0.5;*/ - ParticleData data; data.ParticleID = ent; data.SpawnTime = glfwGetTime(); @@ -149,7 +126,34 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, } } +//Randomizes between -spreadAngle/2 and spreadAngle/2 float Systems::ParticleSystem::RandomizeAngle(float spreadAngle) { return ((float)rand() / ((float)RAND_MAX + 1) * spreadAngle) - spreadAngle/2; +} + +//Interpolates the scale of the particle +void Systems::ParticleSystem::ScaleInterpolation(double timeProgress, std::vector scaleSpectrum, glm::vec3 &scale) +{ + float deltaScale = glm::abs(scaleSpectrum[0] - scaleSpectrum[1]); + if(scaleSpectrum[0] > scaleSpectrum[1]) + deltaScale *= -1; + scale = glm::vec3(scaleSpectrum[0] + deltaScale * timeProgress); +} + +//Interpolates the velocity of the particle +void Systems::ParticleSystem::VelocityInterpolation(double timeProgress, std::vector velocitySpectrum, glm::vec3 &velocity) +{ + float deltaVelocity = glm::abs(velocitySpectrum[0].x - velocitySpectrum[1].x); + if(velocitySpectrum[0].x > velocitySpectrum[1].x) + deltaVelocity *= -1; + velocity.x = velocitySpectrum[0].x + deltaVelocity * timeProgress; + deltaVelocity = glm::abs(velocitySpectrum[0].y - velocitySpectrum[1].y); + if (velocitySpectrum[0].y > velocitySpectrum[1].y) + deltaVelocity *= -1; + velocity.y = velocitySpectrum[0].y + deltaVelocity * timeProgress; + deltaVelocity = glm::abs(velocitySpectrum[0].z - velocitySpectrum[1].z); + if(velocitySpectrum[0].z > velocitySpectrum[1].z) + deltaVelocity *= -1; + velocity.z = velocitySpectrum[0].z + deltaVelocity * timeProgress; } \ No newline at end of file diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 059eb2f..8940794 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -37,7 +37,8 @@ private: std::map m_TimeSinceLastSpawn; float RandomizeAngle(float spreadAngle); - + void ScaleInterpolation(double timeProgress, std::vector scaleSpectrum, glm::vec3 &scale); + void VelocityInterpolation(double timeProgress, std::vector velocitySpectrum, glm::vec3 &velocity); }; }