-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParticleEmitter.cpp
138 lines (114 loc) · 2.6 KB
/
ParticleEmitter.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
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
// Kevin M. Smith - CS 134 SJSU
#include "ParticleEmitter.h"
ParticleEmitter::ParticleEmitter() {
sys = new ParticleSystem();
createdSys = true;
init();
}
ParticleEmitter::ParticleEmitter(ParticleSystem *s) {
if (s == NULL)
{
cout << "fatal error: null particle system passed to ParticleEmitter()" << endl;
ofExit();
}
sys = s;
createdSys = false;
init();
}
ParticleEmitter::~ParticleEmitter() {
// deallocate particle system if emitter created one internally
//
if (createdSys) delete sys;
}
void ParticleEmitter::init() {
rate = 1;
velocity = ofVec3f(0, 20, 0);
lifespan = 3;
started = false;
oneShot = false;
fired = false;
lastSpawned = 0;
radius = 1;
particleRadius = .1;
visible = true;
type = DirectionalEmitter;
groupSize = 1;
}
void ParticleEmitter::draw() {
if (visible) {
switch (type) {
case DirectionalEmitter:
ofDrawSphere(position, radius/10); // just draw a small sphere for point emitters
break;
case SphereEmitter:
case RadialEmitter:
ofDrawSphere(position, radius/10);// just draw a small sphere as a placeholder
break;
default:
break;
}
}
sys->draw();
}
void ParticleEmitter::start() {
started = true;
lastSpawned = ofGetElapsedTimeMillis();
}
void ParticleEmitter::stop() {
started = false;
fired = false;
}
void ParticleEmitter::update() {
float time = ofGetElapsedTimeMillis();
if (oneShot && started) {
if (!fired) {
// spawn a new particle(s)
//
for (int i = 0; i < groupSize; i++)
spawn(time);
lastSpawned = time;
}
fired = true;
stop();
}
else if (((time - lastSpawned) > (1000.0 / rate)) && started) {
// spawn a new particle(s)
//
for (int i= 0; i < groupSize; i++)
spawn(time);
lastSpawned = time;
}
sys->update();
}
// spawn a single particle. time is current time of birth
//
void ParticleEmitter::spawn(float time) {
Particle particle;
// set initial velocity and position
// based on emitter type
//
switch (type) {
case RadialEmitter:
{
ofVec3f dir = ofVec3f(ofRandom(-1, 1), ofRandom(-1, 1), ofRandom(-1, 1));
float speed = velocity.length();
particle.velocity = dir.getNormalized() * speed;
particle.position.set(position);
}
break;
case SphereEmitter:
break;
case DirectionalEmitter:
particle.velocity = velocity;
particle.position.set(position);
break;
}
// other particle attributes
//
particle.lifespan = lifespan;
particle.birthtime = time;
particle.radius = particleRadius;
// add to system
//
sys->add(particle);
}