Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/orxonox/branches/particleEngine/src/lib/graphics/particles/particle_system.cc @ 3936

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

orxonox/branches/particleEngine: size should iterate

File size: 4.3 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: ...
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PARTICLE
17
18#include "particle_system.h"
19
20#include "particle_emitter.h"
21#include "particle_engine.h"
22#include "compiler.h"
23
24using namespace std;
25
26
27/**
28   \brief standard constructor
29   \param count the Count of particles in the System
30   \param type The Type of the ParticleSystem
31
32   \todo this constructor is not jet implemented - do it
33*/
34ParticleSystem::ParticleSystem (unsigned int maxCount, PARTICLE_TYPE type)
35{
36   this->setClassName ("ParticleSystem");
37
38   this->maxCount = maxCount;
39   this->count = 0;
40   this->particleType = type;
41   this->particles = NULL;
42   this->setConserve(.8);
43   this->setLifeSpan(.1);
44
45   ParticleEngine::getInstance()->addSystem(this);
46}
47
48
49/**
50   \brief standard deconstructor
51
52*/
53ParticleSystem::~ParticleSystem() 
54{
55  // delete what has to be deleted here
56   ParticleEngine::getInstance()->removeSystem(this);
57}
58
59// setting properties
60void ParticleSystem::setMaterial(Material* material)
61{
62  this->material = material;
63}
64
65void ParticleSystem::setLifeSpan(float lifeSpan, float randomLifeSpan)
66{
67  this->lifeSpan = lifeSpan;
68  this->randomLifeSpan = randomLifeSpan;
69}
70
71void ParticleSystem::setRadius(float startRadius, float endRadius, float randomRadius)
72{
73  this->startRadius = startRadius;
74  this->endRadius = endRadius;
75  this->randomRadius = randomRadius;
76}
77
78void ParticleSystem::setConserve(float conserve)
79{
80  if (conserve > 1.0)
81    this->conserve = 1.0;
82  else if (conserve < 0.0)
83    this->conserve = 0.0;
84  else
85    this->conserve = conserve;
86}
87
88
89
90void ParticleSystem::tick(float dt)
91{
92  Particle* tickPart = particles;  // the particle to Tick
93  Particle* prevPart = NULL;       //
94  while (likely(tickPart != NULL))
95    {
96     
97      tickPart->position = tickPart->position + tickPart->velocity;
98      tickPart->radius + tickPart->radiusIt * dt;
99      // many more to come
100
101
102
103      if (this->conserve < 1.0)
104        tickPart->velocity = tickPart->velocity * this->conserve;
105      // find out if we have to delete tickPart
106      if ((tickPart->timeToLive -= dt) <= 0)
107        {
108          // remove the particle from the list
109          if (likely(prevPart != NULL))
110            {
111              prevPart->next = tickPart->next;
112              delete tickPart;
113              tickPart = prevPart->next;
114            }
115          else
116            {
117              prevPart = NULL;
118              this->particles = tickPart->next;
119              delete tickPart;
120              tickPart = this->particles;
121            }
122          --this->count;
123        }
124      else
125        {     
126          prevPart = tickPart;
127          tickPart = tickPart->next;
128        }
129    }
130}
131
132void ParticleSystem::draw(void)
133{
134  Particle* drawPart = particles;
135  if (likely(drawPart != NULL))
136    {
137      glBegin(GL_POINTS);
138      while (likely(drawPart != NULL))
139        {
140          // draw in DOT mode
141          glVertex3f(drawPart->position.x, drawPart->position.y, drawPart->position.z);
142         
143         
144          drawPart = drawPart->next;
145        }
146      glEnd();
147    }
148}
149
150
151void ParticleSystem::addParticle(Vector position, Vector velocity, unsigned int data)
152{
153  if (this->count <= this->maxCount)
154    {
155      // if it is the first Particle
156      if (unlikely(particles == NULL))
157        {
158          this->particles = new Particle;
159          this->particles->next = NULL;
160        }
161      // filling the List from the beginning
162      else
163        {
164          Particle* tmpPart = new Particle;
165          tmpPart->next = this->particles;
166          this->particles = tmpPart;
167        }
168     
169      particles->timeToLive = this->lifeSpan + (float)(random()/RAND_MAX)* this->randomLifeSpan;
170      particles->position = position;
171      particles->velocity = velocity;
172      //  particle->rotation = ; //! \todo rotation is once again something to be done.
173      particles->mass = this->initialMass + (random()/RAND_MAX -.5)* this->randomInitialMass;
174      particles->radius = this->startRadius + (random()/RAND_MAX-.5)*this->randomRadius;
175     
176      particles->radiusIt = (this->endRadius + (random()/RAND_MAX-.5)*this->randomRadius - particles->radius) / particles->timeToLive;
177
178      ++this->count;
179    }
180  else
181    PRINTF(4)("maximum count of particles reached not adding any more\n");
182}
183
Note: See TracBrowser for help on using the repository browser.