LiquidFun
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
b2Particle.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2013 Google, Inc.
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 * 1. The origin of this software must not be misrepresented; you must not
11 * claim that you wrote the original software. If you use this software
12 * in a product, an acknowledgment in the product documentation would be
13 * appreciated but is not required.
14 * 2. Altered source versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software.
16 * 3. This notice may not be removed or altered from any source distribution.
17 */
18 #ifndef B2_PARTICLE
19 #define B2_PARTICLE
20 
21 #include <Box2D/Common/b2Math.h>
23 #include <Box2D/Common/b2IntrusiveList.h>
24 
25 struct b2Color;
26 class b2ParticleGroup;
27 
29 
32 {
38  b2_wallParticle = 1 << 2,
54  b2_barrierParticle = 1 << 10,
78 };
79 
82 {
83 public:
84  b2ParticleColor() {}
88  b2Inline b2ParticleColor(uint8 r, uint8 g, uint8 b, uint8 a)
89  {
90  Set(r, g, b, a);
91  }
92 
95  b2ParticleColor(const b2Color& color);
96 
100  bool IsZero() const
101  {
102  return !r && !g && !b && !a;
103  }
104 
107  b2Color GetColor() const;
108 
111  b2Inline void Set(uint8 r_, uint8 g_, uint8 b_, uint8 a_)
112  {
113  r = r_;
114  g = g_;
115  b = b_;
116  a = a_;
117  }
118 
121  void Set(const b2Color& color);
122 
125  {
126  Set(color.r, color.g, color.b, color.a);
127  return *this;
128  }
129 
133  {
134  Set((uint8)(r * s), (uint8)(g * s), (uint8)(b * s), (uint8)(a * s));
135  return *this;
136  }
137 
140  {
141  // 1..256 to maintain the complete dynamic range.
142  const int32 scale = (int32)s + 1;
143  Set((uint8)(((int32)r * scale) >> k_bitsPerComponent),
144  (uint8)(((int32)g * scale) >> k_bitsPerComponent),
145  (uint8)(((int32)b * scale) >> k_bitsPerComponent),
146  (uint8)(((int32)a * scale) >> k_bitsPerComponent));
147  return *this;
148  }
149 
151  b2ParticleColor operator * (float32 s) const
152  {
153  return MultiplyByScalar(s);
154  }
155 
158  {
159  return MultiplyByScalar(s);
160  }
161 
165  {
166  r += color.r;
167  g += color.g;
168  b += color.b;
169  a += color.a;
170  return *this;
171  }
172 
176  {
177  b2ParticleColor newColor(*this);
178  newColor += color;
179  return newColor;
180  }
181 
185  {
186  r -= color.r;
187  g -= color.g;
188  b -= color.b;
189  a -= color.a;
190  return *this;
191  }
192 
196  {
197  b2ParticleColor newColor(*this);
198  newColor -= color;
199  return newColor;
200  }
201 
203  bool operator == (const b2ParticleColor &color) const
204  {
205  return r == color.r && g == color.g && b == color.b && a == color.a;
206  }
207 
213  b2Inline void Mix(b2ParticleColor * const mixColor, const int32 strength)
214  {
215  MixColors(this, mixColor, strength);
216  }
217 
223  static b2Inline void MixColors(b2ParticleColor * const colorA,
224  b2ParticleColor * const colorB,
225  const int32 strength)
226  {
227  const uint8 dr = (uint8)((strength * (colorB->r - colorA->r)) >>
229  const uint8 dg = (uint8)((strength * (colorB->g - colorA->g)) >>
231  const uint8 db = (uint8)((strength * (colorB->b - colorA->b)) >>
233  const uint8 da = (uint8)((strength * (colorB->a - colorA->a)) >>
235  colorA->r += dr;
236  colorA->g += dg;
237  colorA->b += db;
238  colorA->a += da;
239  colorB->r -= dr;
240  colorB->g -= dg;
241  colorB->b -= db;
242  colorB->a -= da;
243  }
244 
245 private:
248  template <typename T>
249  b2ParticleColor MultiplyByScalar(T s) const
250  {
251  b2ParticleColor color(*this);
252  color *= s;
253  return color;
254  }
255 
256 public:
257  uint8 r, g, b, a;
258 
259 protected:
261  static const float32 k_maxValue;
263  static const float32 k_inverseMaxValue;
265  static const uint8 k_bitsPerComponent;
266 };
267 
268 extern b2ParticleColor b2ParticleColor_zero;
269 
273 {
274  b2ParticleDef()
275  {
276  flags = 0;
277  position = b2Vec2_zero;
278  velocity = b2Vec2_zero;
279  color = b2ParticleColor_zero;
280  lifetime = 0.0f;
281  userData = NULL;
282  group = NULL;
283  }
284 
285 #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
286  void SetPosition(float32 x, float32 y);
288 
290  void SetColor(int32 r, int32 g, int32 b, int32 a);
291 #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
292 
298  uint32 flags;
299 
302 
305 
308 
311  float32 lifetime;
312 
314  void* userData;
315 
318 
319 };
320 
323  float32 gravity, float32 radius, float32 timeStep);
324 
330 class b2ParticleHandle : public b2TypedIntrusiveListNode<b2ParticleHandle>
331 {
332  // Allow b2ParticleSystem to use SetIndex() to associate particle handles
333  // with particle indices.
334  friend class b2ParticleSystem;
335 
336 public:
341 
343  int32 GetIndex() const { return m_index; }
344 
345 private:
347  void SetIndex(int32 index) { m_index = index; }
348 
349 private:
350  // Index of the particle within the particle system.
351  int32 m_index;
352 };
353 
354 #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
355 inline void b2ParticleDef::SetPosition(float32 x, float32 y)
356 {
357  position.Set(x, y);
358 }
359 
360 inline void b2ParticleDef::SetColor(int32 r, int32 g, int32 b, int32 a)
361 {
362  color.Set((uint8)r, (uint8)g, (uint8)b, (uint8)a);
363 }
364 #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
365 
366 #endif
Mix color between contacting particles.
Definition: b2Particle.h:50
Prevents other particles from leaking.
Definition: b2Particle.h:54
static const float32 k_inverseMaxValue
1.0 / k_maxValue.
Definition: b2Particle.h:263
Makes pairs or triads with other particles.
Definition: b2Particle.h:58
With high repulsive force.
Definition: b2Particle.h:60
Definition: b2Particle.h:77
Call b2DestructionListener on destruction.
Definition: b2Particle.h:52
float32 lifetime
Definition: b2Particle.h:311
~b2ParticleHandle()
Empty destructor.
Definition: b2Particle.h:340
Definition: b2Particle.h:72
b2Vec2 velocity
The linear velocity of the particle in world co-ordinates.
Definition: b2Particle.h:304
Removed after next simulation step.
Definition: b2Particle.h:36
b2ParticleColor & operator=(const b2ParticleColor &color)
Assign a b2ParticleColor to this instance.
Definition: b2Particle.h:124
Definition: b2ParticleSystem.h:281
b2ParticleColor operator*(float32 s) const
Scales r, g, b, a members by s returning the modified b2ParticleColor.
Definition: b2Particle.h:151
int32 b2CalculateParticleIterations(float32 gravity, float32 radius, float32 timeStep)
A helper function to calculate the optimal number of iterations.
Definition: b2Particle.cpp:55
bool operator==(const b2ParticleColor &color) const
Compare this color with the specified color.
Definition: b2Particle.h:203
Color for debug drawing. Each value has the range [0,1].
Definition: b2Draw.h:27
With restitution from deformation.
Definition: b2Particle.h:42
With viscosity.
Definition: b2Particle.h:44
b2Inline b2ParticleColor & operator+=(const b2ParticleColor &color)
Definition: b2Particle.h:164
b2Vec2 position
The world position of the particle.
Definition: b2Particle.h:301
Definition: b2IntrusiveList.h:272
Definition: b2Particle.h:272
b2Inline void Mix(b2ParticleColor *const mixColor, const int32 strength)
Definition: b2Particle.h:213
Small color object for each particle.
Definition: b2Particle.h:81
Without isotropic pressure.
Definition: b2Particle.h:46
void Set(float32 x_, float32 y_)
Set this vector to some specified coordinates.
Definition: b2Math.h:68
b2ParticleColor operator+(const b2ParticleColor &color) const
Definition: b2Particle.h:175
b2Inline b2ParticleColor(uint8 r, uint8 g, uint8 b, uint8 a)
Definition: b2Particle.h:88
Definition: b2Particle.h:66
void * userData
Use this to store application-specific body data.
Definition: b2Particle.h:314
b2Color GetColor() const
Definition: b2Particle.cpp:40
With surface tension.
Definition: b2Particle.h:48
A group of particles. b2ParticleGroup::CreateParticleGroup creates these.
Definition: b2ParticleGroup.h:172
int32 GetIndex() const
Get the index of the particle associated with this handle.
Definition: b2Particle.h:343
b2ParticleHandle()
Initialize the index associated with the handle to an invalid index.
Definition: b2Particle.h:338
b2ParticleColor color
The color of the particle.
Definition: b2Particle.h:307
b2Inline void Set(uint8 r_, uint8 g_, uint8 b_, uint8 a_)
Definition: b2Particle.h:111
Less compressibility.
Definition: b2Particle.h:56
b2Inline b2ParticleColor & operator-=(const b2ParticleColor &color)
Definition: b2Particle.h:184
uint32 flags
Specifies the type of particle (see b2ParticleFlag).
Definition: b2Particle.h:298
b2ParticleColor & operator*=(float32 s)
Definition: b2Particle.h:132
Water particle.
Definition: b2Particle.h:34
With restitution from stretching.
Definition: b2Particle.h:40
bool IsZero() const
Definition: b2Particle.h:100
b2ParticleColor operator-(const b2ParticleColor &color) const
Definition: b2Particle.h:195
static const uint8 k_bitsPerComponent
Number of bits used to store each b2ParticleColor component.
Definition: b2Particle.h:265
A 2D column vector.
Definition: b2Math.h:56
static b2Inline void MixColors(b2ParticleColor *const colorA, b2ParticleColor *const colorB, const int32 strength)
Definition: b2Particle.h:223
#define b2_invalidParticleIndex
NEON SIMD requires 16-bit particle indices.
Definition: b2Settings.h:168
b2ParticleGroup * group
An existing particle group to which the particle will be added.
Definition: b2Particle.h:317
static const float32 k_maxValue
Maximum value of a b2ParticleColor component.
Definition: b2Particle.h:261
Call b2ContactFilter when this particle interacts with rigid bodies.
Definition: b2Particle.h:74
b2ParticleFlag
The particle type. Can be combined with the | operator.
Definition: b2Particle.h:31
Zero velocity.
Definition: b2Particle.h:38
Definition: b2Particle.h:330