History

more developing on the API for the particle system

Jeffrey Ventrella committed 13 years ago

Commit 3649c891 parent 48bf26f4 files · +127 −55Browse files

Showing the changes to interface/src/ParticleSystem.cpp (1 of 4 files). Show all

Unified · Split

interface/src/ParticleSystem.cpp+92−45
@@ -13,22 +13,22 @@ · 12 unmodified lines
1313
1414 ParticleSystem::ParticleSystem() {
1515
16− _gravity = 0.005;
17− _numEmitters = 1;
18− _bounce = 0.9f;
19− _timer = 0.0f;
20− _airFriction = 6.0f;
21− _jitter = 0.1f;
22− _homeAttraction = 0.0f;
23− _tornadoForce = 0.0f;
24− _neighborAttraction = 0.02f;
25− _neighborRepulsion = 0.9f;
26− _TEST_bigSphereRadius = 0.5f;
27− _TEST_bigSpherePosition = glm::vec3( 5.0f, _TEST_bigSphereRadius, 5.0f);
28− _numParticles = 1500;
29−
30− for (unsigned int e = 0; e < _numEmitters; e++) {
16+ _gravity = 0.005;
17+ _numEmitters = 0;
18+ _bounce = 0.9f;
19+ _timer = 0.0f;
20+ _airFriction = 6.0f;
21+ _jitter = 0.1f;
22+ _homeAttraction = 0.0f;
23+ _tornadoForce = 0.0f;
24+ _neighborAttraction = 0.02f;
25+ _neighborRepulsion = 0.9f;
26+ _collisionSphereRadius = 0.0f;
27+ _collisionSpherePosition = glm::vec3(0.0f, 0.0f, 0.0f);
28+ _numParticles = 0;
29+ _usingCollisionSphere = false;
3130
31+ for (unsigned int e = 0; e < MAX_EMITTERS; e++) {
3232 _emitter[e].position = glm::vec3(0.0f, 0.0f, 0.0f);
3333 _emitter[e].rotation = glm::quat();
3434 _emitter[e].right = IDENTITY_RIGHT;
@@ -36,25 +36,25 @@ ParticleSystem::ParticleSystem() { · 1 unmodified line
3636 _emitter[e].front = IDENTITY_FRONT;
3737 };
3838
39− for (unsigned int p = 0; p < _numParticles; p++) {
40−
41− float radian = ((float)p / (float)_numParticles) * PI_TIMES_TWO;
42− float wave = sinf(radian);
43−
44− float red = 0.5f + 0.5f * wave;
45− float green = 0.3f + 0.3f * wave;
46− float blue = 0.2f - 0.2f * wave;
47−
48− _particle[p].color = glm::vec3(red, green, blue);
39+ for (unsigned int p = 0; p < MAX_PARTICLES; p++) {
40+ _particle[p].alive = false;
4941 _particle[p].age = 0.0f;
5042 _particle[p].radius = 0.01f;
5143 _particle[p].emitterIndex = 0;
5244 _particle[p].position = glm::vec3(0.0f, 0.0f, 0.0f);
5345 _particle[p].velocity = glm::vec3(0.0f, 0.0f, 0.0f);
46+ }
47+}
48+
49+
50+int ParticleSystem::addEmitter() {
51+ _numEmitters ++;
52+
53+ if (_numEmitters > MAX_EMITTERS) {
54+ return -1;
5455 }
5556
56− assert(_numParticles <= MAX_PARTICLES);
57− assert(_numEmitters <= MAX_EMITTERS );
57+ return _numEmitters - 1;
5858 }
5959
6060
@@ -67,9 +67,11 @@ void ParticleSystem::simulate(float deltaTime) { · 6 unmodified lines
6767
6868 // update particles
6969 for (unsigned int p = 0; p < _numParticles; p++) {
70− updateParticle(p, deltaTime);
70+ if (_particle[p].alive) {
71+ updateParticle(p, deltaTime);
72+ }
7173 }
72−
74+
7375 // apply special effects
7476 runSpecialEffectsTest(deltaTime);
7577 }
@@ -81,6 +83,40 @@ void ParticleSystem::updateEmitter(int e, float deltaTime) { · 5 unmodified lines
8183 _emitter[e].up = _emitter[e].rotation * IDENTITY_UP;
8284 }
8385
86+
87+
88+void ParticleSystem::emitParticlesNow(int e, int num) {
89+
90+ _numParticles = num;
91+
92+ if (_numParticles > MAX_PARTICLES) {
93+ _numParticles = MAX_PARTICLES;
94+ }
95+
96+ for (unsigned int p = 0; p < num; p++) {
97+ _particle[p].alive = true;
98+ _particle[p].position = _emitter[e].position;
99+ }
100+}
101+
102+void ParticleSystem::useOrangeBlueColorPalette() {
103+
104+ for (unsigned int p = 0; p < _numParticles; p++) {
105+
106+ float radian = ((float)p / (float)_numParticles) * PI_TIMES_TWO;
107+ float wave = sinf(radian);
108+ float red = 0.5f + 0.5f * wave;
109+ float green = 0.3f + 0.3f * wave;
110+ float blue = 0.2f - 0.2f * wave;
111+ _particle[p].color = glm::vec3(red, green, blue);
112+ }
113+}
114+
115+
116+
117+
118+
119+
84120 void ParticleSystem::runSpecialEffectsTest(float deltaTime) {
85121
86122 _timer += deltaTime;
@@ -93,14 +129,18 @@ void ParticleSystem::runSpecialEffectsTest(float deltaTime) { · 6 unmodified lines
93129 );
94130
95131 _emitter[0].rotation = glm::quat(glm::radians(tilt));
96−
97− _gravity = 0.01f + 0.01f * sinf( _timer * 0.52f );
98− _airFriction = 3.0f + 2.0f * sinf( _timer * 0.32f );
132+
133+ _gravity = 0.0f + 0.02f * sinf( _timer * 0.52f );
134+ _airFriction = 3.0f + 1.0f * sinf( _timer * 0.32f );
99135 _jitter = 0.05f + 0.05f * sinf( _timer * 0.42f );
100136 _homeAttraction = 0.01f + 0.01f * cosf( _timer * 0.6f );
101137 _tornadoForce = 0.0f + 0.03f * sinf( _timer * 0.7f );
102138 _neighborAttraction = 0.1f + 0.1f * cosf( _timer * 0.8f );
103139 _neighborRepulsion = 0.4f + 0.3f * sinf( _timer * 0.4f );
140+
141+ if (_gravity < 0.0f) {
142+ _gravity = 0.0f;
143+ }
104144 }
105145
106146
@@ -118,7 +158,6 @@ void ParticleSystem::updateParticle(int p, float deltaTime) { · 11 unmodified lines
118158 -_jitter * ONE_HALF + _jitter * randFloat()
119159 ) * deltaTime;
120160
121−
122161 // apply attraction to home position
123162 glm::vec3 vectorToHome = _emitter[_particle[p].emitterIndex].position - _particle[p].position;
124163 _particle[p].velocity += vectorToHome * _homeAttraction * deltaTime;
@@ -138,9 +177,8 @@ void ParticleSystem::updateParticle(int p, float deltaTime) { · 13 unmodified lines
138177 }
139178
140179 // apply tornado force
141− //glm::vec3 tornadoDirection = glm::cross(vectorToHome, _emitter[_particle[p].emitterIndex].up);
142− //_particle[p].velocity += tornadoDirection * _tornadoForce * deltaTime;
143− //_particle[p].velocity += _emitter[_particle[p].emitterIndex].up * _tornadoForce * deltaTime;
180+ glm::vec3 tornadoDirection = glm::cross(vectorToHome, _emitter[_particle[p].emitterIndex].up);
181+ _particle[p].velocity += tornadoDirection * _tornadoForce * deltaTime;
144182
145183 // apply air friction
146184 float drag = 1.0 - _airFriction * deltaTime;
@@ -166,18 +204,25 @@ void ParticleSystem::updateParticle(int p, float deltaTime) { · 19 unmodified lines
166204 }
167205
168206 // collision with sphere
169− glm::vec3 vectorToSphereCenter = _TEST_bigSpherePosition - _particle[p].position;
170− float distanceToSphereCenter = glm::length(vectorToSphereCenter);
171− float combinedRadius = _TEST_bigSphereRadius + _particle[p].radius;
172− if (distanceToSphereCenter < combinedRadius) {
173−
174− if (distanceToSphereCenter > 0.0f){
175− glm::vec3 directionToSphereCenter = vectorToSphereCenter / distanceToSphereCenter;
176− _particle[p].position = _TEST_bigSpherePosition - directionToSphereCenter * combinedRadius;
207+ if (_usingCollisionSphere) {
208+ glm::vec3 vectorToSphereCenter = _collisionSpherePosition - _particle[p].position;
209+ float distanceToSphereCenter = glm::length(vectorToSphereCenter);
210+ float combinedRadius = _collisionSphereRadius + _particle[p].radius;
211+ if (distanceToSphereCenter < combinedRadius) {
212+
213+ if (distanceToSphereCenter > 0.0f){
214+ glm::vec3 directionToSphereCenter = vectorToSphereCenter / distanceToSphereCenter;
215+ _particle[p].position = _collisionSpherePosition - directionToSphereCenter * combinedRadius;
216+ }
177217 }
178218 }
179219 }
180220
221+void ParticleSystem::setCollisionSphere(glm::vec3 position, float radius) {
222+ _usingCollisionSphere = true;
223+ _collisionSpherePosition = position;
224+ _collisionSphereRadius = radius;
225+}
181226
182227 void ParticleSystem::render() {
183228
@@ -188,7 +233,9 @@ void ParticleSystem::render() { · 4 unmodified lines
188233
189234 // render the particles
190235 for (unsigned int p = 0; p < _numParticles; p++) {
191− renderParticle(p);
236+ if (_particle[p].alive) {
237+ renderParticle(p);
238+ }
192239 }
193240 }
194241