-
Notifications
You must be signed in to change notification settings - Fork 1
/
particleSystem.cpp
68 lines (63 loc) · 1.75 KB
/
particleSystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//Own
#include "particleSystem.h"
//Particle System Contants
static const int lifetimeMin = 10;
static const int lifetimeMax = 20;
static const float distMin = 0;
static const float distMax = 30;
static const float angleMin = 0;
static const float angleMax = 360;
static const float dDistMin = 1;
static const float dDistMax = 15;
static const float dAngleMin = 0.5;
static const float dAngleMax = 3.0;
ParticleSystem::ParticleSystem()
{
reset();
}
Particle &ParticleSystem::nextParticle()
{
m_curParticle %= PARTICLE_COUNT;
return m_particles[m_curParticle++];
}
void ParticleSystem::reset()
{
for (int i = 0; i < PARTICLE_COUNT; ++i) {
m_particles[i] = newParticle();
}
m_curParticle = 0;
}
void ParticleSystem::update()
{
for (int i = 0; i < PARTICLE_COUNT; ++i) {
if (m_particles[i].lifetime > 0) {
m_particles[i].rho += m_particles[i].dRho;
m_particles[i].theta += m_particles[i].dTheta;
m_particles[i].phi += m_particles[i].dPhi;
m_particles[i].lifetime--;
} else {
m_particles[i] = newParticle();
}
}
}
float ParticleSystem::randomNumber(float min, float max) const
{
static bool firstTime = true;
if (firstTime) {
srand(time(0));
firstTime = false;
}
return min + rand() * (max - min) / RAND_MAX;
}
Particle ParticleSystem::newParticle() const
{
Particle p;
p.lifetime = (int)randomNumber(lifetimeMin, lifetimeMax);
p.rho = randomNumber(distMin, distMax);
p.theta = randomNumber(angleMin, angleMax);
p.phi = randomNumber(angleMin, angleMax);
p.dRho = randomNumber(dDistMin, dDistMax);
p.dTheta = randomNumber(dAngleMin, dAngleMax);
p.dPhi = randomNumber(dAngleMin, dAngleMax);
return p;
}