Showing the changes to interface/src/ParticleSystem.cpp (1 of 4 files). Show all
Unified · Split
| @@ -13,22 +13,22 @@ · 12 unmodified lines | |||
| 13 | 13 | ||
| 14 | ParticleSystem::ParticleSystem() { | 14 | ParticleSystem::ParticleSystem() { |
| 15 | 15 | ||
| 16 | _gravity = 0.005; | 16 | _gravity = 0.005; |
| 17 | _numEmitters = 1; | 17 | _numEmitters = 0; |
| 18 | _bounce = 0.9f; | 18 | _bounce = 0.9f; |
| 19 | _timer = 0.0f; | 19 | _timer = 0.0f; |
| 20 | _airFriction = 6.0f; | 20 | _airFriction = 6.0f; |
| 21 | _jitter = 0.1f; | 21 | _jitter = 0.1f; |
| 22 | _homeAttraction = 0.0f; | 22 | _homeAttraction = 0.0f; |
| 23 | _tornadoForce = 0.0f; | 23 | _tornadoForce = 0.0f; |
| 24 | _neighborAttraction = 0.02f; | 24 | _neighborAttraction = 0.02f; |
| 25 | _neighborRepulsion = 0.9f; | 25 | _neighborRepulsion = 0.9f; |
| 26 | _TEST_bigSphereRadius = 0.5f; | 26 | _collisionSphereRadius = 0.0f; |
| 27 | _TEST_bigSpherePosition = glm::vec3( 5.0f, _TEST_bigSphereRadius, 5.0f); | 27 | _collisionSpherePosition = glm::vec3(0.0f, 0.0f, 0.0f); |
| 28 | _numParticles = 1500; | 28 | _numParticles = 0; |
| 29 | 29 | _usingCollisionSphere = false; | |
| 30 | for (unsigned int e = 0; e < _numEmitters; e++) { | ||
| 31 | 30 | ||
| 31 | for (unsigned int e = 0; e < MAX_EMITTERS; e++) { | ||
| 32 | _emitter[e].position = glm::vec3(0.0f, 0.0f, 0.0f); | 32 | _emitter[e].position = glm::vec3(0.0f, 0.0f, 0.0f); |
| 33 | _emitter[e].rotation = glm::quat(); | 33 | _emitter[e].rotation = glm::quat(); |
| 34 | _emitter[e].right = IDENTITY_RIGHT; | 34 | _emitter[e].right = IDENTITY_RIGHT; |
| @@ -36,25 +36,25 @@ ParticleSystem::ParticleSystem() { · 1 unmodified line | |||
| 36 | _emitter[e].front = IDENTITY_FRONT; | 36 | _emitter[e].front = IDENTITY_FRONT; |
| 37 | }; | 37 | }; |
| 38 | 38 | ||
| 39 | for (unsigned int p = 0; p < _numParticles; p++) { | 39 | for (unsigned int p = 0; p < MAX_PARTICLES; p++) { |
| 40 | 40 | _particle[p].alive = false; | |
| 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); | ||
| 49 | _particle[p].age = 0.0f; | 41 | _particle[p].age = 0.0f; |
| 50 | _particle[p].radius = 0.01f; | 42 | _particle[p].radius = 0.01f; |
| 51 | _particle[p].emitterIndex = 0; | 43 | _particle[p].emitterIndex = 0; |
| 52 | _particle[p].position = glm::vec3(0.0f, 0.0f, 0.0f); | 44 | _particle[p].position = glm::vec3(0.0f, 0.0f, 0.0f); |
| 53 | _particle[p].velocity = glm::vec3(0.0f, 0.0f, 0.0f); | 45 | _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; | ||
| 54 | } | 55 | } |
| 55 | 56 | ||
| 56 | assert(_numParticles <= MAX_PARTICLES); | 57 | return _numEmitters - 1; |
| 57 | assert(_numEmitters <= MAX_EMITTERS ); | ||
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | 60 | ||
| @@ -67,9 +67,11 @@ void ParticleSystem::simulate(float deltaTime) { · 6 unmodified lines | |||
| 67 | 67 | ||
| 68 | // update particles | 68 | // update particles |
| 69 | for (unsigned int p = 0; p < _numParticles; p++) { | 69 | for (unsigned int p = 0; p < _numParticles; p++) { |
| 70 | updateParticle(p, deltaTime); | 70 | if (_particle[p].alive) { |
| 71 | updateParticle(p, deltaTime); | ||
| 72 | } | ||
| 71 | } | 73 | } |
| 72 | 74 | ||
| 73 | // apply special effects | 75 | // apply special effects |
| 74 | runSpecialEffectsTest(deltaTime); | 76 | runSpecialEffectsTest(deltaTime); |
| 75 | } | 77 | } |
| @@ -81,6 +83,40 @@ void ParticleSystem::updateEmitter(int e, float deltaTime) { · 5 unmodified lines | |||
| 81 | _emitter[e].up = _emitter[e].rotation * IDENTITY_UP; | 83 | _emitter[e].up = _emitter[e].rotation * IDENTITY_UP; |
| 82 | } | 84 | } |
| 83 | 85 | ||
| 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 | |||
| 84 | void ParticleSystem::runSpecialEffectsTest(float deltaTime) { | 120 | void ParticleSystem::runSpecialEffectsTest(float deltaTime) { |
| 85 | 121 | ||
| 86 | _timer += deltaTime; | 122 | _timer += deltaTime; |
| @@ -93,14 +129,18 @@ void ParticleSystem::runSpecialEffectsTest(float deltaTime) { · 6 unmodified lines | |||
| 93 | ); | 129 | ); |
| 94 | 130 | ||
| 95 | _emitter[0].rotation = glm::quat(glm::radians(tilt)); | 131 | _emitter[0].rotation = glm::quat(glm::radians(tilt)); |
| 96 | 132 | ||
| 97 | _gravity = 0.01f + 0.01f * sinf( _timer * 0.52f ); | 133 | _gravity = 0.0f + 0.02f * sinf( _timer * 0.52f ); |
| 98 | _airFriction = 3.0f + 2.0f * sinf( _timer * 0.32f ); | 134 | _airFriction = 3.0f + 1.0f * sinf( _timer * 0.32f ); |
| 99 | _jitter = 0.05f + 0.05f * sinf( _timer * 0.42f ); | 135 | _jitter = 0.05f + 0.05f * sinf( _timer * 0.42f ); |
| 100 | _homeAttraction = 0.01f + 0.01f * cosf( _timer * 0.6f ); | 136 | _homeAttraction = 0.01f + 0.01f * cosf( _timer * 0.6f ); |
| 101 | _tornadoForce = 0.0f + 0.03f * sinf( _timer * 0.7f ); | 137 | _tornadoForce = 0.0f + 0.03f * sinf( _timer * 0.7f ); |
| 102 | _neighborAttraction = 0.1f + 0.1f * cosf( _timer * 0.8f ); | 138 | _neighborAttraction = 0.1f + 0.1f * cosf( _timer * 0.8f ); |
| 103 | _neighborRepulsion = 0.4f + 0.3f * sinf( _timer * 0.4f ); | 139 | _neighborRepulsion = 0.4f + 0.3f * sinf( _timer * 0.4f ); |
| 140 | |||
| 141 | if (_gravity < 0.0f) { | ||
| 142 | _gravity = 0.0f; | ||
| 143 | } | ||
| 104 | } | 144 | } |
| 105 | 145 | ||
| 106 | 146 | ||
| @@ -118,7 +158,6 @@ void ParticleSystem::updateParticle(int p, float deltaTime) { · 11 unmodified lines | |||
| 118 | -_jitter * ONE_HALF + _jitter * randFloat() | 158 | -_jitter * ONE_HALF + _jitter * randFloat() |
| 119 | ) * deltaTime; | 159 | ) * deltaTime; |
| 120 | 160 | ||
| 121 | |||
| 122 | // apply attraction to home position | 161 | // apply attraction to home position |
| 123 | glm::vec3 vectorToHome = _emitter[_particle[p].emitterIndex].position - _particle[p].position; | 162 | glm::vec3 vectorToHome = _emitter[_particle[p].emitterIndex].position - _particle[p].position; |
| 124 | _particle[p].velocity += vectorToHome * _homeAttraction * deltaTime; | 163 | _particle[p].velocity += vectorToHome * _homeAttraction * deltaTime; |
| @@ -138,9 +177,8 @@ void ParticleSystem::updateParticle(int p, float deltaTime) { · 13 unmodified lines | |||
| 138 | } | 177 | } |
| 139 | 178 | ||
| 140 | // apply tornado force | 179 | // apply tornado force |
| 141 | //glm::vec3 tornadoDirection = glm::cross(vectorToHome, _emitter[_particle[p].emitterIndex].up); | 180 | glm::vec3 tornadoDirection = glm::cross(vectorToHome, _emitter[_particle[p].emitterIndex].up); |
| 142 | //_particle[p].velocity += tornadoDirection * _tornadoForce * deltaTime; | 181 | _particle[p].velocity += tornadoDirection * _tornadoForce * deltaTime; |
| 143 | //_particle[p].velocity += _emitter[_particle[p].emitterIndex].up * _tornadoForce * deltaTime; | ||
| 144 | 182 | ||
| 145 | // apply air friction | 183 | // apply air friction |
| 146 | float drag = 1.0 - _airFriction * deltaTime; | 184 | float drag = 1.0 - _airFriction * deltaTime; |
| @@ -166,18 +204,25 @@ void ParticleSystem::updateParticle(int p, float deltaTime) { · 19 unmodified lines | |||
| 166 | } | 204 | } |
| 167 | 205 | ||
| 168 | // collision with sphere | 206 | // collision with sphere |
| 169 | glm::vec3 vectorToSphereCenter = _TEST_bigSpherePosition - _particle[p].position; | 207 | if (_usingCollisionSphere) { |
| 170 | float distanceToSphereCenter = glm::length(vectorToSphereCenter); | 208 | glm::vec3 vectorToSphereCenter = _collisionSpherePosition - _particle[p].position; |
| 171 | float combinedRadius = _TEST_bigSphereRadius + _particle[p].radius; | 209 | float distanceToSphereCenter = glm::length(vectorToSphereCenter); |
| 172 | if (distanceToSphereCenter < combinedRadius) { | 210 | float combinedRadius = _collisionSphereRadius + _particle[p].radius; |
| 173 | 211 | if (distanceToSphereCenter < combinedRadius) { | |
| 174 | if (distanceToSphereCenter > 0.0f){ | 212 | |
| 175 | glm::vec3 directionToSphereCenter = vectorToSphereCenter / distanceToSphereCenter; | 213 | if (distanceToSphereCenter > 0.0f){ |
| 176 | _particle[p].position = _TEST_bigSpherePosition - directionToSphereCenter * combinedRadius; | 214 | glm::vec3 directionToSphereCenter = vectorToSphereCenter / distanceToSphereCenter; |
| 215 | _particle[p].position = _collisionSpherePosition - directionToSphereCenter * combinedRadius; | ||
| 216 | } | ||
| 177 | } | 217 | } |
| 178 | } | 218 | } |
| 179 | } | 219 | } |
| 180 | 220 | ||
| 221 | void ParticleSystem::setCollisionSphere(glm::vec3 position, float radius) { | ||
| 222 | _usingCollisionSphere = true; | ||
| 223 | _collisionSpherePosition = position; | ||
| 224 | _collisionSphereRadius = radius; | ||
| 225 | } | ||
| 181 | 226 | ||
| 182 | void ParticleSystem::render() { | 227 | void ParticleSystem::render() { |
| 183 | 228 | ||
| @@ -188,7 +233,9 @@ void ParticleSystem::render() { · 4 unmodified lines | |||
| 188 | 233 | ||
| 189 | // render the particles | 234 | // render the particles |
| 190 | for (unsigned int p = 0; p < _numParticles; p++) { | 235 | for (unsigned int p = 0; p < _numParticles; p++) { |
| 191 | renderParticle(p); | 236 | if (_particle[p].alive) { |
| 237 | renderParticle(p); | ||
| 238 | } | ||
| 192 | } | 239 | } |
| 193 | } | 240 | } |
| 194 | 241 | ||