Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

changes from landauf

File size: 6.2 KB
RevLine 
[4597]1/*
[3926]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:
[3938]12   main-programmer: Benjamin Grauer
[3926]13   co-programmer: Patrick Boenzli
14*/
15
[5357]16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GRAPHICS
[3926]17
18#include "particle_emitter.h"
19
[3932]20#include "particle_system.h"
21
[7193]22#include "util/loading/load_param.h"
[4381]23#include "debug.h"
24
[9715]25ObjectListDefinition(ParticleEmitter);
[3926]26
27/**
[4836]28 *  standard constructor
[3926]29*/
[6825]30ParticleEmitter::ParticleEmitter(float emissionRate, float velocity, float angle)
[3926]31{
[9686]32  this->registerObject(this, ParticleEmitter::_objectList);
[4726]33
[6822]34  this->system = NULL;
35
36  this->setInheritSpeed(PARTICLE_EMITTER_DEFAULT_INHERIT_SPEED);
37  this->setEmissionMomentum(0);
[4439]38  this->setSpread(angle);
39  this->setEmissionRate(emissionRate);
40  this->setEmissionVelocity(velocity);
[10658]41  this->setExtForce(0, 0, 0);
[4437]42
[6822]43  this->saveTime = 0.0;
[3926]44}
45
46/**
[4836]47 *  standard destructor
[3926]48
[4496]49   removes the EmitterSystem from the ParticleEngine
[3926]50*/
[4597]51ParticleEmitter::~ParticleEmitter ()
[3935]52{
[6623]53  this->setSystem(NULL);
[3935]54}
[3929]55
[4478]56/**
[4836]57 *  loads a ParticleEmitter from a XML-element
58 * @param root the XML-element to load from
[4478]59*/
[4437]60void ParticleEmitter::loadParams(const TiXmlElement* root)
61{
[6512]62  PNode::loadParams(root);
[3929]63
[5671]64  LoadParam(root, "rate", this, ParticleEmitter, setEmissionRate)
[4437]65    .describe("How many particles should be emittet from this emitter");
66
[5671]67  LoadParam(root, "inherit-speed", this, ParticleEmitter, setInheritSpeed)
[4494]68    .describe("the extent, the speed of the emitter has on the particles");
69
[5671]70  LoadParam(root, "emission-velocity", this, ParticleEmitter, setEmissionVelocity)
[4437]71    .describe("How fast the particles are emittet (their initial speed)");
[4597]72
[10658]73  LoadParam(root, "ext-force", this, ParticleEmitter, setExtForce)
74      .describe("The external force that affects the particles.");
75
[5671]76  LoadParam(root, "emission-momentum", this, ParticleEmitter, setEmissionMomentum)
[4725]77      .describe("How fast the particles rotation is at emissiontime (their initial momentum)");
78
[5671]79  LoadParam(root, "spread", this, ParticleEmitter, setSpread)
[4437]80    .describe("The angle the particles are emitted from (angle, deviation)");
81}
82
[6619]83void 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
[3929]91/**
[4836]92 *  this start the emitter
[3929]93*/
94void ParticleEmitter::start() {}
95
96
97/**
[4836]98 *  this stops the emitter
[3929]99*/
100void ParticleEmitter::stop() {}
101
102
103/**
[4836]104 *  set the emission rate
105 * @param emissionRate: sets the number of particles emitted per second
[3929]106
107   if you want to change the value of this variable during emission time (to make it more dynamic)
[3930]108   you may want to use the animation class
[3929]109*/
[3931]110void ParticleEmitter::setEmissionRate(float emissionRate)
[3933]111{
[4338]112  if (emissionRate > 0.0)
113    this->emissionRate = emissionRate;
114  else
115    this->emissionRate = 0.0;
[3933]116}
[3929]117
118/**
[4836]119 *  how much of the speed from the ParticleEmitter should flow onto the ParticleSystem
120 * @param value a Value between zero and one
[4493]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*/
125void 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/**
[4836]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
[3929]139
140   if you want to change the value of this variable during emission time (to make it more dynamic)
[3930]141   you may want to use the animation class
[3929]142*/
[3931]143void ParticleEmitter::setSpread(float angle, float randomAngle)
[3935]144{
145  this->angle = angle;
146  this->randomAngle = randomAngle;
147}
[3929]148
149/**
[4836]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
[3929]153
154   if you want to change the value of this variable during emission time (to make it more dynamic)
[3930]155   you may want to use the animation class
[3929]156*/
[4338]157void ParticleEmitter::setEmissionVelocity(float velocity, float randomVelocity)
[3935]158{
159  this->velocity = velocity;
160  this->randomVelocity = randomVelocity;
161}
[3929]162
163/**
[4836]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.
[4690]167 */
168void ParticleEmitter::setEmissionMomentum(float momentum, float randomMomentum)
169{
170  this->momentum = momentum;
171  this->momentumRandom = randomMomentum;
172}
173
174/**
[10658]175 *  sets the external force that affects all particles emitted
176*/
177void ParticleEmitter::setExtForce(float x, float y, float z)
178{
179    this->extForce = Vector(x, y, z);
180}
181
182/**
[4836]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
[3929]186
187   if you want to change the value of this variable during emission time (to make it more dynamic)
[3930]188   you may want to use the animation class
[3929]189*/
[6620]190void ParticleEmitter::tick(float dt)
[3932]191{
[6620]192  assert (this->system != NULL);
[3950]193  if (likely(dt > 0.0 && this->emissionRate > 0.0))
194  {
[4692]195    // saving the time (particles only partly emitted in this timestep)
[3950]196    float count = (dt+this->saveTime) * this->emissionRate;
[4017]197    this->saveTime = modff(count, &count) / this->emissionRate;
[4692]198    PRINTF(5)("emitting %f particles, saving %f seconds for the next timestep\n", count, this->saveTime);
[7027]199    if (count + this->system->getCount() > this->system->getMaxCount())
200      count = this->system->getMaxCount() - this->system->getCount();
[6822]201    if (likely(count > 0.0f))
202      this->emitParticles((unsigned int)count);
[3950]203  }
[3932]204}
[3944]205
206/**
[4836]207 *  outputs some nice debug information
[3944]208*/
[4746]209void ParticleEmitter::debug() const
[3944]210{
[9406]211  PRINT(0)(" ParticleEmitter %s::%s\n", this->getClassCName(), this->getCName());
[4639]212  PRINT(0)("  EmissionRate: %f, Speed: %f, SpreadAngle: %f\n", this->getEmissionRate(), this->getEmissionVelocity(), this->getSpread());
[3944]213}
Note: See TracBrowser for help on using the repository browser.