| 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->setClassID(CL_PARTICLE_EMITTER, "ParticleEmitter"); |
|---|
| 33 | |
|---|
| 34 | this->type = EMITTER_DOT; |
|---|
| 35 | this->emitterSize = 1.0; |
|---|
| 36 | this->direction = direction; |
|---|
| 37 | this->setSpread(angle); |
|---|
| 38 | this->setEmissionRate(emissionRate); |
|---|
| 39 | this->setEmissionVelocity(velocity); |
|---|
| 40 | |
|---|
| 41 | this->saveTime = 0.0; |
|---|
| 42 | |
|---|
| 43 | ParticleEngine::getInstance()->addEmitter(this); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | \brief standard destructor |
|---|
| 50 | |
|---|
| 51 | */ |
|---|
| 52 | ParticleEmitter::~ParticleEmitter () |
|---|
| 53 | { |
|---|
| 54 | ParticleEngine::getInstance()->removeEmitter(this); |
|---|
| 55 | } |
|---|
| 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 | /** |
|---|
| 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 | if (emitterSize > 0.0) |
|---|
| 85 | this->emitterSize = emitterSize; |
|---|
| 86 | else |
|---|
| 87 | emitterSize = 0.0; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /** |
|---|
| 91 | \brief set the emission rate |
|---|
| 92 | \param sets the number of particles emitted per second |
|---|
| 93 | |
|---|
| 94 | if you want to change the value of this variable during emission time (to make it more dynamic) |
|---|
| 95 | you may want to use the animation class |
|---|
| 96 | */ |
|---|
| 97 | void ParticleEmitter::setEmissionRate(float emissionRate) |
|---|
| 98 | { |
|---|
| 99 | if (emissionRate > 0.0) |
|---|
| 100 | this->emissionRate = emissionRate; |
|---|
| 101 | else |
|---|
| 102 | this->emissionRate = 0.0; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | \brief set the angle of the emitter |
|---|
| 107 | \param angle around the direction in which there are particles to be emitted |
|---|
| 108 | \param randomAngle A random spread-angle, the +- randomness of this option |
|---|
| 109 | |
|---|
| 110 | if you want to change the value of this variable during emission time (to make it more dynamic) |
|---|
| 111 | you may want to use the animation class |
|---|
| 112 | */ |
|---|
| 113 | void ParticleEmitter::setSpread(float angle, float randomAngle) |
|---|
| 114 | { |
|---|
| 115 | this->angle = angle; |
|---|
| 116 | this->randomAngle = randomAngle; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | /** |
|---|
| 120 | \brief sets the velocity of all particles emitted |
|---|
| 121 | \param velocity The starting velocity of the emitted particles |
|---|
| 122 | \param random A random starting velocity, the +- randomness of this option |
|---|
| 123 | |
|---|
| 124 | if you want to change the value of this variable during emission time (to make it more dynamic) |
|---|
| 125 | you may want to use the animation class |
|---|
| 126 | */ |
|---|
| 127 | void ParticleEmitter::setEmissionVelocity(float velocity, float randomVelocity) |
|---|
| 128 | { |
|---|
| 129 | this->velocity = velocity; |
|---|
| 130 | this->randomVelocity = randomVelocity; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | /** |
|---|
| 134 | \brief this set the time to life of a particle, after which it will die |
|---|
| 135 | \param the time to live in seconds |
|---|
| 136 | |
|---|
| 137 | if you want to change the value of this variable during emission time (to make it more dynamic) |
|---|
| 138 | you may want to use the animation class |
|---|
| 139 | */ |
|---|
| 140 | |
|---|
| 141 | void ParticleEmitter::tick(float dt, ParticleSystem* system) |
|---|
| 142 | { |
|---|
| 143 | if (likely(dt > 0.0 && this->emissionRate > 0.0)) |
|---|
| 144 | { |
|---|
| 145 | // saving the time |
|---|
| 146 | float count = (dt+this->saveTime) * this->emissionRate; |
|---|
| 147 | this->saveTime = modff(count, &count) / this->emissionRate; |
|---|
| 148 | PRINTF(5)("emitting %f particles, saving %f seconds for the next round\n", count, this->saveTime); |
|---|
| 149 | |
|---|
| 150 | if (likely(count > 0)) |
|---|
| 151 | { |
|---|
| 152 | Vector inheritVelocity = this->getVelocity() * system->inheritSpeed; |
|---|
| 153 | for (int i = 0; i < count; i++) |
|---|
| 154 | // emmits from EMITTER_DOT, |
|---|
| 155 | { |
|---|
| 156 | Vector randDir = Vector(rand()-RAND_MAX/2, rand()-RAND_MAX/2, rand()-RAND_MAX/2); |
|---|
| 157 | randDir.normalize(); |
|---|
| 158 | randDir = (this->getAbsDir()*Quaternion(angle + randomAngle *((float)rand()/RAND_MAX -.5), randDir)).apply(this->direction); |
|---|
| 159 | Vector velocityV = randDir.getNormalized()*this->velocity + inheritVelocity; |
|---|
| 160 | |
|---|
| 161 | // this should spread the Particles evenly. if the Emitter is moved around quickly |
|---|
| 162 | Vector equalSpread = this->getVelocity() * rand()/RAND_MAX * dt; |
|---|
| 163 | Vector extension; // the Vector for different fields. |
|---|
| 164 | |
|---|
| 165 | if (this->type & 2) |
|---|
| 166 | { |
|---|
| 167 | extension = Vector(this->emitterSize * ((float)rand()/RAND_MAX -.5), 0, this->emitterSize * ((float)rand()/RAND_MAX - .5)); |
|---|
| 168 | extension = this->getAbsDir().apply(extension); |
|---|
| 169 | } |
|---|
| 170 | else if (this->type & 8) |
|---|
| 171 | { |
|---|
| 172 | extension = Vector((float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5) * this->emitterSize; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | system->addParticle(this->getAbsCoor() + extension - equalSpread, velocityV); |
|---|
| 176 | |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | /** |
|---|
| 183 | \brief outputs some nice debug information |
|---|
| 184 | */ |
|---|
| 185 | void ParticleEmitter::debug(void) |
|---|
| 186 | { |
|---|
| 187 | |
|---|
| 188 | } |
|---|