| 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_GRAPHICS | 
|---|
| 17 |  | 
|---|
| 18 | #include "particle_emitter.h" | 
|---|
| 19 |  | 
|---|
| 20 | #include "particle_system.h" | 
|---|
| 21 |  | 
|---|
| 22 | #include "util/loading/load_param.h" | 
|---|
| 23 | #include "debug.h" | 
|---|
| 24 |  | 
|---|
| 25 | ObjectListDefinition(ParticleEmitter); | 
|---|
| 26 |  | 
|---|
| 27 | /** | 
|---|
| 28 |  *  standard constructor | 
|---|
| 29 | */ | 
|---|
| 30 | ParticleEmitter::ParticleEmitter(float emissionRate, float velocity, float angle) | 
|---|
| 31 | { | 
|---|
| 32 |   this->registerObject(this, ParticleEmitter::_objectList); | 
|---|
| 33 |  | 
|---|
| 34 |   this->system = NULL; | 
|---|
| 35 |  | 
|---|
| 36 |   this->setInheritSpeed(PARTICLE_EMITTER_DEFAULT_INHERIT_SPEED); | 
|---|
| 37 |   this->setEmissionMomentum(0); | 
|---|
| 38 |   this->setSpread(angle); | 
|---|
| 39 |   this->setEmissionRate(emissionRate); | 
|---|
| 40 |   this->setEmissionVelocity(velocity); | 
|---|
| 41 |  | 
|---|
| 42 |   this->saveTime = 0.0; | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | /** | 
|---|
| 46 |  *  standard destructor | 
|---|
| 47 |  | 
|---|
| 48 |    removes the EmitterSystem from the ParticleEngine | 
|---|
| 49 | */ | 
|---|
| 50 | ParticleEmitter::~ParticleEmitter () | 
|---|
| 51 | { | 
|---|
| 52 |   this->setSystem(NULL); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | /** | 
|---|
| 56 |  *  loads a ParticleEmitter from a XML-element | 
|---|
| 57 |  * @param root the XML-element to load from | 
|---|
| 58 | */ | 
|---|
| 59 | void ParticleEmitter::loadParams(const TiXmlElement* root) | 
|---|
| 60 | { | 
|---|
| 61 |   PNode::loadParams(root); | 
|---|
| 62 |  | 
|---|
| 63 |   LoadParam(root, "rate", this, ParticleEmitter, setEmissionRate) | 
|---|
| 64 |     .describe("How many particles should be emittet from this emitter"); | 
|---|
| 65 |  | 
|---|
| 66 |   LoadParam(root, "inherit-speed", this, ParticleEmitter, setInheritSpeed) | 
|---|
| 67 |     .describe("the extent, the speed of the emitter has on the particles"); | 
|---|
| 68 |  | 
|---|
| 69 |   LoadParam(root, "emission-velocity", this, ParticleEmitter, setEmissionVelocity) | 
|---|
| 70 |     .describe("How fast the particles are emittet (their initial speed)"); | 
|---|
| 71 |  | 
|---|
| 72 |   LoadParam(root, "emission-momentum", this, ParticleEmitter, setEmissionMomentum) | 
|---|
| 73 |       .describe("How fast the particles rotation is at emissiontime (their initial momentum)"); | 
|---|
| 74 |  | 
|---|
| 75 |   LoadParam(root, "spread", this, ParticleEmitter, setSpread) | 
|---|
| 76 |     .describe("The angle the particles are emitted from (angle, deviation)"); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | void ParticleEmitter::setSystem(ParticleSystem* system) | 
|---|
| 80 | { | 
|---|
| 81 |   if (system != NULL) | 
|---|
| 82 |     system->addEmitter(this); | 
|---|
| 83 |   else if (this->system != NULL) | 
|---|
| 84 |     this->system->removeEmitter(this); | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | /** | 
|---|
| 88 |  *  this start the emitter | 
|---|
| 89 | */ | 
|---|
| 90 | void ParticleEmitter::start() {} | 
|---|
| 91 |  | 
|---|
| 92 |  | 
|---|
| 93 | /** | 
|---|
| 94 |  *  this stops the emitter | 
|---|
| 95 | */ | 
|---|
| 96 | void ParticleEmitter::stop() {} | 
|---|
| 97 |  | 
|---|
| 98 |  | 
|---|
| 99 | /** | 
|---|
| 100 |  *  set the emission rate | 
|---|
| 101 |  * @param emissionRate: sets the number of particles emitted per second | 
|---|
| 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::setEmissionRate(float emissionRate) | 
|---|
| 107 | { | 
|---|
| 108 |   if (emissionRate > 0.0) | 
|---|
| 109 |     this->emissionRate = emissionRate; | 
|---|
| 110 |   else | 
|---|
| 111 |     this->emissionRate = 0.0; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | /** | 
|---|
| 115 |  *  how much of the speed from the ParticleEmitter should flow onto the ParticleSystem | 
|---|
| 116 |  * @param value a Value between zero and one | 
|---|
| 117 |  | 
|---|
| 118 |    if you want to change the value of this variable during emission time (to make it more dynamic) | 
|---|
| 119 |    you may want to use the animation class | 
|---|
| 120 | */ | 
|---|
| 121 | void ParticleEmitter::setInheritSpeed(float value) | 
|---|
| 122 | { | 
|---|
| 123 |   if (unlikely(value > 1.0)) | 
|---|
| 124 |     this->inheritSpeed = 1; | 
|---|
| 125 |   else if (unlikely(value < 0.0)) | 
|---|
| 126 |     this->inheritSpeed = 0; | 
|---|
| 127 |   else | 
|---|
| 128 |     this->inheritSpeed = value; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | /** | 
|---|
| 132 |  *  set the angle of the emitter | 
|---|
| 133 |  * @param angle around the direction in which there are particles to be emitted | 
|---|
| 134 |  * @param randomAngle A random spread-angle, the +- randomness of this option | 
|---|
| 135 |  | 
|---|
| 136 |    if you want to change the value of this variable during emission time (to make it more dynamic) | 
|---|
| 137 |    you may want to use the animation class | 
|---|
| 138 | */ | 
|---|
| 139 | void ParticleEmitter::setSpread(float angle, float randomAngle) | 
|---|
| 140 | { | 
|---|
| 141 |   this->angle = angle; | 
|---|
| 142 |   this->randomAngle = randomAngle; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | /** | 
|---|
| 146 |  *  sets the initial velocity of all particles emitted | 
|---|
| 147 |  * @param velocity The starting velocity of the emitted particles | 
|---|
| 148 |  * @param randomVelocity A random starting velocity, the +- randomness of this option | 
|---|
| 149 |  | 
|---|
| 150 |    if you want to change the value of this variable during emission time (to make it more dynamic) | 
|---|
| 151 |    you may want to use the animation class | 
|---|
| 152 | */ | 
|---|
| 153 | void ParticleEmitter::setEmissionVelocity(float velocity, float randomVelocity) | 
|---|
| 154 | { | 
|---|
| 155 |   this->velocity = velocity; | 
|---|
| 156 |   this->randomVelocity = randomVelocity; | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | /** | 
|---|
| 160 |  *  sets the initial Momentum of all particles emitted | 
|---|
| 161 |  * @param momentum the new Momentum (just a float for being not too complicated). | 
|---|
| 162 |  * @param randomMomentum variation from the given value. | 
|---|
| 163 |  */ | 
|---|
| 164 | void ParticleEmitter::setEmissionMomentum(float momentum, float randomMomentum) | 
|---|
| 165 | { | 
|---|
| 166 |   this->momentum = momentum; | 
|---|
| 167 |   this->momentumRandom = randomMomentum; | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | /** | 
|---|
| 171 |  *  this set the time to life of a particle, after which it will die | 
|---|
| 172 |  * @param dt: the time to live in seconds | 
|---|
| 173 |  * @param system: the system into which to emitt | 
|---|
| 174 |  | 
|---|
| 175 |    if you want to change the value of this variable during emission time (to make it more dynamic) | 
|---|
| 176 |    you may want to use the animation class | 
|---|
| 177 | */ | 
|---|
| 178 | void ParticleEmitter::tick(float dt) | 
|---|
| 179 | { | 
|---|
| 180 |   assert (this->system != NULL); | 
|---|
| 181 |   if (likely(dt > 0.0 && this->emissionRate > 0.0)) | 
|---|
| 182 |   { | 
|---|
| 183 |     // saving the time (particles only partly emitted in this timestep) | 
|---|
| 184 |     float count = (dt+this->saveTime) * this->emissionRate; | 
|---|
| 185 |     this->saveTime = modff(count, &count) / this->emissionRate; | 
|---|
| 186 |     PRINTF(5)("emitting %f particles, saving %f seconds for the next timestep\n", count, this->saveTime); | 
|---|
| 187 |     if (count + this->system->getCount() > this->system->getMaxCount()) | 
|---|
| 188 |       count = this->system->getMaxCount() - this->system->getCount(); | 
|---|
| 189 |     if (likely(count > 0.0f)) | 
|---|
| 190 |       this->emitParticles((unsigned int)count); | 
|---|
| 191 |   } | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | /** | 
|---|
| 195 |  *  outputs some nice debug information | 
|---|
| 196 | */ | 
|---|
| 197 | void ParticleEmitter::debug() const | 
|---|
| 198 | { | 
|---|
| 199 |   PRINT(0)(" ParticleEmitter %s::%s\n", this->getClassCName(), this->getCName()); | 
|---|
| 200 |   PRINT(0)("  EmissionRate: %f, Speed: %f, SpreadAngle: %f\n", this->getEmissionRate(), this->getEmissionVelocity(), this->getSpread()); | 
|---|
| 201 | } | 
|---|