LiquidFun
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
b2ParticleGroup.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_GROUP
19 #define B2_PARTICLE_GROUP
20 
22 
23 class b2Shape;
24 class b2World;
25 class b2ParticleSystem;
26 class b2ParticleGroup;
27 class b2ParticleColor;
28 #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
29 class b2CircleShape;
30 #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
31 
33 
36 {
47  b2_particleGroupInternalMask =
50 };
51 
55 {
56 
58  {
59  flags = 0;
60  groupFlags = 0;
61  position = b2Vec2_zero;
62  angle = 0;
63  linearVelocity = b2Vec2_zero;
64  angularVelocity = 0;
65  color = b2ParticleColor_zero;
66  strength = 1;
67  shape = NULL;
68  shapes = NULL;
69  shapeCount = 0;
70  stride = 0;
71  particleCount = 0;
72  positionData = NULL;
73  lifetime = 0.0f;
74  userData = NULL;
75  group = NULL;
76 
77 #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
78  circleShapes = NULL;
79  ownShapesArray = false;
80 #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
81  }
82 
84  {
85 #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
86  FreeShapesMemory();
87 #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
88  }
89 
91  uint32 flags;
92 
94  uint32 groupFlags;
95 
99 
102  float32 angle;
103 
106 
109 
112 
115  float32 strength;
116 
118  const b2Shape* shape;
119 
121  const b2Shape* const* shapes;
122 
124  int32 shapeCount;
125 
128  float32 stride;
129 
132 
135 
138  float32 lifetime;
139 
141  void* userData;
142 
145 
146 #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
147  const b2CircleShape* circleShapes;
149 
151  bool ownShapesArray;
152 
154  void FreeShapesMemory();
155 
159  void SetCircleShapesFromVertexList(void* inBuf,
160  int numShapes,
161  float radius);
162 
164  void SetPosition(float32 x, float32 y);
165 
167  void SetColor(int32 r, int32 g, int32 b, int32 a);
168 #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
169 };
170 
173 {
174 
175 public:
176 
179  const b2ParticleGroup* GetNext() const;
180 
183  const b2ParticleSystem* GetParticleSystem() const;
184 
186  int32 GetParticleCount() const;
187 
189  int32 GetBufferIndex() const;
190 
192  bool ContainsParticle(int32 index) const;
193 
195  uint32 GetAllParticleFlags() const;
196 
198  uint32 GetGroupFlags() const;
199 
201  void SetGroupFlags(uint32 flags);
202 
204  float32 GetMass() const;
205 
207  float32 GetInertia() const;
208 
210  b2Vec2 GetCenter() const;
211 
213  b2Vec2 GetLinearVelocity() const;
214 
216  float32 GetAngularVelocity() const;
217 
220  const b2Transform& GetTransform() const;
221 
224  const b2Vec2& GetPosition() const;
225 
228  float32 GetAngle() const;
229 
234  b2Vec2 GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const;
235 
237  void* GetUserData() const;
238 
240  void SetUserData(void* data);
241 
243  void ApplyForce(const b2Vec2& force);
244 
247  void ApplyLinearImpulse(const b2Vec2& impulse);
248 
254  void DestroyParticles(bool callDestructionListener);
255 
260  void DestroyParticles();
261 
262 private:
263 
264  friend class b2ParticleSystem;
265 
266  b2ParticleSystem* m_system;
267  int32 m_firstIndex, m_lastIndex;
268  uint32 m_groupFlags;
269  float32 m_strength;
270  b2ParticleGroup* m_prev;
271  b2ParticleGroup* m_next;
272 
273  mutable int32 m_timestamp;
274  mutable float32 m_mass;
275  mutable float32 m_inertia;
276  mutable b2Vec2 m_center;
277  mutable b2Vec2 m_linearVelocity;
278  mutable float32 m_angularVelocity;
279  mutable b2Transform m_transform;
280 
281  void* m_userData;
282 
283  b2ParticleGroup();
284  ~b2ParticleGroup();
285  void UpdateStatistics() const;
286 
287 };
288 
290 {
291  return m_next;
292 }
293 
294 inline const b2ParticleGroup* b2ParticleGroup::GetNext() const
295 {
296  return m_next;
297 }
298 
300 {
301  return m_system;
302 }
303 
305 {
306  return m_system;
307 }
308 
310 {
311  return m_lastIndex - m_firstIndex;
312 }
313 
314 inline bool b2ParticleGroup::ContainsParticle(int32 index) const
315 {
316  return m_firstIndex <= index && index < m_lastIndex;
317 }
318 
319 inline b2ParticleGroup::~b2ParticleGroup()
320 {
321 }
322 
324 {
325  return m_firstIndex;
326 }
327 
328 inline uint32 b2ParticleGroup::GetGroupFlags() const
329 {
330  return m_groupFlags & ~b2_particleGroupInternalMask;
331 }
332 
333 inline float32 b2ParticleGroup::GetMass() const
334 {
335  UpdateStatistics();
336  return m_mass;
337 }
338 
339 inline float32 b2ParticleGroup::GetInertia() const
340 {
341  UpdateStatistics();
342  return m_inertia;
343 }
344 
346 {
347  UpdateStatistics();
348  return m_center;
349 }
350 
352 {
353  UpdateStatistics();
354  return m_linearVelocity;
355 }
356 
358 {
359  UpdateStatistics();
360  return m_angularVelocity;
361 }
362 
364 {
365  return m_transform;
366 }
367 
369 {
370  return m_transform.p;
371 }
372 
373 inline float32 b2ParticleGroup::GetAngle() const
374 {
375  return m_transform.q.GetAngle();
376 }
377 
379  const b2Vec2& worldPoint) const
380 {
381  UpdateStatistics();
382  return m_linearVelocity + b2Cross(m_angularVelocity, worldPoint - m_center);
383 }
384 
385 inline void* b2ParticleGroup::GetUserData() const
386 {
387  return m_userData;
388 }
389 
390 inline void b2ParticleGroup::SetUserData(void* data)
391 {
392  m_userData = data;
393 }
394 
396 {
397  DestroyParticles(false);
398 }
399 
400 #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
401 inline void b2ParticleGroupDef::SetPosition(float32 x, float32 y)
402 {
403  position.Set(x, y);
404 }
405 
406 inline void b2ParticleGroupDef::SetColor(int32 r, int32 g, int32 b, int32 a)
407 {
408  color.Set((uint8)r, (uint8)g, (uint8)b, (uint8)a);
409 }
410 #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
411 
412 
413 #endif
Definition: b2Math.h:411
b2Vec2 linearVelocity
The linear velocity of the group's origin in world co-ordinates.
Definition: b2ParticleGroup.h:105
A circle shape.
Definition: b2CircleShape.h:26
float32 stride
Definition: b2ParticleGroup.h:128
uint32 flags
The particle-behavior flags (See b2ParticleFlag).
Definition: b2ParticleGroup.h:91
Keeps its shape.
Definition: b2ParticleGroup.h:40
float32 GetAngle() const
Get the angle in radians.
Definition: b2Math.h:388
void * userData
Use this to store application-specific group data.
Definition: b2ParticleGroup.h:141
Definition: b2ParticleGroup.h:54
float32 angle
Definition: b2ParticleGroup.h:102
const b2Shape *const * shapes
A array of shapes where particles will be added.
Definition: b2ParticleGroup.h:121
float32 GetMass() const
Get the total mass of the group: the sum of all particles in it.
Definition: b2ParticleGroup.h:333
const b2Vec2 * positionData
The initial positions of the particleCount particles.
Definition: b2ParticleGroup.h:134
b2ParticleGroupFlag
The particle group type. Can be combined with the | operator.
Definition: b2ParticleGroup.h:35
b2Vec2 position
Definition: b2ParticleGroup.h:98
Will be destroyed on next simulation step.
Definition: b2ParticleGroup.h:44
void ApplyForce(const b2Vec2 &force)
Call b2ParticleSystem::ApplyForce for every particle in the group.
Definition: b2ParticleGroup.cpp:102
b2ParticleGroup * group
An existing particle group to which the particles will be added.
Definition: b2ParticleGroup.h:144
Updates depth data on next simulation step.
Definition: b2ParticleGroup.h:46
uint32 groupFlags
The group-construction flags (See b2ParticleGroupFlag).
Definition: b2ParticleGroup.h:94
b2Vec2 GetLinearVelocity() const
Get the linear velocity of the group.
Definition: b2ParticleGroup.h:351
Definition: b2World.h:44
Definition: b2ParticleSystem.h:281
uint32 GetAllParticleFlags() const
Get the logical sum of particle flags.
Definition: b2ParticleGroup.cpp:49
Won't be destroyed if it gets empty.
Definition: b2ParticleGroup.h:42
bool ContainsParticle(int32 index) const
Does this group contain the particle.
Definition: b2ParticleGroup.h:314
Definition: b2Shape.h:43
b2Vec2 GetCenter() const
Get the center of gravity for the group.
Definition: b2ParticleGroup.h:345
Small color object for each particle.
Definition: b2Particle.h:81
void Set(float32 x_, float32 y_)
Set this vector to some specified coordinates.
Definition: b2Math.h:68
float32 angularVelocity
The angular velocity of the group.
Definition: b2ParticleGroup.h:108
float32 GetAngle() const
Definition: b2ParticleGroup.h:373
uint32 GetGroupFlags() const
Get the construction flags for the group.
Definition: b2ParticleGroup.h:328
void SetGroupFlags(uint32 flags)
Set the construction flags for the group.
Definition: b2ParticleGroup.cpp:59
int32 particleCount
The number of particles in addition to ones added in the shape.
Definition: b2ParticleGroup.h:131
const b2Transform & GetTransform() const
Definition: b2ParticleGroup.h:363
A group of particles. b2ParticleGroup::CreateParticleGroup creates these.
Definition: b2ParticleGroup.h:172
b2Vec2 GetLinearVelocityFromWorldPoint(const b2Vec2 &worldPoint) const
Definition: b2ParticleGroup.h:378
void SetUserData(void *data)
Set the user data. Use this to store your application specific data.
Definition: b2ParticleGroup.h:390
float32 strength
Definition: b2ParticleGroup.h:115
b2Inline void Set(uint8 r_, uint8 g_, uint8 b_, uint8 a_)
Definition: b2Particle.h:111
void ApplyLinearImpulse(const b2Vec2 &impulse)
Definition: b2ParticleGroup.cpp:107
int32 GetParticleCount() const
Get the number of particles.
Definition: b2ParticleGroup.h:309
Prevents overlapping or leaking.
Definition: b2ParticleGroup.h:38
float32 GetInertia() const
Get the moment of inertia for the group.
Definition: b2ParticleGroup.h:339
b2ParticleSystem * GetParticleSystem()
Get the particle system that holds this particle group.
Definition: b2ParticleGroup.h:299
int32 shapeCount
The number of shapes.
Definition: b2ParticleGroup.h:124
void * GetUserData() const
Get the user data pointer that was provided in the group definition.
Definition: b2ParticleGroup.h:385
const b2Vec2 & GetPosition() const
Definition: b2ParticleGroup.h:368
int32 GetBufferIndex() const
Get the offset of this group in the global particle buffer.
Definition: b2ParticleGroup.h:323
A 2D column vector.
Definition: b2Math.h:56
b2ParticleGroup * GetNext()
Get the next particle group from the list in b2_World.
Definition: b2ParticleGroup.h:289
void DestroyParticles()
Definition: b2ParticleGroup.h:395
b2ParticleColor color
The color of all particles in the group.
Definition: b2ParticleGroup.h:111
float32 GetAngularVelocity() const
Get the angular velocity of the group.
Definition: b2ParticleGroup.h:357
const b2Shape * shape
The shape where particles will be added.
Definition: b2ParticleGroup.h:118
float32 lifetime
Definition: b2ParticleGroup.h:138