[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"); |
---|
[3928] | 33 | this->direction = direction; |
---|
[3930] | 34 | this->setSpread(angle); |
---|
[3933] | 35 | this->setEmissionRate(emissionRate); |
---|
[3935] | 36 | this->setVelocity(velocity); |
---|
[3932] | 37 | |
---|
| 38 | this->saveTime = 0.0; |
---|
[3935] | 39 | |
---|
| 40 | ParticleEngine::getInstance()->addEmitter(this); |
---|
[3926] | 41 | } |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | \brief standard destructor |
---|
| 47 | |
---|
| 48 | */ |
---|
| 49 | ParticleEmitter::~ParticleEmitter () |
---|
[3935] | 50 | { |
---|
[3944] | 51 | ParticleEngine::getInstance()->removeEmitter(this); |
---|
[3935] | 52 | |
---|
| 53 | } |
---|
[3929] | 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 | /** |
---|
[3931] | 73 | \brief set the emission rate |
---|
| 74 | \param sets the number of particles emitted per second |
---|
[3929] | 75 | |
---|
| 76 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
[3930] | 77 | you may want to use the animation class |
---|
[3929] | 78 | */ |
---|
[3931] | 79 | void ParticleEmitter::setEmissionRate(float emissionRate) |
---|
[3933] | 80 | { |
---|
| 81 | this->emissionRate = emissionRate; |
---|
| 82 | } |
---|
[3929] | 83 | |
---|
| 84 | /** |
---|
[3931] | 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 |
---|
[3929] | 88 | |
---|
| 89 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
[3930] | 90 | you may want to use the animation class |
---|
[3929] | 91 | */ |
---|
[3931] | 92 | void ParticleEmitter::setSpread(float angle, float randomAngle) |
---|
[3935] | 93 | { |
---|
| 94 | this->angle = angle; |
---|
| 95 | this->randomAngle = randomAngle; |
---|
| 96 | } |
---|
[3929] | 97 | |
---|
| 98 | /** |
---|
| 99 | \brief sets the velocity of all particles emitted |
---|
[3930] | 100 | \param velocity The starting velocity of the emitted particles |
---|
| 101 | \param random A random starting velocity, the +- randomness of this option |
---|
[3929] | 102 | |
---|
| 103 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
[3930] | 104 | you may want to use the animation class |
---|
[3929] | 105 | */ |
---|
[3935] | 106 | void ParticleEmitter::setVelocity(float velocity, float randomVelocity) |
---|
| 107 | { |
---|
| 108 | this->velocity = velocity; |
---|
| 109 | this->randomVelocity = randomVelocity; |
---|
| 110 | } |
---|
[3929] | 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) |
---|
[3930] | 117 | you may want to use the animation class |
---|
[3929] | 118 | */ |
---|
| 119 | |
---|
[3932] | 120 | void ParticleEmitter::tick(float dt, ParticleSystem* system) |
---|
| 121 | { |
---|
[3950] | 122 | if (likely(dt > 0.0 && this->emissionRate > 0.0)) |
---|
| 123 | { |
---|
| 124 | // saving the time |
---|
| 125 | float count = (dt+this->saveTime) * this->emissionRate; |
---|
[4017] | 126 | this->saveTime = modff(count, &count) / this->emissionRate; |
---|
[3950] | 127 | PRINTF(5)("emitting %f particles, saving %f seconds for the next round\n", count, this->saveTime); |
---|
| 128 | |
---|
[4017] | 129 | if (likely(count > 0)) |
---|
[3950] | 130 | { |
---|
[4017] | 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 | system->addParticle(this->getAbsCoor(), velocityV); |
---|
| 141 | } |
---|
[3950] | 142 | } |
---|
| 143 | } |
---|
[3932] | 144 | } |
---|
[3944] | 145 | |
---|
| 146 | /** |
---|
| 147 | \brief outputs some nice debug information |
---|
| 148 | */ |
---|
| 149 | void ParticleEmitter::debug(void) |
---|
| 150 | { |
---|
| 151 | |
---|
| 152 | } |
---|