| 1 | /*  | 
|---|
| 2 |    orxonox - the future of 3D-vertical-scrollers | 
|---|
| 3 |  | 
|---|
| 4 |    Copyright (C) 2004 orx | 
|---|
| 5 |  | 
|---|
| 6 |    This program is free software; you can redistribute it and/or modify | 
|---|
| 7 |    it under the terms of the GNU General Public License as published by | 
|---|
| 8 |    the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 9 |    any later version. | 
|---|
| 10 |  | 
|---|
| 11 |    ### File Specific: | 
|---|
| 12 |    main-programmer: Benjamin Grauer | 
|---|
| 13 |    co-programmer: Patrick Boenzli | 
|---|
| 14 | */ | 
|---|
| 15 |  | 
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PARTICLE | 
|---|
| 17 |  | 
|---|
| 18 | #include "particle_emitter.h" | 
|---|
| 19 |  | 
|---|
| 20 | #include "particle_system.h" | 
|---|
| 21 | #include "particle_engine.h" | 
|---|
| 22 |  | 
|---|
| 23 | using namespace std; | 
|---|
| 24 |  | 
|---|
| 25 |  | 
|---|
| 26 | /** | 
|---|
| 27 |    \brief standard constructor | 
|---|
| 28 | */ | 
|---|
| 29 | ParticleEmitter::ParticleEmitter(const Vector& direction, float angle, float emissionRate,  | 
|---|
| 30 |                   float velocity) | 
|---|
| 31 | { | 
|---|
| 32 |    this->setClassName ("ParticleEmitter"); | 
|---|
| 33 |    this->direction = direction; | 
|---|
| 34 |    this->setSpread(angle); | 
|---|
| 35 |    this->setEmissionRate(emissionRate); | 
|---|
| 36 |    this->setVelocity(velocity); | 
|---|
| 37 |  | 
|---|
| 38 |    this->saveTime = 0.0; | 
|---|
| 39 |  | 
|---|
| 40 |    ParticleEngine::getInstance()->addEmitter(this); | 
|---|
| 41 | } | 
|---|
| 42 |  | 
|---|
| 43 |  | 
|---|
| 44 |  | 
|---|
| 45 | /** | 
|---|
| 46 |    \brief standard destructor | 
|---|
| 47 |  | 
|---|
| 48 | */ | 
|---|
| 49 | ParticleEmitter::~ParticleEmitter ()  | 
|---|
| 50 | { | 
|---|
| 51 |   ParticleEngine::getInstance()->removeEmitter(this); | 
|---|
| 52 |    | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 |  | 
|---|
| 56 | /** | 
|---|
| 57 |    \brief this start the emitter | 
|---|
| 58 | */ | 
|---|
| 59 | void ParticleEmitter::start() {} | 
|---|
| 60 |  | 
|---|
| 61 |  | 
|---|
| 62 | /** | 
|---|
| 63 |    \brief this stops the emitter | 
|---|
| 64 | */ | 
|---|
| 65 | void ParticleEmitter::stop() {} | 
|---|
| 66 |  | 
|---|
| 67 |  | 
|---|
| 68 |  | 
|---|
| 69 |  | 
|---|
| 70 | /* these are Animation interfaces: so you can change spec values as you want */ | 
|---|
| 71 |  | 
|---|
| 72 | /** | 
|---|
| 73 |    \brief set the emission rate | 
|---|
| 74 |    \param sets the number of particles emitted per second | 
|---|
| 75 |  | 
|---|
| 76 |    if you want to change the value of this variable during emission time (to make it more dynamic) | 
|---|
| 77 |    you may want to use the animation class | 
|---|
| 78 | */ | 
|---|
| 79 | void ParticleEmitter::setEmissionRate(float emissionRate) | 
|---|
| 80 | { | 
|---|
| 81 |   this->emissionRate = emissionRate; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | /** | 
|---|
| 85 |    \brief set the angle of the emitter | 
|---|
| 86 |    \param angle around the direction in which there are particles to be emitted | 
|---|
| 87 |    \param randomAngle A random spread-angle, the +- randomness of this option | 
|---|
| 88 |  | 
|---|
| 89 |    if you want to change the value of this variable during emission time (to make it more dynamic) | 
|---|
| 90 |    you may want to use the animation class | 
|---|
| 91 | */ | 
|---|
| 92 | void ParticleEmitter::setSpread(float angle, float randomAngle) | 
|---|
| 93 | { | 
|---|
| 94 |   this->angle = angle; | 
|---|
| 95 |   this->randomAngle = randomAngle; | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | /** | 
|---|
| 99 |    \brief sets the velocity of all particles emitted | 
|---|
| 100 |    \param velocity The starting velocity of the emitted particles | 
|---|
| 101 |    \param random A random starting velocity, the +- randomness of this option | 
|---|
| 102 |  | 
|---|
| 103 |    if you want to change the value of this variable during emission time (to make it more dynamic) | 
|---|
| 104 |    you may want to use the animation class | 
|---|
| 105 | */ | 
|---|
| 106 | void ParticleEmitter::setVelocity(float velocity, float randomVelocity) | 
|---|
| 107 | { | 
|---|
| 108 |   this->velocity = velocity; | 
|---|
| 109 |   this->randomVelocity = randomVelocity; | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | /** | 
|---|
| 113 |    \brief this set the time to life of a particle, after which it will die | 
|---|
| 114 |    \param the time to live in seconds | 
|---|
| 115 |  | 
|---|
| 116 |    if you want to change the value of this variable during emission time (to make it more dynamic) | 
|---|
| 117 |    you may want to use the animation class | 
|---|
| 118 | */ | 
|---|
| 119 |  | 
|---|
| 120 | void ParticleEmitter::tick(float dt, ParticleSystem* system) | 
|---|
| 121 | { | 
|---|
| 122 |   if (likely(dt > 0.0 && this->emissionRate > 0.0)) | 
|---|
| 123 |   { | 
|---|
| 124 |     // saving the time | 
|---|
| 125 |     float count = (dt+this->saveTime) * this->emissionRate; | 
|---|
| 126 |     this->saveTime = modff(count, &count) / this->emissionRate; | 
|---|
| 127 |     PRINTF(5)("emitting %f particles, saving %f seconds for the next round\n", count, this->saveTime);  | 
|---|
| 128 |      | 
|---|
| 129 |     if (likely(count > 0)) | 
|---|
| 130 |       { | 
|---|
| 131 |         Vector inheritVelocity = this->getVelocity() * system->inheritSpeed; | 
|---|
| 132 |         for (int i = 0; i < count; i++) | 
|---|
| 133 |           // emmits from EMITTER_DOT, | 
|---|
| 134 |           { | 
|---|
| 135 |             Vector randDir = Vector(rand()-RAND_MAX/2, rand()-RAND_MAX/2, rand()-RAND_MAX/2); | 
|---|
| 136 |             randDir.normalize(); | 
|---|
| 137 |             randDir = (this->getAbsDir()*Quaternion(angle + randomAngle *((float)rand()/RAND_MAX -.5), randDir)).apply(this->direction); | 
|---|
| 138 |             Vector velocityV = randDir.getNormalized()*this->velocity + inheritVelocity; | 
|---|
| 139 |  | 
|---|
| 140 |             // this should spread the Particles evenly. if the Emitter is moved around quickly | 
|---|
| 141 |             Vector equalSpread = this->getVelocity() * random()/RAND_MAX * dt; | 
|---|
| 142 |  | 
|---|
| 143 |             system->addParticle(this->getAbsCoor() - equalSpread, velocityV); | 
|---|
| 144 |           } | 
|---|
| 145 |       } | 
|---|
| 146 |   } | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | /** | 
|---|
| 150 |    \brief outputs some nice debug information | 
|---|
| 151 | */ | 
|---|
| 152 | void ParticleEmitter::debug(void) | 
|---|
| 153 | { | 
|---|
| 154 |  | 
|---|
| 155 | } | 
|---|