Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/particleEngine: spread works, and many other functions implemented

File size: 4.1 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      // many more to come
99     
100
101
102
103
104      if (this->conserve < 1.0)
105        tickPart->velocity = tickPart->velocity * this->conserve;
106      // find out if we have to delete tickPart
107      if ((tickPart->timeToLive -= dt) <= 0)
108        {
109          // remove the particle from the list
110          if (likely(prevPart != NULL))
111            {
112              prevPart->next = tickPart->next;
113              delete tickPart;
114              tickPart = prevPart->next;
115            }
116          else
117            {
118              prevPart = NULL;
119              this->particles = tickPart->next;
120              delete tickPart;
121              tickPart = this->particles;
122            }
123          --this->count;
124        }
125      else
126        {     
127          prevPart = tickPart;
128          tickPart = tickPart->next;
129        }
130    }
131}
132
133void ParticleSystem::draw(void)
134{
135  Particle* drawPart = particles;
136  if (likely(drawPart != NULL))
137    {
138      glBegin(GL_POINTS);
139      while (likely(drawPart != NULL))
140        {
141          // draw in DOT mode
142          glVertex3f(drawPart->position.x, drawPart->position.y, drawPart->position.z);
143         
144         
145          drawPart = drawPart->next;
146        }
147      glEnd();
148    }
149}
150
151
152void ParticleSystem::addParticle(Vector position, Vector velocity, unsigned int data)
153{
154  if (this->count <= this->maxCount)
155    {
156      // if it is the first Particle
157      if (unlikely(particles == NULL))
158        {
159          this->particles = new Particle;
160          this->particles->next = NULL;
161        }
162      // filling the List from the beginning
163      else
164        {
165          Particle* tmpPart = new Particle;
166          tmpPart->next = this->particles;
167          this->particles = tmpPart;
168        }
169     
170      particles->timeToLive = this->lifeSpan + (float)(random()/RAND_MAX)* this->randomLifeSpan;
171      particles->position = position;
172      particles->velocity = velocity;
173      //  particle->rotation = ; //! \todo rotation is once again something to be done.
174      particles->mass = this->initialMass + (random()/RAND_MAX)* this->randomInitialMass;
175      particles->radius = this->startRadius + (random()/RAND_MAX)*this->randomRadius;
176
177      ++this->count;
178    }
179  else
180    PRINTF(4)("maximum count of particles reached not adding any more\n");
181}
182
Note: See TracBrowser for help on using the repository browser.