Showing the changes to interface/src/ParticleSystem.cpp (1 of 6 files). Show all
Unified · Split
| @@ -22,24 +22,23 @@ ParticleSystem::ParticleSystem() { · 21 unmodified lines | ||
| 22 | 22 | _upDirection = glm::vec3(0.0f, 1.0f, 0.0f); // default |
| 23 | 23 | |
| 24 | 24 | for (unsigned int e = 0; e < MAX_EMITTERS; e++) { |
| 25 | − _emitter[e].position = glm::vec3(0.0f, 0.0f, 0.0f); | |
| 26 | − _emitter[e].rotation = glm::quat(); | |
| 27 | − _emitter[e].right = IDENTITY_RIGHT; | |
| 28 | − _emitter[e].up = IDENTITY_UP; | |
| 29 | − _emitter[e].front = IDENTITY_FRONT; | |
| 30 | − _emitter[e].bounce = DEFAULT_PARTICLE_BOUNCE; | |
| 31 | − _emitter[e].airFriction = DEFAULT_PARTICLE_AIR_FRICTION; | |
| 32 | − _emitter[e].gravity = 0.0f; | |
| 33 | − _emitter[e].jitter = 0.0f; | |
| 34 | − _emitter[e].emitterAttraction = 0.0f; | |
| 35 | − _emitter[e].tornadoForce = 0.0f; | |
| 36 | − _emitter[e].neighborAttraction = 0.0f; | |
| 37 | − _emitter[e].neighborRepulsion = 0.0f; | |
| 38 | − _emitter[e].collisionSphereRadius = 0.0f; | |
| 39 | − _emitter[e].collisionSpherePosition = glm::vec3(0.0f, 0.0f, 0.0f); | |
| 40 | − _emitter[e].usingCollisionSphere = false; | |
| 41 | − _emitter[e].showingEmitter = false; | |
| 42 | − | |
| 25 | + _emitter[e].position = glm::vec3(0.0f, 0.0f, 0.0f); | |
| 26 | + _emitter[e].rotation = glm::quat(); | |
| 27 | + _emitter[e].right = IDENTITY_RIGHT; | |
| 28 | + _emitter[e].up = IDENTITY_UP; | |
| 29 | + _emitter[e].front = IDENTITY_FRONT; | |
| 30 | + _emitter[e].showingEmitter = false; | |
| 31 | + _emitter[e].particleAttributes.bounce = DEFAULT_PARTICLE_BOUNCE; | |
| 32 | + _emitter[e].particleAttributes.airFriction = DEFAULT_PARTICLE_AIR_FRICTION; | |
| 33 | + _emitter[e].particleAttributes.gravity = 0.0f; | |
| 34 | + _emitter[e].particleAttributes.jitter = 0.0f; | |
| 35 | + _emitter[e].particleAttributes.emitterAttraction = 0.0f; | |
| 36 | + _emitter[e].particleAttributes.tornadoForce = 0.0f; | |
| 37 | + _emitter[e].particleAttributes.neighborAttraction = 0.0f; | |
| 38 | + _emitter[e].particleAttributes.neighborRepulsion = 0.0f; | |
| 39 | + _emitter[e].particleAttributes.collisionSphereRadius = 0.0f; | |
| 40 | + _emitter[e].particleAttributes.collisionSpherePosition = glm::vec3(0.0f, 0.0f, 0.0f); | |
| 41 | + _emitter[e].particleAttributes.usingCollisionSphere = false; | |
| 43 | 42 | }; |
| 44 | 43 | |
| 45 | 44 | for (unsigned int p = 0; p < MAX_PARTICLES; p++) { |
| @@ -56,19 +55,11 @@ ParticleSystem::ParticleSystem() { · 10 unmodified lines | ||
| 56 | 55 | |
| 57 | 56 | int ParticleSystem::addEmitter() { |
| 58 | 57 | |
| 59 | − printf( "\n" ); | |
| 60 | − printf( "ParticleSystem::addEmitter\n" ); | |
| 61 | − | |
| 62 | 58 | _numEmitters ++; |
| 63 | 59 | |
| 64 | 60 | if (_numEmitters > MAX_EMITTERS) { |
| 65 | − | |
| 66 | − printf( "Oops! _numEmitters > MAX_EMITTERS... returning -1\n" ); | |
| 67 | − | |
| 68 | 61 | return -1; |
| 69 | 62 | } |
| 70 | − | |
| 71 | − printf( "ok, _numEmitters is %d,and the index of this newly added emitter is %d.\n", _numEmitters, _numEmitters-1 ); | |
| 72 | 63 | |
| 73 | 64 | return _numEmitters - 1; |
| 74 | 65 | } |
| @@ -81,8 +72,6 @@ void ParticleSystem::simulate(float deltaTime) { · 6 unmodified lines | ||
| 81 | 72 | updateEmitter(e, deltaTime); |
| 82 | 73 | } |
| 83 | 74 | |
| 84 | − runSpecialEffectsTest(0, deltaTime); | |
| 85 | − | |
| 86 | 75 | // update particles |
| 87 | 76 | for (unsigned int p = 0; p < _numParticles; p++) { |
| 88 | 77 | if (_particle[p].alive) { |
| @@ -157,24 +146,23 @@ void ParticleSystem::setOrangeBlueColorPalette() { · 68 unmodified lines | ||
| 157 | 146 | } |
| 158 | 147 | |
| 159 | 148 | |
| 160 | −void ParticleSystem::runSpecialEffectsTest(int e, float deltaTime) { | |
| 161 | − | |
| 162 | − _timer += deltaTime; | |
| 163 | − | |
| 164 | − _emitter[e].gravity = 0.0f + 0.05f * sinf( _timer * 0.52f ); | |
| 165 | − _emitter[e].airFriction = (DEFAULT_PARTICLE_AIR_FRICTION + 0.5f) + 2.0f * sinf( _timer * 0.32f ); | |
| 166 | − _emitter[e].jitter = 0.05f + 0.05f * sinf( _timer * 0.42f ); | |
| 167 | − _emitter[e].emitterAttraction = 0.015f + 0.015f * cosf( _timer * 0.6f ); | |
| 168 | − _emitter[e].tornadoForce = 0.0f + 0.03f * sinf( _timer * 0.7f ); | |
| 169 | − _emitter[e].neighborAttraction = 0.1f + 0.1f * cosf( _timer * 0.8f ); | |
| 170 | − _emitter[e].neighborRepulsion = 0.2f + 0.2f * sinf( _timer * 0.4f ); | |
| 149 | +void ParticleSystem::setParticleAttributesForEmitter(int emitterIndex, ParticleAttributes attributes) { | |
| 171 | 150 | |
| 172 | − if (_emitter[e].gravity < 0.0f) { | |
| 173 | − _emitter[e].gravity = 0.0f; | |
| 174 | − } | |
| 151 | + _emitter[emitterIndex].particleAttributes.bounce = attributes.bounce; | |
| 152 | + _emitter[emitterIndex].particleAttributes.gravity = attributes.gravity; | |
| 153 | + _emitter[emitterIndex].particleAttributes.airFriction = attributes.airFriction; | |
| 154 | + _emitter[emitterIndex].particleAttributes.jitter = attributes.jitter; | |
| 155 | + _emitter[emitterIndex].particleAttributes.emitterAttraction = attributes.emitterAttraction; | |
| 156 | + _emitter[emitterIndex].particleAttributes.tornadoForce = attributes.tornadoForce; | |
| 157 | + _emitter[emitterIndex].particleAttributes.neighborAttraction = attributes.neighborAttraction; | |
| 158 | + _emitter[emitterIndex].particleAttributes.neighborRepulsion = attributes.neighborRepulsion; | |
| 159 | + _emitter[emitterIndex].particleAttributes.usingCollisionSphere = attributes.usingCollisionSphere; | |
| 160 | + _emitter[emitterIndex].particleAttributes.collisionSpherePosition = attributes.collisionSpherePosition; | |
| 161 | + _emitter[emitterIndex].particleAttributes.collisionSphereRadius = attributes.collisionSphereRadius; | |
| 175 | 162 | } |
| 176 | 163 | |
| 177 | 164 | |
| 165 | + | |
| 178 | 166 | void ParticleSystem::updateParticle(int p, float deltaTime) { |
| 179 | 167 | |
| 180 | 168 | _particle[p].age += deltaTime; |
| @@ -189,14 +177,14 @@ void ParticleSystem::updateParticle(int p, float deltaTime) { · 8 unmodified lines | ||
| 189 | 177 | _particle[p].velocity += |
| 190 | 178 | glm::vec3 |
| 191 | 179 | ( |
| 192 | − -myEmitter.jitter * ONE_HALF + myEmitter.jitter * randFloat(), | |
| 193 | − -myEmitter.jitter * ONE_HALF + myEmitter.jitter * randFloat(), | |
| 194 | − -myEmitter.jitter * ONE_HALF + myEmitter.jitter * randFloat() | |
| 180 | + -myEmitter.particleAttributes.jitter * ONE_HALF + myEmitter.particleAttributes.jitter * randFloat(), | |
| 181 | + -myEmitter.particleAttributes.jitter * ONE_HALF + myEmitter.particleAttributes.jitter * randFloat(), | |
| 182 | + -myEmitter.particleAttributes.jitter * ONE_HALF + myEmitter.particleAttributes.jitter * randFloat() | |
| 195 | 183 | ) * deltaTime; |
| 196 | 184 | |
| 197 | 185 | // apply attraction to home position |
| 198 | 186 | glm::vec3 vectorToHome = myEmitter.position - _particle[p].position; |
| 199 | − _particle[p].velocity += vectorToHome * myEmitter.emitterAttraction * deltaTime; | |
| 187 | + _particle[p].velocity += vectorToHome * myEmitter.particleAttributes.emitterAttraction * deltaTime; | |
| 200 | 188 | |
| 201 | 189 | // apply neighbor attraction |
| 202 | 190 | int neighbor = p + 1; |
| @@ -207,20 +195,20 @@ void ParticleSystem::updateParticle(int p, float deltaTime) { · 4 unmodified lines | ||
| 207 | 195 | if ( _particle[neighbor].emitterIndex == _particle[p].emitterIndex) { |
| 208 | 196 | glm::vec3 vectorToNeighbor = _particle[p].position - _particle[neighbor].position; |
| 209 | 197 | |
| 210 | − _particle[p].velocity -= vectorToNeighbor * myEmitter.neighborAttraction * deltaTime; | |
| 198 | + _particle[p].velocity -= vectorToNeighbor * myEmitter.particleAttributes.neighborAttraction * deltaTime; | |
| 211 | 199 | |
| 212 | 200 | float distanceToNeighbor = glm::length(vectorToNeighbor); |
| 213 | 201 | if (distanceToNeighbor > 0.0f) { |
| 214 | − _particle[neighbor].velocity += (vectorToNeighbor / ( 1.0f + distanceToNeighbor * distanceToNeighbor)) * myEmitter.neighborRepulsion * deltaTime; | |
| 202 | + _particle[neighbor].velocity += (vectorToNeighbor / ( 1.0f + distanceToNeighbor * distanceToNeighbor)) * myEmitter.particleAttributes.neighborRepulsion * deltaTime; | |
| 215 | 203 | } |
| 216 | 204 | } |
| 217 | 205 | |
| 218 | 206 | // apply tornado force |
| 219 | 207 | glm::vec3 tornadoDirection = glm::cross(vectorToHome, myEmitter.up); |
| 220 | − _particle[p].velocity += tornadoDirection * myEmitter.tornadoForce * deltaTime; | |
| 208 | + _particle[p].velocity += tornadoDirection * myEmitter.particleAttributes.tornadoForce * deltaTime; | |
| 221 | 209 | |
| 222 | 210 | // apply air friction |
| 223 | − float drag = 1.0 - myEmitter.airFriction * deltaTime; | |
| 211 | + float drag = 1.0 - myEmitter.particleAttributes.airFriction * deltaTime; | |
| 224 | 212 | if (drag < 0.0f) { |
| 225 | 213 | _particle[p].velocity = glm::vec3(0.0f, 0.0f, 0.0f); |
| 226 | 214 | } else { |
| @@ -228,7 +216,7 @@ void ParticleSystem::updateParticle(int p, float deltaTime) { · 1 unmodified line | ||
| 228 | 216 | } |
| 229 | 217 | |
| 230 | 218 | // apply gravity |
| 231 | − _particle[p].velocity -= _upDirection * myEmitter.gravity * deltaTime; | |
| 219 | + _particle[p].velocity -= _upDirection * myEmitter.particleAttributes.gravity * deltaTime; | |
| 232 | 220 | |
| 233 | 221 | // update position by velocity |
| 234 | 222 | _particle[p].position += _particle[p].velocity; |
| @@ -238,29 +226,29 @@ void ParticleSystem::updateParticle(int p, float deltaTime) { · 3 unmodified lines | ||
| 238 | 226 | _particle[p].position.y = _particle[p].radius; |
| 239 | 227 | |
| 240 | 228 | if (_particle[p].velocity.y < 0.0f) { |
| 241 | − _particle[p].velocity.y *= -myEmitter.bounce; | |
| 229 | + _particle[p].velocity.y *= -myEmitter.particleAttributes.bounce; | |
| 242 | 230 | } |
| 243 | 231 | } |
| 244 | 232 | |
| 245 | 233 | // collision with sphere |
| 246 | − if (myEmitter.usingCollisionSphere) { | |
| 247 | − glm::vec3 vectorToSphereCenter = myEmitter.collisionSpherePosition - _particle[p].position; | |
| 234 | + if (myEmitter.particleAttributes.usingCollisionSphere) { | |
| 235 | + glm::vec3 vectorToSphereCenter = myEmitter.particleAttributes.collisionSpherePosition - _particle[p].position; | |
| 248 | 236 | float distanceToSphereCenter = glm::length(vectorToSphereCenter); |
| 249 | − float combinedRadius = myEmitter.collisionSphereRadius + _particle[p].radius; | |
| 237 | + float combinedRadius = myEmitter.particleAttributes.collisionSphereRadius + _particle[p].radius; | |
| 250 | 238 | if (distanceToSphereCenter < combinedRadius) { |
| 251 | 239 | |
| 252 | 240 | if (distanceToSphereCenter > 0.0f){ |
| 253 | 241 | glm::vec3 directionToSphereCenter = vectorToSphereCenter / distanceToSphereCenter; |
| 254 | − _particle[p].position = myEmitter.collisionSpherePosition - directionToSphereCenter * combinedRadius; | |
| 242 | + _particle[p].position = myEmitter.particleAttributes.collisionSpherePosition - directionToSphereCenter * combinedRadius; | |
| 255 | 243 | } |
| 256 | 244 | } |
| 257 | 245 | } |
| 258 | 246 | } |
| 259 | 247 | |
| 260 | 248 | void ParticleSystem::setCollisionSphere(int e, glm::vec3 position, float radius) { |
| 261 | − _emitter[e].usingCollisionSphere = true; | |
| 262 | − _emitter[e].collisionSpherePosition = position; | |
| 263 | − _emitter[e].collisionSphereRadius = radius; | |
| 249 | + _emitter[e].particleAttributes.usingCollisionSphere = true; | |
| 250 | + _emitter[e].particleAttributes.collisionSpherePosition = position; | |
| 251 | + _emitter[e].particleAttributes.collisionSphereRadius = radius; | |
| 264 | 252 | } |
| 265 | 253 | |
| 266 | 254 | void ParticleSystem::render() { |