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