Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/particles/particle_emitter.cc @ 6822

Last change on this file since 6822 was 6822, checked in by bensch, 18 years ago

trunk: ParticleEmitters now splitted into SubClasses.
Also fixed a little Boeg in the ClassID

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