From bf82e549aad41b5f4a189d13d9ab87557c3609f5 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Fri, 16 Oct 2015 12:43:40 +0200 Subject: [PATCH] Added VectorInterpolation function. Fixed time bug for particles. --- include/Physics/PhysicsSystem.h | 2 + src/game/Game/LevelSystem.cpp | 2 +- src/game/Physics/PhysicsSystem.cpp | 60 ++++++++++++++++++------------ 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/include/Physics/PhysicsSystem.h b/include/Physics/PhysicsSystem.h index fc5925e..bd37c74 100755 --- a/include/Physics/PhysicsSystem.h +++ b/include/Physics/PhysicsSystem.h @@ -107,6 +107,8 @@ namespace dd void CreateParticleEmitter(EntityID entity); void UpdateParticleEmitters(double dt); //TODO: Remove them and particles if needed. float ScalarInterpolation(float timeProgress, std::vector spectrum); + glm::vec3 VectorInterpolation(float timeProgress, std::vector spectrum); + EventRelay m_SetImpulse; bool SetImpulse(const Events::SetImpulse &event); diff --git a/src/game/Game/LevelSystem.cpp b/src/game/Game/LevelSystem.cpp index 3183d9e..f5b99a0 100755 --- a/src/game/Game/LevelSystem.cpp +++ b/src/game/Game/LevelSystem.cpp @@ -400,7 +400,7 @@ void dd::Systems::LevelSystem::BrickHit(EntityID entityHitter, EntityID entityBr p.parent = b; p.AlphaValues.push_back(1.f); p.AlphaValues.push_back(0.f); - p.ScaleValues.push_back(glm::vec3(0.06f)); + p.ScaleValues.push_back(glm::vec3(0.08f)); p.ScaleValues.push_back(glm::vec3(0.f)); p.RadiusDistribution = 1; p.EmitterLifeTime = 2.f; diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index 460d6bd..6c55530 100755 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -269,19 +269,14 @@ void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, Entity //Update alpha auto sprite = m_World->GetComponent(entity); float timeProgress = particle->TimeLived / (particle->LifeTime - 0.1); - if (timeProgress > particle->LifeTime) { - timeProgress = particle->LifeTime; + if (timeProgress > 1) { + timeProgress = 1; } auto emitter = m_World->GetComponent(particle->ParticleSystem); if (emitter) { sprite->Color.w = ScalarInterpolation(timeProgress, emitter->AlphaValues); - if (emitter->ScaleValues.size() > 1) { - //TODO: Implement vector interpolation function. Use scale from resourcemanager. Texture->Width... - std::vector spectrum; - spectrum.push_back(emitter->ScaleValues[0].x); - spectrum.push_back(emitter->ScaleValues[1].x); - particle->Scale = glm::vec3(ScalarInterpolation(timeProgress, spectrum)); - } + particle->Scale = VectorInterpolation(timeProgress, emitter->ScaleValues); + } } @@ -301,25 +296,41 @@ void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, Entity float dd::Systems::PhysicsSystem::ScalarInterpolation(float timeProgress, std::vector spectrum) { - int alphaVecSize = spectrum.size(); - if (alphaVecSize == 0) { - return 0.01f; + int spectrumSize = spectrum.size(); + if (spectrumSize == 0) { + return 1.f; } - if (alphaVecSize == 1) { + if (spectrumSize == 1) { return spectrum[0]; } float dValue = spectrum[1] - spectrum[0]; -// glm::abs(spectrum[0] - spectrum[1]); -// if (spectrum[0] > spectrum[1]) { -// dValue *= -1; -// } - float scalar = spectrum[0] + dValue * timeProgress; return scalar; } +glm::vec3 dd::Systems::PhysicsSystem::VectorInterpolation(float timeProgress, std::vector spectrum) +{ + int spectrumSize = spectrum.size(); + if (spectrumSize == 0) { + LOG_ERROR("You need to supply your particlesystem with scales in ScaleValues."); + return glm::vec3(1); + } + else if (spectrumSize == 1) { + return spectrum[0]; + } + + float dX = spectrum[1].x - spectrum[0].x; + float x = spectrum[0].x + dX * timeProgress; + float dY = spectrum[1].y - spectrum[0].y; + float y = spectrum[0].y + dY * timeProgress; + float dZ = spectrum[1].z - spectrum[0].z; + float z = spectrum[0].z + dZ * timeProgress; + + return glm::vec3(x, y, z); +} + bool dd::Systems::PhysicsSystem::OnPause(const dd::Events::Pause &event) { if (event.Type != "PhysicsSystem" && event.Type != "All") { @@ -837,17 +848,18 @@ bool dd::Systems::PhysicsSystem::OnContact(const dd::Events::Contact &event) e.EmittingAngle = glm::half_pi(); e.Spread = 0.5f; e.NumberOfTicks = 1; - e.ParticleLifeTime = 1.f; - e.ParticlesPerTick = 15; + e.ParticleLifeTime = 0.5f; + e.ParticlesPerTick = 1; e.Position = glm::vec3(event.IntersectionPoint.x, event.IntersectionPoint.y, -10); e.ScaleValues.clear(); - e.ScaleValues.push_back(glm::vec3(0.2f)); - e.ScaleValues.push_back(glm::vec3(1.f)); + e.ScaleValues.push_back(glm::vec3(0.5f)); + e.ScaleValues.push_back(glm::vec3(4.f, 4.f, 0.2f)); e.SpriteFile = "Textures/Particles/Cloud_Particle.png"; - e.Color = model->Color + glm::vec4(0.5f); + e.Color = model->Color; + e.AlphaValues.clear(); e.AlphaValues.push_back(1.f); e.AlphaValues.push_back(0.f); - e.Speed = 100; + //e.Speed = 0; } EventBroker->Publish(e);