[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 | |
---|
[7193] | 22 | #include "util/loading/load_param.h" |
---|
[4381] | 23 | #include "debug.h" |
---|
| 24 | |
---|
[9715] | 25 | ObjectListDefinition(ParticleEmitter); |
---|
[3926] | 26 | |
---|
| 27 | /** |
---|
[4836] | 28 | * standard constructor |
---|
[3926] | 29 | */ |
---|
[6825] | 30 | ParticleEmitter::ParticleEmitter(float emissionRate, float velocity, float angle) |
---|
[3926] | 31 | { |
---|
[9686] | 32 | this->registerObject(this, ParticleEmitter::_objectList); |
---|
[4726] | 33 | |
---|
[6822] | 34 | this->system = NULL; |
---|
| 35 | |
---|
| 36 | this->setInheritSpeed(PARTICLE_EMITTER_DEFAULT_INHERIT_SPEED); |
---|
| 37 | this->setEmissionMomentum(0); |
---|
[4439] | 38 | this->setSpread(angle); |
---|
| 39 | this->setEmissionRate(emissionRate); |
---|
| 40 | this->setEmissionVelocity(velocity); |
---|
[4437] | 41 | |
---|
[6822] | 42 | this->saveTime = 0.0; |
---|
[3926] | 43 | } |
---|
| 44 | |
---|
| 45 | /** |
---|
[4836] | 46 | * standard destructor |
---|
[3926] | 47 | |
---|
[4496] | 48 | removes the EmitterSystem from the ParticleEngine |
---|
[3926] | 49 | */ |
---|
[4597] | 50 | ParticleEmitter::~ParticleEmitter () |
---|
[3935] | 51 | { |
---|
[6623] | 52 | this->setSystem(NULL); |
---|
[3935] | 53 | } |
---|
[3929] | 54 | |
---|
[4478] | 55 | /** |
---|
[4836] | 56 | * loads a ParticleEmitter from a XML-element |
---|
| 57 | * @param root the XML-element to load from |
---|
[4478] | 58 | */ |
---|
[4437] | 59 | void ParticleEmitter::loadParams(const TiXmlElement* root) |
---|
| 60 | { |
---|
[6512] | 61 | PNode::loadParams(root); |
---|
[3929] | 62 | |
---|
[5671] | 63 | LoadParam(root, "rate", this, ParticleEmitter, setEmissionRate) |
---|
[4437] | 64 | .describe("How many particles should be emittet from this emitter"); |
---|
| 65 | |
---|
[5671] | 66 | LoadParam(root, "inherit-speed", this, ParticleEmitter, setInheritSpeed) |
---|
[4494] | 67 | .describe("the extent, the speed of the emitter has on the particles"); |
---|
| 68 | |
---|
[5671] | 69 | LoadParam(root, "emission-velocity", this, ParticleEmitter, setEmissionVelocity) |
---|
[4437] | 70 | .describe("How fast the particles are emittet (their initial speed)"); |
---|
[4597] | 71 | |
---|
[5671] | 72 | LoadParam(root, "emission-momentum", this, ParticleEmitter, setEmissionMomentum) |
---|
[4725] | 73 | .describe("How fast the particles rotation is at emissiontime (their initial momentum)"); |
---|
| 74 | |
---|
[5671] | 75 | LoadParam(root, "spread", this, ParticleEmitter, setSpread) |
---|
[4437] | 76 | .describe("The angle the particles are emitted from (angle, deviation)"); |
---|
| 77 | } |
---|
| 78 | |
---|
[6619] | 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 | |
---|
[3929] | 87 | /** |
---|
[4836] | 88 | * this start the emitter |
---|
[3929] | 89 | */ |
---|
| 90 | void ParticleEmitter::start() {} |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | /** |
---|
[4836] | 94 | * this stops the emitter |
---|
[3929] | 95 | */ |
---|
| 96 | void ParticleEmitter::stop() {} |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | /** |
---|
[4836] | 100 | * set the emission rate |
---|
| 101 | * @param emissionRate: sets the number of particles emitted per second |
---|
[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 | */ |
---|
[3931] | 106 | void ParticleEmitter::setEmissionRate(float emissionRate) |
---|
[3933] | 107 | { |
---|
[4338] | 108 | if (emissionRate > 0.0) |
---|
| 109 | this->emissionRate = emissionRate; |
---|
| 110 | else |
---|
| 111 | this->emissionRate = 0.0; |
---|
[3933] | 112 | } |
---|
[3929] | 113 | |
---|
| 114 | /** |
---|
[4836] | 115 | * how much of the speed from the ParticleEmitter should flow onto the ParticleSystem |
---|
| 116 | * @param value a Value between zero and one |
---|
[4493] | 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 | /** |
---|
[4836] | 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 |
---|
[3929] | 135 | |
---|
| 136 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
[3930] | 137 | you may want to use the animation class |
---|
[3929] | 138 | */ |
---|
[3931] | 139 | void ParticleEmitter::setSpread(float angle, float randomAngle) |
---|
[3935] | 140 | { |
---|
| 141 | this->angle = angle; |
---|
| 142 | this->randomAngle = randomAngle; |
---|
| 143 | } |
---|
[3929] | 144 | |
---|
| 145 | /** |
---|
[4836] | 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 |
---|
[3929] | 149 | |
---|
| 150 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
[3930] | 151 | you may want to use the animation class |
---|
[3929] | 152 | */ |
---|
[4338] | 153 | void ParticleEmitter::setEmissionVelocity(float velocity, float randomVelocity) |
---|
[3935] | 154 | { |
---|
| 155 | this->velocity = velocity; |
---|
| 156 | this->randomVelocity = randomVelocity; |
---|
| 157 | } |
---|
[3929] | 158 | |
---|
| 159 | /** |
---|
[4836] | 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. |
---|
[4690] | 163 | */ |
---|
| 164 | void ParticleEmitter::setEmissionMomentum(float momentum, float randomMomentum) |
---|
| 165 | { |
---|
| 166 | this->momentum = momentum; |
---|
| 167 | this->momentumRandom = randomMomentum; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | /** |
---|
[4836] | 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 |
---|
[3929] | 174 | |
---|
| 175 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
[3930] | 176 | you may want to use the animation class |
---|
[3929] | 177 | */ |
---|
[6620] | 178 | void ParticleEmitter::tick(float dt) |
---|
[3932] | 179 | { |
---|
[6620] | 180 | assert (this->system != NULL); |
---|
[3950] | 181 | if (likely(dt > 0.0 && this->emissionRate > 0.0)) |
---|
| 182 | { |
---|
[4692] | 183 | // saving the time (particles only partly emitted in this timestep) |
---|
[3950] | 184 | float count = (dt+this->saveTime) * this->emissionRate; |
---|
[4017] | 185 | this->saveTime = modff(count, &count) / this->emissionRate; |
---|
[4692] | 186 | PRINTF(5)("emitting %f particles, saving %f seconds for the next timestep\n", count, this->saveTime); |
---|
[7027] | 187 | if (count + this->system->getCount() > this->system->getMaxCount()) |
---|
| 188 | count = this->system->getMaxCount() - this->system->getCount(); |
---|
[6822] | 189 | if (likely(count > 0.0f)) |
---|
| 190 | this->emitParticles((unsigned int)count); |
---|
[3950] | 191 | } |
---|
[3932] | 192 | } |
---|
[3944] | 193 | |
---|
| 194 | /** |
---|
[4836] | 195 | * outputs some nice debug information |
---|
[3944] | 196 | */ |
---|
[4746] | 197 | void ParticleEmitter::debug() const |
---|
[3944] | 198 | { |
---|
[9406] | 199 | PRINT(0)(" ParticleEmitter %s::%s\n", this->getClassCName(), this->getCName()); |
---|
[4639] | 200 | PRINT(0)(" EmissionRate: %f, Speed: %f, SpreadAngle: %f\n", this->getEmissionRate(), this->getEmissionVelocity(), this->getSpread()); |
---|
[3944] | 201 | } |
---|