Created
May 12, 2014 15:44
-
-
Save fenbf/919145473cc04da14af8 to your computer and use it in GitHub Desktop.
Basic Particle Update modules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "particleUpdaters.h" | |
#include <assert.h> | |
#include <algorithm> | |
#include <glm/common.hpp> | |
#include <glm/gtc/random.hpp> | |
namespace particles | |
{ | |
namespace updaters | |
{ | |
void EulerUpdater::update(double dt, ParticleData *p) | |
{ | |
const glm::vec4 globalA{ dt * m_globalAcceleration.x, dt * m_globalAcceleration.y, dt * m_globalAcceleration.z, 0.0 }; | |
const float localDT = (float)dt; | |
const unsigned int endId = p->m_countAlive; | |
for (size_t i = 0; i < endId; ++i) | |
p->m_acc[i] += globalA; | |
for (size_t i = 0; i < endId; ++i) | |
p->m_vel[i] += localDT * p->m_acc[i]; | |
for (size_t i = 0; i < endId; ++i) | |
p->m_pos[i] += localDT * p->m_vel[i]; | |
} | |
void AttractorUpdater::update(double dt, ParticleData *p) | |
{ | |
const float localDT = (float)dt; | |
const size_t endId = p->m_countAlive; | |
const size_t countAttractors = m_attractors.size(); | |
glm::vec4 off; | |
float dist; | |
size_t a; | |
for (size_t i = 0; i < endId; ++i) | |
{ | |
for (a = 0; a < countAttractors; ++a) | |
{ | |
off.x = m_attractors[a].x - p->m_pos[i].x; | |
off.y = m_attractors[a].y - p->m_pos[i].y; | |
off.z = m_attractors[a].z - p->m_pos[i].z; | |
dist = glm::dot(off, off); | |
//if (fabs(dist) > 0.00001) | |
dist = m_attractors[a].w / dist; | |
p->m_acc[i] += off * dist; | |
} | |
} | |
} | |
void BasicColorUpdater::update(double dt, ParticleData *p) | |
{ | |
const size_t endId = p->m_countAlive; | |
for (size_t i = 0; i < endId; ++i) | |
p->m_col[i] = glm::mix(p->m_startCol[i], p->m_endCol[i], p->m_time[i].z); | |
} | |
void BasicTimeUpdater::update(double dt, ParticleData *p) | |
{ | |
unsigned int endId = p->m_countAlive; | |
const float localDT = (float)dt; | |
if (endId == 0) return; | |
for (size_t i = 0; i < endId; ++i) | |
{ | |
p->m_time[i].x -= localDT; | |
// interpolation: from 0 (start of life) till 1 (end of life) | |
p->m_time[i].z = (float)1.0 - (p->m_time[i].x*p->m_time[i].w); // .w is 1.0/max life time | |
if (p->m_time[i].x < (float)0.0) | |
{ | |
p->kill(i); | |
endId = p->m_countAlive < p->m_count ? p->m_countAlive : p->m_count; | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <vector> | |
#include "commonMath.h" | |
#include "particles.h" | |
namespace particles | |
{ | |
namespace updaters | |
{ | |
class EulerUpdater : public particles::ParticleUpdater | |
{ | |
public: | |
glm::vec4 m_globalAcceleration{ 0.0f }; | |
public: | |
virtual void update(double dt, ParticleData *p) override; | |
}; | |
class AttractorUpdater : public particles::ParticleUpdater | |
{ | |
protected: | |
std::vector<glm::vec4> m_attractors; // .w is force | |
public: | |
virtual void update(double dt, ParticleData *p) override; | |
size_t collectionSize() const { return m_attractors.size(); } | |
void add(const glm::vec4 &attr) { m_attractors.push_back(attr); } | |
glm::vec4 &get(size_t id) { return m_attractors[id]; } | |
}; | |
class BasicColorUpdater : public ParticleUpdater | |
{ | |
public: | |
virtual void update(double dt, ParticleData *p) override; | |
}; | |
class BasicTimeUpdater : public ParticleUpdater | |
{ | |
public: | |
virtual void update(double dt, ParticleData *p) override; | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment