lbm_reference
Particle.h
Go to the documentation of this file.
1 //! \file Particle.h
2 //! \date Mar 12, 2009
3 //! \author Florian Rathgeber
4 
5 #ifndef PARTICLE_H_
6 #define PARTICLE_H_
7 
8 #define RAND1 .4 * rand() / (float) RAND_MAX - .2
9 
10 #include <vector>
11 #include <irrlicht/irrlicht.h>
12 
13 using namespace irr;
14 
15 //! Common namespace for all classes related to the particle system
16 
17 namespace particles {
18 
19 //! Possible different particle types
20 
21 enum ParticleType { FIRE = 0, SMOKE = 1 };
22 
23 class ParticleSystem;
24 
25 //! Individual particle of the particle system
26 
27 //! Has a configurable number of sprites (display primitives) assigned to it.
28 
29 class Particle {
30 
31  //! Fried declaration to allow ParticleSystem class access to protected
32  //! members
33 
34  friend class ParticleSystem;
35 
36 public:
37 
38  // ============================ //
39  // Constructors and destructors //
40  // ============================ //
41 
42  //! Default constructor
43 
44  //! Does nothing.
45 
46  Particle () {}
47 
48  //! Constructor for particles with sprites
49 
50  //! \param[in] mgr Pointer to irrlicht scene manager
51  //! \param[in] id Particle ID
52  //! \param[in] position Initial particle position
53  //! \param[in] texture Texture to assign to sprites of this particle
54  //! \param[in] numSprites Number of sprites assigned to each particle instance
55  //! \param[in] temp Initial particle temperature
56  //! \param[in] color Initial particle color
57  //! \param[in] lifetime Particle lifetime (in timesteps)
58  //! \param[in] dynamicLights Turn dynamic lighting on / off
59 
60  Particle ( scene::ISceneManager* mgr,
61  s32 id,
62  const core::vector3df &position,
63  std::vector< video::ITexture* >& textures,
64  int numSprites,
65  float temp,
66  video::SColor& color,
67  float size,
68  int lifetime,
69  bool dynamicLights = false );
70 
71  //! Constructor for particles without sprites
72 
73  //! \param[in] position Initial particle position
74  //! \param[in] temp Initial particle temperature
75  //! \param[in] lifetime Particle lifetime (in timesteps)
76 
77  Particle ( const core::vector3df &position,
78  float temp,
79  int lifetime ) : pos_( position ),
80  type_( FIRE ),
81  temp_( temp ),
82  lifetime_( lifetime ) {}
83 
84  //! Destructor
85 
86  //! Does nothing.
87 
88  virtual ~Particle () {}
89 
90  // ======= //
91  // Getters //
92  // ======= //
93 
94  //! Get distance between this and another particle
95 
96  //! \param[in] p Particle to measure distance from
97  //! \return Distance between this and given particle
98 
99  float dist ( Particle& p ) {
100  float dx = pos_.X - p.pos_.X;
101  float dy = pos_.Y - p.pos_.Y;
102  float dz = pos_.Y - p.pos_.Y;
103  return sqrt( dx * dx + dy * dy + dz * dz );
104  }
105 
106  //! Get current particle position
107 
108  //! \return Current particle position as vector
109 
110  const core::vector3df& getPos() {
111  return pos_;
112  }
113 
114  // ======= //
115  // Setters //
116  // ======= //
117 
118  //! Update particle position, including assigned sprites
119 
120  //! \param[in] d Displacement vector by which particle is to be moved
121 
122  void updatePos( const core::vector3df& d ) {
123  pos_ += d;
124  for ( uint i = 0; i < sprites_.size(); ++i ) {
125  // Sprites are moved by given displacement + a random offset
126  sprites_[i]->setPosition( sprites_[i]->getPosition() + d
127  + core::vector3df( RAND1, RAND1, RAND1 ) );
128  }
129  }
130 
131  //! Set the color for all assigned sprites
132 
133  //! \param[in] c Color to assign to all assigned sprites
134 
135  void setColor( const video::SColor& c ) {
136  for ( uint i = 0; i < sprites_.size(); ++i ) {
137  sprites_[i]->setColor( c );
138 // (*( sprites_[i]->getChildren().begin() ))->setRadius( temp_ );
139  }
140  }
141 
142  //! Set the size for all assigned sprites
143 
144  //! \param[in] sz Size to assign to all assigned sprites
145 
146  void setSize( float sz ) {
147  for ( uint i = 0; i < sprites_.size(); ++i ) {
148  sprites_[i]->setSize( core::dimension2df( sz, sz ) );
149  }
150  }
151 
152  void setTexture( std::vector< video::ITexture* >& textures ) {
153  for ( uint i = 0; i < sprites_.size(); ++i ) {
154  sprites_[i]->setMaterialTexture( 0, textures[ rand() % textures.size() ] );
155  }
156  }
157 
158  void setSmoke( float colorCoeff) {
159  type_ = SMOKE;
160 
161  for ( uint i = 0; i < sprites_.size(); ++i ) {
162  sprites_[i]->setMaterialType( video::EMT_TRANSPARENT_ALPHA_CHANNEL );
163  sprites_[i]->setColor( video::SColor( 255 * lifetime_ * colorCoeff,
164  255 * lifetime_ * colorCoeff,
165  255 * lifetime_ * colorCoeff,
166  255 * lifetime_ * colorCoeff ) );
167  // Remove light source attached to the billboard
168  sprites_[i]->removeAll();
169  }
170  }
171 
172  //! Delete all sprites, i.e. remove them from the scene
173 
174  void clear() {
175  for ( uint i = 0; i < sprites_.size(); ++i ) {
176  sprites_[i]->remove();
177  }
178  sprites_.clear();
179  }
180 
181 protected:
182 
183  // ============ //
184  // Data members //
185  // ============ //
186 
187  //! Vector of sprites (display primitives) assigned to this particle
188  std::vector< scene::IBillboardSceneNode* > sprites_;
189 
190  //! Current particle position
191  core::vector3df pos_;
192 
193  //! Particle type (either FIRE or SMOKE)
195 
196  //! Current particle temperature
197  float temp_;
198 
199  //! Current particle lifetime, decreased every timestep
201 
202 };
203 
204 }
205 
206 #endif /* PARTICLE_H_ */