Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/particles/particle_system.cc

Last change on this file was 10658, checked in by bknecht, 17 years ago

changes from landauf

File size: 15.0 KB
RevLine 
[4597]1/*
[1853]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.
[1855]10
11   ### File Specific:
[3925]12   main-programmer: Benjamin Grauer
[1855]13   co-programmer: ...
[1853]14*/
15
[7301]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
[1853]17
[3925]18#include "particle_system.h"
[1853]19
[3930]20#include "particle_emitter.h"
[4338]21
[9815]22#include "physics/fields/field.h"
[5511]23#include "model.h"
[4338]24
[9727]25#include "util/loading/load_param_xml.h"
[7193]26#include "util/loading/factory.h"
[3942]27#include "material.h"
[4338]28#include "state.h"
[5446]29#include "shell_command.h"
[3930]30
[6612]31#include <algorithm>
[4338]32
[9715]33ObjectListDefinition(ParticleSystem);
[9406]34
[3245]35/**
[4836]36 *  standard constructor
37 * @param maxCount the Count of particles in the System
38 * @param type The Type of the ParticleSystem
[3245]39*/
[6621]40ParticleSystem::ParticleSystem (unsigned int maxCount)
[3365]41{
[9686]42  this->registerObject(this, ParticleSystem::_objectList);
[4597]43
[7300]44  this->setMaxCount(PARTICLE_DEFAULT_MAX_COUNT);
45  this->count = 0;
46  this->particles = NULL;
47  this->deadList = NULL;
48  this->conserve = 1.0;
49  this->lifeSpan = 1.0; this->randomLifeSpan = 0.0;
50
51  this->toList(OM_ENVIRON);
52
53  this->maxCount = maxCount;
[3365]54}
[1853]55
[4677]56/**
[9656]57 * @brief standard deconstructor
58 */
[4176]59ParticleSystem::~ParticleSystem()
[3543]60{
[6625]61  // deleting all the living Particles
62  while (this->particles)
63  {
64    Particle* tmpDelPart = this->particles;
65    this->particles = this->particles->next;
66    delete tmpDelPart;
67  }
[4123]68
[6625]69  // deleting all the dead particles
70  while (this->deadList)
71  {
72    Particle* tmpDelPart = this->deadList;
73    this->deadList = this->deadList->next;
74    delete tmpDelPart;
75  }
[4176]76
[6625]77  while(!this->emitters.empty())
78  {
79    this->removeEmitter(this->emitters.front());
80  }
[6623]81
[3543]82}
[1853]83
[4602]84
85/**
[9656]86 * @brief loads Parameters from a TiXmlElement
[4602]87 * @param root the XML-element to load from.
88 */
89void ParticleSystem::loadParams(const TiXmlElement* root)
90{
[6512]91  WorldEntity::loadParams(root);
92  PhysicsInterface::loadParams(root);
[4602]93
[5671]94  LoadParam(root, "max-count", this, ParticleSystem, setMaxCount)
[6625]95  .describe("the maximal count of Particles, that can be emitted into this system");
[4727]96
[5671]97  LoadParam(root, "life-span", this, ParticleSystem, setLifeSpan)
[6625]98  .describe("sets the life-span of the Particles.");
[4602]99
[5671]100  LoadParam(root, "conserve", this, ParticleSystem, setConserve)
[6625]101  .describe("sets the Conserve factor of the Particles (1.0: they keep all their energy, 0.0:they keep no energy)");
[4725]102
[6623]103  LoadParamXML(root, "emitters", this, ParticleSystem, loadEmitters);
[6222]104
[5654]105  LOAD_PARAM_START_CYCLE(root, element);
[4727]106  {
[5654]107    element->ToText();
[6625]108    // PER-PARTICLE-ATTRIBUTES:
[5654]109    LoadParam_CYCLE(element, "radius", this, ParticleSystem, setRadius)
[6625]110    .describe("The Radius of each particle over time (TimeIndex [0-1], radius at TimeIndex, randomRadius at TimeIndex)");
[4725]111
[5654]112    LoadParam_CYCLE(element, "mass", this, ParticleSystem, setMass)
[6625]113    .describe("The Mass of each particle over time (TimeIndex: [0-1], mass at TimeIndex, randomMass at TimeIndex)");
[4725]114
[5654]115    LoadParam_CYCLE(element, "color", this, ParticleSystem, setColor)
[6625]116    .describe("The Color of each particle over time (TimeIndex: [0-1], red: [0-1], green: [0-1], blue: [0-1], alpha: [0-1])");
[4727]117  }
[5654]118  LOAD_PARAM_END_CYCLE(element);
[6629]119
120  LoadParam(root, "precache", this, ParticleSystem, precache)
[7300]121  .describe("Precaches the ParticleSystem for %1 seconds, %2 times per Second")
122  .defaultValues(1.0, 25.0);
[4602]123}
[4727]124
[4725]125/**
[6623]126 * @brief loads the Emitters from An XML-Root
127 * @param root the XML-Element to load all emitters from
128 */
129void ParticleSystem::loadEmitters(const TiXmlElement* root)
130{
131  LOAD_PARAM_START_CYCLE(root, element);
132  {
133    BaseObject* emitter = Factory::fabricate(element);
[6823]134    if (emitter != NULL)
135    {
[9757]136      if (emitter->isA(ParticleEmitter::staticClassID()))
[10515]137      {
[6823]138        this->addEmitter(dynamic_cast<ParticleEmitter*>(emitter));
[10515]139      }
[6823]140      else
141      {
142        PRINTF(2)("Tried to load an Element of type '%s' that should be a ParticleEmitter onto '%s::%s'.\n",
[9406]143                  emitter->getClassCName(), this->getClassCName(), this->getCName());
[6823]144        delete emitter;
145      }
146    }
[6623]147    else
148    {
[9406]149      PRINTF(2)("Could not Generate Emitter for system %s::%s (wrong type in XML-format)\n", this->getClassCName(), getCName());
[6623]150    }
151  }
152  LOAD_PARAM_END_CYCLE(element);
153}
154
155/**
[7300]156 * @param maxCount the maximum count of particles that can be emitted
[4727]157 */
[7300]158void ParticleSystem::setMaxCount(unsigned int maxCount)
[4727]159{
160  this->maxCount = maxCount;
[9406]161  PRINTF(4)("MAXCOUNT of %s::%s is %d\n", this->getClassCName(), this->getCName(), maxCount);
[4727]162}
163
[3932]164// setting properties
[4478]165/**
[7300]166 * @brief Sets the lifespan of newly created particles
167 * @param lifeSpan the LifeSpan of each particle in the System
168 * @param randomLifeSpan the Deviation from lifeSpan (random Value).
[4597]169*/
[3932]170void ParticleSystem::setLifeSpan(float lifeSpan, float randomLifeSpan)
171{
[3934]172  this->lifeSpan = lifeSpan;
173  this->randomLifeSpan = randomLifeSpan;
[9406]174  PRINTF(4)("LifeTime of %s::%s is %f\n", this->getClassCName(), this->getCName(), lifeSpan);
[3932]175}
176
[3945]177/**
[7300]178 * @brief sets the conserve Factor of newly created particles
179 * @param conserve sets the conserve factor of each particle.
180 * Conserve is the ammount of energy a particle takes from the last Frame into the next.
181 * A Value of 1 means, that all energy is conserved, a Value of 0 means infinit friction.
182 */
[4430]183void ParticleSystem::setConserve(float conserve)
[3932]184{
[4430]185  if (conserve > 1.0)
186    this->conserve = 1.0;
187  else if (conserve < 0.0)
188    this->conserve = 0.0;
189  else
190    this->conserve = conserve;
[7300]191
[9406]192  PRINTF(4)("Conserve of %s::%s is %f\n", this->getClassCName(), this->getCName(), conserve);
[3932]193}
194
[4430]195/////////////////////////////
196/* Per-Particle Attributes */
197/////////////////////////////
[3945]198/**
[7300]199 * @brief sets a key in the radius-animation on a per-particle basis
[4836]200 * @param lifeCycleTime the time (partilceLifeTime/particleAge) [0-1]
201 * @param radius the radius at this position
202 * @param randRadius the randRadius at this position
[4378]203*/
[4430]204void ParticleSystem::setRadius(float lifeCycleTime, float radius, float randRadius)
[4378]205{
[7333]206  this->radiusAnim.changeValue(lifeCycleTime, radius);
207  this->randRadiusAnim.changeValue(lifeCycleTime, randRadius);
[7300]208
209  PRINTF(4)("Radius of %s::%s at timeSlice %f is %f with a Random of %f\n",
[9406]210    this->getClassCName(), this->getCName(), lifeCycleTime, radius, randRadius);
[4378]211}
212
213/**
[7300]214 * @brief sets a key in the mass-animation on a per-particle basis
[4836]215 * @param lifeCycleTime the time (partilceLifeTime/particleAge) [0-1]
216 * @param mass the mass at this position
217 * @param randMass the randomMass at this position
[3945]218*/
[4430]219void ParticleSystem::setMass(float lifeCycleTime, float mass, float randMass)
[3932]220{
[7333]221  this->massAnim.changeValue(lifeCycleTime, mass);
222  this->randMassAnim.changeValue(lifeCycleTime, randMass);
[3932]223}
224
[3945]225/**
[7300]226 * @brief sets a key in the color-animation on a per-particle basis
[4836]227 * @param lifeCycleTime: the time (partilceLifeTime/particleAge) [0-1]
228 * @param red: red
229 * @param green: green
230 * @param blue: blue
231 * @param alpha: alpha
[4338]232*/
[4725]233void ParticleSystem::setColor(float lifeCycleTime, float red, float green, float blue, float alpha)
[4338]234{
[7333]235  this->colorAnim[0].changeValue(lifeCycleTime, red);
236  this->colorAnim[1].changeValue(lifeCycleTime, green);
237  this->colorAnim[2].changeValue(lifeCycleTime, blue);
238  this->colorAnim[3].changeValue(lifeCycleTime, alpha);
[7300]239
240  PRINTF(4)("Color of %s::%s on timeslice %f is r:%f g:%f b:%f a:%f\n",
[9406]241    this->getClassCName(), this->getCName(), lifeCycleTime, red, green, blue, alpha);
[4338]242}
243
[6629]244/**
[9656]245 * @brief sets a key in the color-animation on a per-particle basis
246 * @param lifeCycleTime: the time (partilceLifeTime/particleAge) [0-1]
247 * @param color the Color.
248 */
249void ParticleSystem::setColor(float lifeCycleTime, const Color& color)
250{
251  this->setColor(lifeCycleTime, color.r(), color.g(), color.b(), color.a());
252}
253
254
255/**
[6629]256 * @brief adds an Emitter to this System.
257 * @param emitter the Emitter to add.
258 */
[6612]259void ParticleSystem::addEmitter(ParticleEmitter* emitter)
260{
[6619]261  assert (emitter != NULL);
262  if (emitter->getSystem() != NULL)
263    emitter->getSystem()->removeEmitter(emitter);
[6625]264  emitter->system = this;
[6612]265  this->emitters.push_back(emitter);
[10519]266
[6612]267}
268
[6629]269/**
270 * @brief removes a ParticleEmitter from this System
271 * @param emitter the Emitter to remove
272 */
[6612]273void ParticleSystem::removeEmitter(ParticleEmitter* emitter)
274{
[6625]275  assert (emitter != NULL);
276  emitter->system = NULL;
277  this->emitters.remove(emitter);
278  /*  std::list<ParticleEmitter*>::iterator it = std::find(this->emitters.begin(), this->emitters.end(), emitter);
[6612]279  if (it != this->emitters.end())
[6625]280    this->emitters.erase(it);*/
[6612]281}
282
[10523]283
[4338]284/**
[10523]285 * attackes
286 * @param node
287 */
[10528]288void ParticleSystem::attachEmmittersTo(PNode* node, const Vector& offset, const Quaternion& dir)
[10523]289{
290  std::list<ParticleEmitter*>::iterator it = this->emitters.begin();
291  for( ; it != this->emitters.end(); it++)
292  {
293    //
294    (*it)->setParent(node);
295    (*it)->setRelCoor(offset);
[10528]296    (*it)->setRelDir(dir);
[10523]297  }
298}
299
300
301
302/**
[6629]303 * @brief does a Precaching, meaning, that the ParticleSystem(and its emitters) will be ticked force
304 * @param seconds: seconds
305 * @param ticksPerSeconds times per Second.
306 */
307void ParticleSystem::precache(unsigned int seconds, unsigned int ticksPerSecond)
308{
[6630]309  std::list<ParticleEmitter*>::iterator emitter;
310  for (emitter = this->emitters.begin(); emitter != this->emitters.end(); emitter++)
311    (*emitter)->updateNode(.1), (*emitter)->updateNode(.1);
312
[9406]313  PRINTF(4)("Precaching %s::%s %d seconds %d timesPerSecond\n", this->getClassCName(), this->getCName(), seconds, ticksPerSecond);
[6629]314  for (unsigned int i = 0; i < seconds*ticksPerSecond; i++)
315    this->tick(1.0/(float)ticksPerSecond);
316}
317
318
319/**
[7300]320 * @brief ticks the system.
[4836]321 * @param dt the time to tick all the Particles of the System
[3932]322
[3945]323   this is used to get all the particles some motion
324*/
[3931]325void ParticleSystem::tick(float dt)
326{
[3934]327  Particle* tickPart = particles;  // the particle to Tick
[4692]328  Particle* prevPart = NULL;
[3934]329  while (likely(tickPart != NULL))
[6625]330  {
331    // applying force to the System.
332    if (likely (tickPart->mass > 0.0))
333      tickPart->velocity += tickPart->extForce / tickPart->mass * dt;
[4687]334
[6625]335    tickPart->radius = radiusAnim.getValue(tickPart->lifeCycle)
336                       + randRadiusAnim.getValue(tickPart->lifeCycle) * tickPart->radiusRand;
[4434]337
[6625]338    tickPart->mass = massAnim.getValue(tickPart->lifeCycle)
339                     + randMassAnim.getValue(tickPart->lifeCycle) * tickPart->massRand;
[4597]340
[10658]341    //tickPart->extForce = Vector(0,0,0);
[4338]342
[6625]343    // applying Color
[7333]344    this->colorAnim[0].getValue(tickPart->color[0], tickPart->lifeCycle);
345    this->colorAnim[1].getValue(tickPart->color[1], tickPart->lifeCycle);
346    this->colorAnim[2].getValue(tickPart->color[2], tickPart->lifeCycle);
347    this->colorAnim[3].getValue(tickPart->color[3], tickPart->lifeCycle);
[4431]348
[6757]349    // rendering new position.
350    tickPart->position += tickPart->velocity * dt;
[7027]351    tickPart->orientation *= tickPart->momentum *dt;
[6757]352
[6625]353    // many more to come
[3932]354
[6625]355    if (this->conserve < 1.0)
356    {
357      tickPart->velocity *= this->conserve;
358      tickPart->momentum *= this->conserve;
359    }
360    // find out if we have to delete tickPart
361    if (unlikely((tickPart->lifeCycle += dt/tickPart->lifeTime) >= 1.0))
362    {
363      // remove the particle from the list
364      if (likely(prevPart != NULL))
[4691]365      {
[6625]366        prevPart->next = tickPart->next;
367        tickPart->next = this->deadList;
368        this->deadList = tickPart;
369        tickPart = prevPart->next;
[4691]370      }
[3934]371      else
[6625]372      {
373        prevPart = NULL;
374        this->particles = tickPart->next;
375        tickPart->next = this->deadList;
376        this->deadList = tickPart;
377        tickPart = this->particles;
378      }
379      --this->count;
[3932]380    }
[6625]381    else
382    {
383      prevPart = tickPart;
384      tickPart = tickPart->next;
385    }
386  }
[6612]387
[6625]388  std::list<ParticleEmitter*>::iterator emitter;
389  for (emitter = this->emitters.begin(); emitter != this->emitters.end(); emitter++)
390    (*emitter)->tick(dt);
[3932]391}
392
[4597]393/**
[4836]394  *  applies some force to a Particle.
395  * @param field the Field to apply.
[4338]396 */
[7300]397void ParticleSystem::applyField(const Field* field)
[4338]398{
399  Particle* tickPart = particles;
400  while (tickPart)
[6625]401  {
402    tickPart->extForce += field->calcForce(tickPart->position);
403    tickPart = tickPart->next;
404  }
[4338]405}
406
407
[3945]408/**
[4836]409 * @returns the count of Faces of this ParticleSystem
[4677]410 */
[4746]411unsigned int ParticleSystem::getFaceCount() const
[4677]412{
[6621]413  return this->count;
[4677]414}
415
416/**
[7300]417 * @brief adds a new Particle to the System
[4836]418 * @param position the initial position, where the particle gets emitted.
419 * @param velocity the initial velocity of the particle.
420 * @param orientation the initial orientation of the Paritcle.
421 * @param momentum the initial momentum of the Particle (the speed of its rotation).
422 * @param data some more data given by the emitter
[3945]423*/
[10658]424void ParticleSystem::addParticle(const Vector& position, const Vector& velocity, const Quaternion& orientation, const Quaternion& momentum, const Vector& extForce, unsigned int data)
[3932]425{
[3934]426  if (this->count <= this->maxCount)
[6625]427  {
428    // if it is the first Particle
429    if (unlikely(particles == NULL))
[3932]430    {
[6625]431      if (likely(deadList != NULL))
432      {
433        this->particles = this->deadList;
434        deadList = deadList->next;
435      }
[3934]436      else
[6625]437      {
438        PRINTF(5)("Generating new Particle\n");
439        this->particles = new Particle;
440      }
441      this->particles->next = NULL;
442    }
443    // filling the List from the beginning
444    else
445    {
446      Particle* tmpPart;
447      if (likely(deadList != NULL))
448      {
449        tmpPart = this->deadList;
450        deadList = deadList->next;
451      }
452      else
453      {
454        PRINTF(5)("Generating new Particle\n");
455        tmpPart = new Particle;
456      }
457      tmpPart->next = this->particles;
458      this->particles = tmpPart;
459    }
460    particles->lifeTime = this->lifeSpan + (float)(rand()/RAND_MAX)* this->randomLifeSpan;
461    particles->lifeCycle = 0.0;
462    particles->position = position;
463    particles->velocity = velocity;
[10658]464    particles->extForce = extForce;
[3951]465
[6625]466    particles->orientation = orientation;
467    particles->momentum = momentum;
[4687]468
[6625]469    //  particle->rotation = ; //! @todo rotation is once again something to be done.
470    particles->massRand = 2*(float)rand()/RAND_MAX -1;
471    particles->radiusRand = 2* (float)rand()/RAND_MAX -1;
472    particles->mass = this->massAnim.getValue(0.0) + this->randMassAnim.getValue(0.0)*particles->massRand;
473    particles->radius = this->radiusAnim.getValue(0.0) + this->randRadiusAnim.getValue(0.0)*particles->radiusRand;
[3934]474
[6625]475    ++this->count;
476  }
[3932]477  else
[7300]478    PRINTF(4)("maximum count of particles reached not adding any more\n");
[3932]479}
[3931]480
[3944]481/**
[4836]482 *  outputs some nice debug information
[3944]483*/
[4746]484void ParticleSystem::debug() const
[3944]485{
[7300]486  PRINT(0)("  ParticleCount: %d emitters: %d, maximumCount: %d :: filled %d%%\n",
487           this->count,
488           this->emitters.size(),
489           this->maxCount,
490           ((this->maxCount!=0)?100*this->count/this->maxCount:0));
[9656]491
492
493  PRINT(0)("  Coloring sceme: r:"), this->colorAnim[0].debug();
494  PRINT(0)("  Coloring sceme: g:"), this->colorAnim[1].debug();
495  PRINT(0)("  Coloring sceme: b:"), this->colorAnim[2].debug();
496  PRINT(0)("  Coloring sceme: a:"), this->colorAnim[3].debug();
497
498  if (likely(this->deadList != NULL))
[6625]499  {
500    PRINT(0)("  - ParticleDeadList is used: ");
501    int i = 1;
502    Particle* tmpPart = this->deadList;
[9406]503    while ((tmpPart = tmpPart->next) != NULL) { ++i; }
[6625]504    PRINT(0)("count: %d\n", i);
505  }
[3944]506}
Note: See TracBrowser for help on using the repository browser.