-
Notifications
You must be signed in to change notification settings - Fork 0
/
Particles.h
144 lines (120 loc) · 3.58 KB
/
Particles.h
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef Headers_H
#define Headers_H
#include <SFML\Graphics.hpp>
#include <functional>
#include <iostream>
//initial parameters for velocity fields
float flowStrength = 200;
float radius = 100;
float cylinderrotation = -100000;
float rankinevortexrotation = 10;
class ParticleEmitter : public sf::Drawable, public sf::Transformable
{
struct particle
{
sf::Vector2f velocity;
sf::Time lifetme;
};
sf::VertexArray vert_particles;
std::vector<particle> particleVect;
sf::Vector2f emitterPos;
std::function<sf::Vector2f(sf::Vector2f)> particle_velocity;
public:
ParticleEmitter(unsigned int count) :
vert_particles(sf::Points, count),
particleVect(count),
emitterPos(0, 0)
{}
//initialise starting lifetime of particles to random values, to slightly avoid annoying clumping at start
void initialise(void)
{
for (std::size_t i = 0; i < particleVect.size(); ++i)
{
particleVect[i].lifetme = sf::milliseconds((std::rand() % 5000));
}
}
//set emitter position
void setemitterPos(sf::Vector2f pos)
{
emitterPos = pos;
}
//get emitter position
sf::Vector2f getemitterPos(void)
{
return sf::Vector2f(emitterPos.x, emitterPos.y);
}
//takes a given velocity function as an argument, e.g. setVelocityField(shearFlow);
void setVelocityField(std::function<sf::Vector2f(sf::Vector2f)> func)
{
particle_velocity = func;
}
//update fixed emitters and mouse
void update(const sf::Time& elapsed)
{
for (std::size_t i = 0; i < particleVect.size(); ++i)
{
sf::Vertex& v = vert_particles[i];
particle& p = particleVect[i];
p.lifetme -= elapsed;
if (p.lifetme < sf::Time::Zero)
resetparticles(i);
p.velocity = particle_velocity(v.position);
v.position += p.velocity*elapsed.asSeconds();
}
}
//update random emitters on side walls
void updatehorizontal(sf::Time elapsed)
{
for (std::size_t i = 0; i < particleVect.size(); ++i)
{
sf::Vertex& v = vert_particles[i];
particle& p = particleVect[i];
p.lifetme -= elapsed;
setemitterPos(sf::Vector2f(std::rand() % 800, emitterPos.y));
if (p.lifetme < sf::Time::Zero)
resetparticles(i);
p.velocity = particle_velocity(v.position);
v.position += p.velocity*elapsed.asSeconds();
}
}
//update random emitters on top and bottom walls
void updatevertical(sf::Time elapsed)
{
for (std::size_t i = 0; i < particleVect.size(); ++i)
{
sf::Vertex& v = vert_particles[i];
particle& p = particleVect[i];
p.lifetme -= elapsed;
setemitterPos(sf::Vector2f(emitterPos.x, std::rand() % 600));
if (p.lifetme < sf::Time::Zero)
resetparticles(i);
p.velocity = particle_velocity(v.position);
v.position += p.velocity*elapsed.asSeconds();
}
}
// set particles colour
void setColour(sf::Color colour)
{
for (std::size_t i = 0; i < particleVect.size(); ++i)
{
vert_particles[i].color = colour;
}
}
private:
void resetparticles(std::size_t index)
{
vert_particles[index].position = emitterPos;
particleVect[index].lifetme = sf::milliseconds((std::rand() % 3000) + 3000);
}
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// apply the entity's transform -- combine it with the one that was passed by the caller
states.transform *= getTransform(); // getTransform() is defined by sf::Transformable
// apply the texture
states.texture = NULL;
// you may also override states.shader or states.blendMode if you want
// draw the vertex array
target.draw(vert_particles, states);
}
};
#endif