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_PARTICLE |
---|
17 | |
---|
18 | #include "particle_emitter.h" |
---|
19 | |
---|
20 | #include "particle_system.h" |
---|
21 | #include "particle_engine.h" |
---|
22 | |
---|
23 | #include "debug.h" |
---|
24 | #include "stdlibincl.h" |
---|
25 | |
---|
26 | using namespace std; |
---|
27 | |
---|
28 | |
---|
29 | /** |
---|
30 | \brief standard constructor |
---|
31 | */ |
---|
32 | ParticleEmitter::ParticleEmitter(const Vector& direction, float angle, float emissionRate, |
---|
33 | float velocity) |
---|
34 | { |
---|
35 | this->setClassID(CL_PARTICLE_EMITTER, "ParticleEmitter"); |
---|
36 | |
---|
37 | this->type = EMITTER_DOT; |
---|
38 | this->emitterSize = 1.0; |
---|
39 | this->direction = direction; |
---|
40 | this->setSpread(angle); |
---|
41 | this->setEmissionRate(emissionRate); |
---|
42 | this->setEmissionVelocity(velocity); |
---|
43 | |
---|
44 | this->saveTime = 0.0; |
---|
45 | |
---|
46 | ParticleEngine::getInstance()->addEmitter(this); |
---|
47 | } |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | /** |
---|
52 | \brief standard destructor |
---|
53 | |
---|
54 | */ |
---|
55 | ParticleEmitter::~ParticleEmitter () |
---|
56 | { |
---|
57 | ParticleEngine::getInstance()->removeEmitter(this); |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | /** |
---|
62 | \brief this start the emitter |
---|
63 | */ |
---|
64 | void ParticleEmitter::start() {} |
---|
65 | |
---|
66 | |
---|
67 | /** |
---|
68 | \brief this stops the emitter |
---|
69 | */ |
---|
70 | void ParticleEmitter::stop() {} |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | /* these are Animation interfaces: so you can change spec values as you want */ |
---|
76 | |
---|
77 | /** |
---|
78 | \param type the new Type of this emitter |
---|
79 | */ |
---|
80 | void ParticleEmitter::setType(EMITTER_TYPE type) |
---|
81 | { |
---|
82 | this->type = type; |
---|
83 | } |
---|
84 | |
---|
85 | void ParticleEmitter::setSize(float emitterSize) |
---|
86 | { |
---|
87 | if (emitterSize > 0.0) |
---|
88 | this->emitterSize = emitterSize; |
---|
89 | else |
---|
90 | emitterSize = 0.0; |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | \brief set the emission rate |
---|
95 | \param sets the number of particles emitted per second |
---|
96 | |
---|
97 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
98 | you may want to use the animation class |
---|
99 | */ |
---|
100 | void ParticleEmitter::setEmissionRate(float emissionRate) |
---|
101 | { |
---|
102 | if (emissionRate > 0.0) |
---|
103 | this->emissionRate = emissionRate; |
---|
104 | else |
---|
105 | this->emissionRate = 0.0; |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | \brief set the angle of the emitter |
---|
110 | \param angle around the direction in which there are particles to be emitted |
---|
111 | \param randomAngle A random spread-angle, the +- randomness of this option |
---|
112 | |
---|
113 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
114 | you may want to use the animation class |
---|
115 | */ |
---|
116 | void ParticleEmitter::setSpread(float angle, float randomAngle) |
---|
117 | { |
---|
118 | this->angle = angle; |
---|
119 | this->randomAngle = randomAngle; |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | \brief sets the velocity of all particles emitted |
---|
124 | \param velocity The starting velocity of the emitted particles |
---|
125 | \param random A random starting velocity, the +- randomness of this option |
---|
126 | |
---|
127 | if you want to change the value of this variable during emission time (to make it more dynamic) |
---|
128 | you may want to use the animation class |
---|
129 | */ |
---|
130 | void ParticleEmitter::setEmissionVelocity(float velocity, float randomVelocity) |
---|
131 | { |
---|
132 | this->velocity = velocity; |
---|
133 | this->randomVelocity = randomVelocity; |
---|
134 | } |
---|
135 | |
---|
136 | /** |
---|
137 | \brief this set the time to life of a particle, after which it will die |
---|
138 | \param the time to live in seconds |
---|
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 | |
---|
144 | void ParticleEmitter::tick(float dt, ParticleSystem* system) |
---|
145 | { |
---|
146 | if (likely(dt > 0.0 && this->emissionRate > 0.0)) |
---|
147 | { |
---|
148 | // saving the time |
---|
149 | float count = (dt+this->saveTime) * this->emissionRate; |
---|
150 | this->saveTime = modff(count, &count) / this->emissionRate; |
---|
151 | PRINTF(5)("emitting %f particles, saving %f seconds for the next round\n", count, this->saveTime); |
---|
152 | |
---|
153 | if (likely(count > 0)) |
---|
154 | { |
---|
155 | Vector inheritVelocity = this->getVelocity() * system->inheritSpeed; |
---|
156 | for (int i = 0; i < count; i++) |
---|
157 | // emmits from EMITTER_DOT, |
---|
158 | { |
---|
159 | Vector randDir = Vector(rand()-RAND_MAX/2, rand()-RAND_MAX/2, rand()-RAND_MAX/2); |
---|
160 | randDir.normalize(); |
---|
161 | randDir = (this->getAbsDir()*Quaternion(angle + randomAngle *((float)rand()/RAND_MAX -.5), randDir)).apply(this->direction); |
---|
162 | Vector velocityV = randDir.getNormalized()*this->velocity + inheritVelocity; |
---|
163 | |
---|
164 | // this should spread the Particles evenly. if the Emitter is moved around quickly |
---|
165 | Vector equalSpread = this->getVelocity() * rand()/RAND_MAX * dt; |
---|
166 | Vector extension; // the Vector for different fields. |
---|
167 | |
---|
168 | if (this->type & 2) |
---|
169 | { |
---|
170 | extension = Vector(this->emitterSize * ((float)rand()/RAND_MAX -.5), 0, this->emitterSize * ((float)rand()/RAND_MAX - .5)); |
---|
171 | extension = this->getAbsDir().apply(extension); |
---|
172 | } |
---|
173 | else if (this->type & 8) |
---|
174 | { |
---|
175 | extension = Vector((float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5, (float)rand()/RAND_MAX -.5) * this->emitterSize; |
---|
176 | } |
---|
177 | |
---|
178 | system->addParticle(this->getAbsCoor() + extension - equalSpread, velocityV); |
---|
179 | |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | \brief outputs some nice debug information |
---|
187 | */ |
---|
188 | void ParticleEmitter::debug(void) |
---|
189 | { |
---|
190 | |
---|
191 | } |
---|