[4597] | 1 | /* |
---|
[3926] | 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 | |
---|
[5357] | 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS |
---|
[3926] | 17 | |
---|
| 18 | #include "particle_emitter.h" |
---|
| 19 | |
---|
[3932] | 20 | #include "particle_system.h" |
---|
| 21 | |
---|
[4437] | 22 | #include "load_param.h" |
---|
[4381] | 23 | #include "debug.h" |
---|
| 24 | #include "stdlibincl.h" |
---|
| 25 | |
---|
[3926] | 26 | using namespace std; |
---|
| 27 | |
---|
| 28 | /** |
---|
[4836] | 29 | * standard constructor |
---|
[3926] | 30 | */ |
---|
[6825] | 31 | ParticleEmitter::ParticleEmitter(float emissionRate, float velocity, float angle) |
---|
[3926] | 32 | { |
---|
[6822] | 33 | this->setClassID(CL_PARTICLE_EMITTER, "ParticleEmitter"); |
---|
[4726] | 34 | |
---|
[6822] | 35 | this->system = NULL; |
---|
| 36 | |
---|
| 37 | this->setInheritSpeed(PARTICLE_EMITTER_DEFAULT_INHERIT_SPEED); |
---|
| 38 | this->setEmissionMomentum(0); |
---|
[4439] | 39 | this->setSpread(angle); |
---|
| 40 | this->setEmissionRate(emissionRate); |
---|
| 41 | this->setEmissionVelocity(velocity); |
---|
[4437] | 42 | |
---|
[6822] | 43 | this->saveTime = 0.0; |
---|
[3926] | 44 | } |
---|
| 45 | |
---|
| 46 | /** |
---|
[4836] | 47 | * standard destructor |
---|
[3926] | 48 | |
---|
[4496] | 49 | removes the EmitterSystem from the ParticleEngine |
---|
[3926] | 50 | */ |
---|
[4597] | 51 | ParticleEmitter::~ParticleEmitter () |
---|
[3935] | 52 | { |
---|
[6623] | 53 | this->setSystem(NULL); |
---|
[3935] | 54 | } |
---|
[3929] | 55 | |
---|
[4478] | 56 | /** |
---|
[4836] | 57 | * loads a ParticleEmitter from a XML-element |
---|
| 58 | * @param root the XML-element to load from |
---|
[4478] | 59 | */ |
---|
[4437] | 60 | void ParticleEmitter::loadParams(const TiXmlElement* root) |
---|
| 61 | { |
---|
[6512] | 62 | PNode::loadParams(root); |
---|
[3929] | 63 | |
---|
[5671] | 64 | LoadParam(root, "rate", this, ParticleEmitter, setEmissionRate) |
---|
[4437] | 65 | .describe("How many particles should be emittet from this emitter"); |
---|
| 66 | |
---|
[5671] | 67 | LoadParam(root, "inherit-speed", this, ParticleEmitter, setInheritSpeed) |
---|
[4494] | 68 | .describe("the extent, the speed of the emitter has on the particles"); |
---|
| 69 | |
---|
[5671] | 70 | LoadParam(root, "emission-velocity", this, ParticleEmitter, setEmissionVelocity) |
---|
[4437] | 71 | .describe("How fast the particles are emittet (their initial speed)"); |
---|
[4597] | 72 | |
---|
[5671] | 73 | LoadParam(root, "emission-momentum", this, ParticleEmitter, setEmissionMomentum) |
---|
[4725] | 74 | .describe("How fast the particles rotation is at emissiontime (their initial momentum)"); |
---|
| 75 | |
---|
[5671] | 76 | LoadParam(root, "spread", this, ParticleEmitter, setSpread) |
---|
[4437] | 77 | .describe("The angle the particles are emitted from (angle, deviation)"); |
---|
| 78 | } |
---|
| 79 | |
---|
[6619] | 80 | void ParticleEmitter::setSystem(ParticleSystem* system) |
---|
| 81 | { |
---|
| 82 | if (system != NULL) |
---|
| 83 | system->addEmitter(this); |
---|
| 84 | else if (this->system != NULL) |
---|
| 85 | this->system->removeEmitter(this); |
---|
| 86 | } |
---|
| 87 | |
---|
[3929] | 88 | /** |
---|
[4836] | 89 | * this start the emitter |
---|
[3929] | 90 | */ |
---|
| 91 | void ParticleEmitter::start() {} |
---|
| 92 | |
---|
| 93 | |
---|
| 94 | /** |
---|
[4836] | 95 | * this stops the emitter |
---|
[3929] | 96 | */ |
---|
| 97 | void ParticleEmitter::stop() {} |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | /** |
---|
[4836] | 101 | * set the emission rate |
---|
| 102 | * @param emissionRate: sets the number of particles emitted per second |
---|
[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::setEmissionRate(float emissionRate) |
---|
[3933] | 108 | { |
---|
[4338] | 109 | if (emissionRate > 0.0) |
---|
| 110 | this->emissionRate = emissionRate; |
---|
| 111 | else |
---|
| 112 | this->emissionRate = 0.0; |
---|
[3933] | 113 | } |
---|
[3929] | 114 | |
---|
| 115 | /** |
---|
[4836] | 116 | * how much of the speed from the ParticleEmitter should flow onto the ParticleSystem |
---|
| 117 | * @param value a Value between zero and one |
---|
[4493] | 118 | |
---|
| 119 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
| 120 | you may want to use the animation class |
---|
| 121 | */ |
---|
| 122 | void ParticleEmitter::setInheritSpeed(float value) |
---|
| 123 | { |
---|
| 124 | if (unlikely(value > 1.0)) |
---|
| 125 | this->inheritSpeed = 1; |
---|
| 126 | else if (unlikely(value < 0.0)) |
---|
| 127 | this->inheritSpeed = 0; |
---|
| 128 | else |
---|
| 129 | this->inheritSpeed = value; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | /** |
---|
[4836] | 133 | * set the angle of the emitter |
---|
| 134 | * @param angle around the direction in which there are particles to be emitted |
---|
| 135 | * @param randomAngle A random spread-angle, the +- randomness of this option |
---|
[3929] | 136 | |
---|
| 137 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
[3930] | 138 | you may want to use the animation class |
---|
[3929] | 139 | */ |
---|
[3931] | 140 | void ParticleEmitter::setSpread(float angle, float randomAngle) |
---|
[3935] | 141 | { |
---|
| 142 | this->angle = angle; |
---|
| 143 | this->randomAngle = randomAngle; |
---|
| 144 | } |
---|
[3929] | 145 | |
---|
| 146 | /** |
---|
[4836] | 147 | * sets the initial velocity of all particles emitted |
---|
| 148 | * @param velocity The starting velocity of the emitted particles |
---|
| 149 | * @param randomVelocity A random starting velocity, the +- randomness of this option |
---|
[3929] | 150 | |
---|
| 151 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
[3930] | 152 | you may want to use the animation class |
---|
[3929] | 153 | */ |
---|
[4338] | 154 | void ParticleEmitter::setEmissionVelocity(float velocity, float randomVelocity) |
---|
[3935] | 155 | { |
---|
| 156 | this->velocity = velocity; |
---|
| 157 | this->randomVelocity = randomVelocity; |
---|
| 158 | } |
---|
[3929] | 159 | |
---|
| 160 | /** |
---|
[4836] | 161 | * sets the initial Momentum of all particles emitted |
---|
| 162 | * @param momentum the new Momentum (just a float for being not too complicated). |
---|
| 163 | * @param randomMomentum variation from the given value. |
---|
[4690] | 164 | */ |
---|
| 165 | void ParticleEmitter::setEmissionMomentum(float momentum, float randomMomentum) |
---|
| 166 | { |
---|
| 167 | this->momentum = momentum; |
---|
| 168 | this->momentumRandom = randomMomentum; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
[4836] | 172 | * this set the time to life of a particle, after which it will die |
---|
| 173 | * @param dt: the time to live in seconds |
---|
| 174 | * @param system: the system into which to emitt |
---|
[3929] | 175 | |
---|
| 176 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
[3930] | 177 | you may want to use the animation class |
---|
[3929] | 178 | */ |
---|
[6620] | 179 | void ParticleEmitter::tick(float dt) |
---|
[3932] | 180 | { |
---|
[6620] | 181 | assert (this->system != NULL); |
---|
[3950] | 182 | if (likely(dt > 0.0 && this->emissionRate > 0.0)) |
---|
| 183 | { |
---|
[4692] | 184 | // saving the time (particles only partly emitted in this timestep) |
---|
[3950] | 185 | float count = (dt+this->saveTime) * this->emissionRate; |
---|
[4017] | 186 | this->saveTime = modff(count, &count) / this->emissionRate; |
---|
[4692] | 187 | PRINTF(5)("emitting %f particles, saving %f seconds for the next timestep\n", count, this->saveTime); |
---|
[4597] | 188 | |
---|
[6822] | 189 | if (likely(count > 0.0f)) |
---|
[4690] | 190 | { |
---|
[6822] | 191 | this->emitParticles((unsigned int)count); |
---|
| 192 | |
---|
| 193 | /* Vector inheritVelocity = this->getVelocity() * this->inheritSpeed; |
---|
[4693] | 194 | for (int i = 0; i < (int)count; i++) |
---|
[3950] | 195 | { |
---|
[4690] | 196 | Vector randDir = Vector(rand()-RAND_MAX/2, rand()-RAND_MAX/2, rand()-RAND_MAX/2); |
---|
| 197 | randDir.normalize(); |
---|
| 198 | randDir = (this->getAbsDir()*Quaternion(angle + randomAngle *((float)rand()/RAND_MAX -.5), randDir)).apply(this->direction); |
---|
| 199 | Vector velocityV = randDir.getNormalized()*this->velocity + inheritVelocity; |
---|
[4176] | 200 | |
---|
[4692] | 201 | // this should spread the Particles evenly. if the Emitter is moved around quickly |
---|
[4690] | 202 | Vector equalSpread = this->getVelocity() * rand()/RAND_MAX * dt; |
---|
| 203 | Vector extension; // the Vector for different fields. |
---|
[4176] | 204 | |
---|
[6822] | 205 | // if (this->type & EMITTER_PLANE) |
---|
| 206 | // { |
---|
| 207 | // extension = Vector(this->emitterSize * ((float)rand()/RAND_MAX -.5), 0, this->emitterSize * ((float)rand()/RAND_MAX - .5)); |
---|
| 208 | // extension = this->getAbsDir().apply(extension); |
---|
| 209 | // } |
---|
| 210 | // else if (this->type & EMITTER_CUBE) |
---|
| 211 | // { |
---|
| 212 | // extension = Vector((float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5) * this->emitterSize; |
---|
| 213 | // } |
---|
[4338] | 214 | |
---|
[4597] | 215 | |
---|
[4690] | 216 | // ROTATIONAL CALCULATION (this must not be done for all types of particles.) |
---|
| 217 | randDir = Vector(rand()-RAND_MAX/2, rand()-RAND_MAX/2, rand()-RAND_MAX/2); |
---|
| 218 | randDir.normalize(); |
---|
| 219 | Quaternion orient = Quaternion(M_PI, randDir); |
---|
| 220 | Quaternion moment = Quaternion(this->momentum + this->momentumRandom, randDir); |
---|
| 221 | |
---|
[6620] | 222 | this->system->addParticle(this->getAbsCoor() + extension - equalSpread, velocityV, orient, moment); |
---|
[6822] | 223 | } */ |
---|
[4690] | 224 | } |
---|
[3950] | 225 | } |
---|
[3932] | 226 | } |
---|
[3944] | 227 | |
---|
| 228 | /** |
---|
[4836] | 229 | * outputs some nice debug information |
---|
[3944] | 230 | */ |
---|
[4746] | 231 | void ParticleEmitter::debug() const |
---|
[3944] | 232 | { |
---|
[6822] | 233 | PRINT(0)(" ParticleEmitter %s::%s\n", this->getClassName(), this->getName()); |
---|
[4639] | 234 | PRINT(0)(" EmissionRate: %f, Speed: %f, SpreadAngle: %f\n", this->getEmissionRate(), this->getEmissionVelocity(), this->getSpread()); |
---|
[3944] | 235 | } |
---|