BUG FIX: Iterator not increased incorrectly -> No longer a particle stuck in emitter.
This commit is contained in:
+5
-5
@@ -49,7 +49,7 @@ void GameWorld::Initialize()
|
|||||||
model->ModelFile = "Models/Placeholders/tank/Chassi.obj";
|
model->ModelFile = "Models/Placeholders/tank/Chassi.obj";
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0; i < 2; i++)
|
/*for(int i = 0; i < 2; i++)
|
||||||
{
|
{
|
||||||
auto light = CreateEntity();
|
auto light = CreateEntity();
|
||||||
auto transform = AddComponent<Components::Transform>(light, "Transform");
|
auto transform = AddComponent<Components::Transform>(light, "Transform");
|
||||||
@@ -65,7 +65,7 @@ void GameWorld::Initialize()
|
|||||||
|
|
||||||
auto model = AddComponent<Components::Model>(light, "Model");
|
auto model = AddComponent<Components::Model>(light, "Model");
|
||||||
model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj";
|
model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj";
|
||||||
}
|
}*/
|
||||||
|
|
||||||
for(int i = 0; i < 3; i++)
|
for(int i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
@@ -92,7 +92,7 @@ void GameWorld::Initialize()
|
|||||||
}*/
|
}*/
|
||||||
|
|
||||||
{
|
{
|
||||||
for(int i = 0; i < 4 ; i++)
|
for(int i = 0; i < 1 ; i++)
|
||||||
{
|
{
|
||||||
// Particle emitter
|
// Particle emitter
|
||||||
auto ent = CreateEntity();
|
auto ent = CreateEntity();
|
||||||
@@ -100,9 +100,9 @@ void GameWorld::Initialize()
|
|||||||
transform->Position = glm::vec3(i * 10, 20, 0);
|
transform->Position = glm::vec3(i * 10, 20, 0);
|
||||||
auto emitter = AddComponent<Components::ParticleEmitter>(ent, "ParticleEmitter");
|
auto emitter = AddComponent<Components::ParticleEmitter>(ent, "ParticleEmitter");
|
||||||
emitter->LifeTime = 2;
|
emitter->LifeTime = 2;
|
||||||
emitter->SpawnCount = 1;
|
emitter->SpawnCount = 3;
|
||||||
emitter->SpreadAngle = 35;
|
emitter->SpreadAngle = 35;
|
||||||
emitter->SpawnFrequency = 2;
|
emitter->SpawnFrequency = 0.2;
|
||||||
auto model = AddComponent<Components::Model>(ent, "Model");
|
auto model = AddComponent<Components::Model>(ent, "Model");
|
||||||
model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj";
|
model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj";
|
||||||
//emitter->
|
//emitter->
|
||||||
|
|||||||
@@ -28,14 +28,21 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
|||||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
|
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
|
||||||
if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency)
|
if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency)
|
||||||
{
|
{
|
||||||
SpawnParticles(entity, transformComponent->Position, emitterComponent->SpawnCount, emitterComponent->SpreadAngle);
|
SpawnParticles(entity, transformComponent->Position, emitterComponent->SpawnCount, emitterComponent->SpreadAngle, emitterComponent->LifeTime);
|
||||||
emitterComponent->TimeSinceLastSpawn = 0;
|
emitterComponent->TimeSinceLastSpawn = 0;
|
||||||
}
|
}
|
||||||
|
std::cout<<"Number of particles in list for emitter "<<entity<<": "<<m_ParticleEmitter[entity].size()<<std::endl;
|
||||||
std::list<ParticleData>::iterator it;
|
std::list<ParticleData>::iterator it;
|
||||||
for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();)
|
for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();)
|
||||||
{
|
{
|
||||||
EntityID particleID = it->ParticleID;
|
EntityID particleID = (it)->ParticleID;
|
||||||
|
auto transformComponent = m_World->GetComponent<Components::Transform>(particleID, "Transform");
|
||||||
|
float speed = 20 * dt;
|
||||||
|
//ERROR: Direction seems to be 0 for first particle in the list...
|
||||||
|
transformComponent->Position.x += it->Direction.x * speed;
|
||||||
|
transformComponent->Position.y += it->Direction.y * speed;
|
||||||
|
transformComponent->Position.z += it->Direction.z * speed;
|
||||||
|
|
||||||
auto particleComponent = m_World->GetComponent<Components::Particle>(particleID, "Particle");
|
auto particleComponent = m_World->GetComponent<Components::Particle>(particleID, "Particle");
|
||||||
double timeLived = glfwGetTime() - it->SpawnTime;
|
double timeLived = glfwGetTime() - it->SpawnTime;
|
||||||
if(timeLived > particleComponent->LifeTime)
|
if(timeLived > particleComponent->LifeTime)
|
||||||
@@ -43,8 +50,6 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
|||||||
|
|
||||||
m_World->RemoveEntity(particleID);
|
m_World->RemoveEntity(particleID);
|
||||||
m_ParticleEmitter[entity].erase(it);
|
m_ParticleEmitter[entity].erase(it);
|
||||||
//std::cout<<"Removed dead particle..."<<std::endl; // TEMP
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -52,11 +57,7 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
|||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto transformComponent = m_World->GetComponent<Components::Transform>(particleID, "Transform");
|
|
||||||
float speed = 20 * dt;
|
|
||||||
transformComponent->Position.x += it->Direction.x * speed;
|
|
||||||
transformComponent->Position.y += it->Direction.y * speed;
|
|
||||||
transformComponent->Position.z += it->Direction.z * speed;
|
|
||||||
|
|
||||||
|
|
||||||
// Interpolates the color for each color channel by the start and end value. Decides how much the color should be interpolated based on time.
|
// Interpolates the color for each color channel by the start and end value. Decides how much the color should be interpolated based on time.
|
||||||
@@ -91,7 +92,7 @@ void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf)
|
|||||||
cf->Register("Particle", []() { return new Components::Particle(); });
|
cf->Register("Particle", []() { return new Components::Particle(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle)
|
void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle, double lifeTime)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < spawnCount; i++)
|
for(int i = 0; i < spawnCount; i++)
|
||||||
{
|
{
|
||||||
@@ -104,7 +105,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos,
|
|||||||
transform->Scale = glm::vec3(1, 1, 0);
|
transform->Scale = glm::vec3(1, 1, 0);
|
||||||
|
|
||||||
auto particle = m_World->AddComponent<Components::Particle>(ent, "Particle");
|
auto particle = m_World->AddComponent<Components::Particle>(ent, "Particle");
|
||||||
particle->LifeTime = 4;
|
particle->LifeTime = lifeTime;
|
||||||
/*Color startColor = {.4f, .45f, .2f};
|
/*Color startColor = {.4f, .45f, .2f};
|
||||||
particle->ColorSpectrum.push_back(startColor);
|
particle->ColorSpectrum.push_back(startColor);
|
||||||
Color endColor = {0.f, 45.f, 23.f};
|
Color endColor = {0.f, 45.f, 23.f};
|
||||||
@@ -117,6 +118,15 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos,
|
|||||||
auto model = m_World->AddComponent<Components::Model>(ent, "Model");
|
auto model = m_World->AddComponent<Components::Model>(ent, "Model");
|
||||||
model->ModelFile = "Models/Placeholders/PhysicsTest/Cube.obj";
|
model->ModelFile = "Models/Placeholders/PhysicsTest/Cube.obj";
|
||||||
|
|
||||||
|
auto light = m_World->AddComponent<Components::PointLight>(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<Components::Physics>(ent, "Physics");
|
/*auto physics = m_World->AddComponent<Components::Physics>(ent, "Physics");
|
||||||
physics->Mass = 1;
|
physics->Mass = 1;
|
||||||
|
|
||||||
@@ -130,17 +140,10 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos,
|
|||||||
data.SpawnTime = glfwGetTime();
|
data.SpawnTime = glfwGetTime();
|
||||||
// data.color = particle->ColorSpectrum[0];
|
// data.color = particle->ColorSpectrum[0];
|
||||||
// data.Scale = particle->ScaleSpectrum[0];
|
// data.Scale = particle->ScaleSpectrum[0];
|
||||||
|
//Random between [-1,1] on every axis
|
||||||
data.Direction = glm::vec3(((double)rand() / ((double)RAND_MAX + 1) * 2) -1, ((double)rand() / ((double)RAND_MAX + 1) * 2) -1, ((double)rand() / ((double)RAND_MAX + 1) * 2) -1);
|
data.Direction = glm::vec3(((double)rand() / ((double)RAND_MAX + 1) * 2) -1, ((double)rand() / ((double)RAND_MAX + 1) * 2) -1, ((double)rand() / ((double)RAND_MAX + 1) * 2) -1);
|
||||||
data.Direction = glm::normalize(data.Direction);
|
data.Direction = glm::normalize(data.Direction);
|
||||||
|
|
||||||
m_ParticleEmitter[emitterID].push_back(data);
|
m_ParticleEmitter[emitterID].push_back(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// void Systems::ParticleSystem::Draw(double dt)
|
|
||||||
// {
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#include "Components/Model.h"
|
#include "Components/Model.h"
|
||||||
#include "Components/Physics.h"
|
#include "Components/Physics.h"
|
||||||
#include "Components/Box.h"
|
#include "Components/Box.h"
|
||||||
|
#include "Components/PointLight.h"
|
||||||
#include "Color.h"
|
#include "Color.h"
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
@@ -32,9 +33,8 @@ public:
|
|||||||
|
|
||||||
void Update(double dt) override;
|
void Update(double dt) override;
|
||||||
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
|
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
|
||||||
void Draw(double dt);
|
|
||||||
private:
|
private:
|
||||||
void SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle);
|
void SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle, double lifeTime);
|
||||||
std::map<EntityID, std::list<ParticleData>> m_ParticleEmitter;
|
std::map<EntityID, std::list<ParticleData>> m_ParticleEmitter;
|
||||||
std::map<EntityID, double> m_TimeSinceLastSpawn;
|
std::map<EntityID, double> m_TimeSinceLastSpawn;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user