[3926] | 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: |
---|
[3938] | 12 | main-programmer: Benjamin Grauer |
---|
[3926] | 13 | co-programmer: Patrick Boenzli |
---|
| 14 | */ |
---|
| 15 | |
---|
[3938] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PARTICLE |
---|
[3926] | 17 | |
---|
| 18 | #include "particle_emitter.h" |
---|
| 19 | |
---|
[3932] | 20 | #include "particle_system.h" |
---|
[3935] | 21 | #include "particle_engine.h" |
---|
[3932] | 22 | |
---|
[3926] | 23 | using namespace std; |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | \brief standard constructor |
---|
| 28 | */ |
---|
[3928] | 29 | ParticleEmitter::ParticleEmitter(const Vector& direction, float angle, float emissionRate, |
---|
[3931] | 30 | float velocity) |
---|
[3926] | 31 | { |
---|
| 32 | this->setClassName ("ParticleEmitter"); |
---|
[4274] | 33 | |
---|
| 34 | this->type = EMITTER_DOT; |
---|
| 35 | this->emitterSize = 1.0; |
---|
[3928] | 36 | this->direction = direction; |
---|
[3930] | 37 | this->setSpread(angle); |
---|
[3933] | 38 | this->setEmissionRate(emissionRate); |
---|
[3935] | 39 | this->setVelocity(velocity); |
---|
[3932] | 40 | |
---|
| 41 | this->saveTime = 0.0; |
---|
[3935] | 42 | |
---|
| 43 | ParticleEngine::getInstance()->addEmitter(this); |
---|
[3926] | 44 | } |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | \brief standard destructor |
---|
| 50 | |
---|
| 51 | */ |
---|
| 52 | ParticleEmitter::~ParticleEmitter () |
---|
[3935] | 53 | { |
---|
[3944] | 54 | ParticleEngine::getInstance()->removeEmitter(this); |
---|
[3935] | 55 | } |
---|
[3929] | 56 | |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | \brief this start the emitter |
---|
| 60 | */ |
---|
| 61 | void ParticleEmitter::start() {} |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | \brief this stops the emitter |
---|
| 66 | */ |
---|
| 67 | void ParticleEmitter::stop() {} |
---|
| 68 | |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | |
---|
| 72 | /* these are Animation interfaces: so you can change spec values as you want */ |
---|
| 73 | |
---|
| 74 | /** |
---|
[4274] | 75 | \param type the new Type of this emitter |
---|
| 76 | */ |
---|
| 77 | void ParticleEmitter::setType(EMITTER_TYPE type) |
---|
| 78 | { |
---|
| 79 | this->type = type; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | void ParticleEmitter::setSize(float emitterSize) |
---|
| 83 | { |
---|
| 84 | this->emitterSize = emitterSize; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
[3931] | 88 | \brief set the emission rate |
---|
| 89 | \param sets the number of particles emitted per second |
---|
[3929] | 90 | |
---|
| 91 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
[3930] | 92 | you may want to use the animation class |
---|
[3929] | 93 | */ |
---|
[3931] | 94 | void ParticleEmitter::setEmissionRate(float emissionRate) |
---|
[3933] | 95 | { |
---|
| 96 | this->emissionRate = emissionRate; |
---|
| 97 | } |
---|
[3929] | 98 | |
---|
| 99 | /** |
---|
[3931] | 100 | \brief set the angle of the emitter |
---|
| 101 | \param angle around the direction in which there are particles to be emitted |
---|
| 102 | \param randomAngle A random spread-angle, the +- randomness of this option |
---|
[3929] | 103 | |
---|
| 104 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
[3930] | 105 | you may want to use the animation class |
---|
[3929] | 106 | */ |
---|
[3931] | 107 | void ParticleEmitter::setSpread(float angle, float randomAngle) |
---|
[3935] | 108 | { |
---|
| 109 | this->angle = angle; |
---|
| 110 | this->randomAngle = randomAngle; |
---|
| 111 | } |
---|
[3929] | 112 | |
---|
| 113 | /** |
---|
| 114 | \brief sets the velocity of all particles emitted |
---|
[3930] | 115 | \param velocity The starting velocity of the emitted particles |
---|
| 116 | \param random A random starting velocity, the +- randomness of this option |
---|
[3929] | 117 | |
---|
| 118 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
[3930] | 119 | you may want to use the animation class |
---|
[3929] | 120 | */ |
---|
[3935] | 121 | void ParticleEmitter::setVelocity(float velocity, float randomVelocity) |
---|
| 122 | { |
---|
| 123 | this->velocity = velocity; |
---|
| 124 | this->randomVelocity = randomVelocity; |
---|
| 125 | } |
---|
[3929] | 126 | |
---|
| 127 | /** |
---|
| 128 | \brief this set the time to life of a particle, after which it will die |
---|
| 129 | \param the time to live in seconds |
---|
| 130 | |
---|
| 131 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
[3930] | 132 | you may want to use the animation class |
---|
[3929] | 133 | */ |
---|
| 134 | |
---|
[3932] | 135 | void ParticleEmitter::tick(float dt, ParticleSystem* system) |
---|
| 136 | { |
---|
[3950] | 137 | if (likely(dt > 0.0 && this->emissionRate > 0.0)) |
---|
| 138 | { |
---|
| 139 | // saving the time |
---|
| 140 | float count = (dt+this->saveTime) * this->emissionRate; |
---|
[4017] | 141 | this->saveTime = modff(count, &count) / this->emissionRate; |
---|
[3950] | 142 | PRINTF(5)("emitting %f particles, saving %f seconds for the next round\n", count, this->saveTime); |
---|
| 143 | |
---|
[4017] | 144 | if (likely(count > 0)) |
---|
[3950] | 145 | { |
---|
[4017] | 146 | Vector inheritVelocity = this->getVelocity() * system->inheritSpeed; |
---|
| 147 | for (int i = 0; i < count; i++) |
---|
| 148 | // emmits from EMITTER_DOT, |
---|
| 149 | { |
---|
| 150 | Vector randDir = Vector(rand()-RAND_MAX/2, rand()-RAND_MAX/2, rand()-RAND_MAX/2); |
---|
| 151 | randDir.normalize(); |
---|
| 152 | randDir = (this->getAbsDir()*Quaternion(angle + randomAngle *((float)rand()/RAND_MAX -.5), randDir)).apply(this->direction); |
---|
| 153 | Vector velocityV = randDir.getNormalized()*this->velocity + inheritVelocity; |
---|
[4176] | 154 | |
---|
| 155 | // this should spread the Particles evenly. if the Emitter is moved around quickly |
---|
| 156 | Vector equalSpread = this->getVelocity() * random()/RAND_MAX * dt; |
---|
[4274] | 157 | Vector extension; // the Vector for different fields. |
---|
[4176] | 158 | |
---|
[4274] | 159 | if (this->type & 2) |
---|
| 160 | { |
---|
| 161 | extension = Vector(this->emitterSize * ((float)rand()/RAND_MAX -.5), 0, this->emitterSize * ((float)rand()/RAND_MAX - .5)); |
---|
| 162 | extension = this->getAbsDir().apply(extension); |
---|
| 163 | } |
---|
| 164 | else if (this->type & 8) |
---|
| 165 | { |
---|
| 166 | extension = Vector((float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5) * this->emitterSize; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | system->addParticle(this->getAbsCoor() + extension - equalSpread, velocityV); |
---|
| 170 | |
---|
[4017] | 171 | } |
---|
[3950] | 172 | } |
---|
| 173 | } |
---|
[3932] | 174 | } |
---|
[3944] | 175 | |
---|
| 176 | /** |
---|
| 177 | \brief outputs some nice debug information |
---|
| 178 | */ |
---|
| 179 | void ParticleEmitter::debug(void) |
---|
| 180 | { |
---|
| 181 | |
---|
| 182 | } |
---|