Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/physics/src/lib/graphics/particles/particle_emitter.cc @ 4307

Last change on this file since 4307 was 4307, checked in by bensch, 19 years ago

orxonox/branches/physics: new definitions of particle-return-values

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