LiquidFun
 All Classes Files Functions Variables Enumerations Enumerator Macros Pages
b2ParticleSystem.h
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_PARTICLb2_SYSTEM_H
19 #define B2_PARTICLb2_SYSTEM_H
20 
21 #include <Box2D/Particle/b2Particle.h>
22 #include <Box2D/Dynamics/b2TimeStep.h>
23 
26 
27 class b2World;
28 class b2Body;
29 class b2Shape;
30 class b2ParticleGroup;
31 class b2BlockAllocator;
32 class b2StackAllocator;
33 class b2QueryCallback;
34 class b2RayCastCallback;
35 struct b2ParticleGroupDef;
36 struct b2Vec2;
37 struct b2AABB;
38 
40 {
43  int32 indexA, indexB;
46  uint32 flags;
49  float32 weight;
53 };
54 
56 {
59  int32 index;
65  float32 weight;
71  float32 mass;
72 };
73 
75 {
76 
77 private:
78 
79  friend class b2World;
80  friend class b2ParticleGroup;
81 
82  template <typename T>
83  struct ParticleBuffer
84  {
85  ParticleBuffer()
86  {
87  data = NULL;
88  userSuppliedCapacity = 0;
89  }
90  T* data;
91  int32 userSuppliedCapacity;
92  };
93 
95  struct Proxy
96  {
97  int32 index;
98  uint32 tag;
99  friend inline bool operator<(const Proxy &a, const Proxy &b)
100  {
101  return a.tag < b.tag;
102  }
103  friend inline bool operator<(uint32 a, const Proxy &b)
104  {
105  return a < b.tag;
106  }
107  friend inline bool operator<(const Proxy &a, uint32 b)
108  {
109  return a.tag < b;
110  }
111  };
112 
114  struct Pair
115  {
116  int32 indexA, indexB;
117  uint32 flags;
118  float32 strength;
119  float32 distance;
120  };
121 
123  struct Triad
124  {
125  int32 indexA, indexB, indexC;
126  uint32 flags;
127  float32 strength;
128  b2Vec2 pa, pb, pc;
129  float32 ka, kb, kc, s;
130  };
131 
132  // Callback used with b2VoronoiDiagram.
133  class CreateParticleGroupCallback
134  {
135  public:
136  void operator()(int32 a, int32 b, int32 c) const;
137  b2ParticleSystem* system;
138  const b2ParticleGroupDef* def;
139  int32 firstIndex;
140  };
141 
142  // Callback used with b2VoronoiDiagram.
143  class JoinParticleGroupsCallback
144  {
145  public:
146  void operator()(int32 a, int32 b, int32 c) const;
147  b2ParticleSystem* system;
148  b2ParticleGroup* groupA;
149  b2ParticleGroup* groupB;
150  };
151 
153  static const int32 k_pairFlags =
154  b2_springParticle;
156  static const int32 k_triadFlags =
157  b2_elasticParticle;
159  static const int32 k_noPressureFlags =
160  b2_powderParticle;
161 
163  ~b2ParticleSystem();
164 
165  template <typename T> T* ReallocateBuffer(T* buffer, int32 oldCapacity, int32 newCapacity);
166  template <typename T> T* ReallocateBuffer(T* buffer, int32 userSuppliedCapacity, int32 oldCapacity, int32 newCapacity, bool deferred);
167  template <typename T> T* ReallocateBuffer(ParticleBuffer<T>* buffer, int32 oldCapacity, int32 newCapacity, bool deferred);
168  template <typename T> T* RequestParticleBuffer(T* buffer);
169 
170  int32 CreateParticle(const b2ParticleDef& def);
171  void DestroyParticle(int32 index, bool callDestructionListener);
172  // Destroy particles in the specified shape with the transform xf applied
173  // optionally calling the destruction listener for particles that are
174  // destroyed.
175  int32 DestroyParticlesInShape(const b2Shape& shape, const b2Transform& xf,
176  bool callDestructionListener);
177  void DestroyParticlesInGroup(b2ParticleGroup* group,
178  bool callDestructionListener);
179  b2ParticleGroup* CreateParticleGroup(const b2ParticleGroupDef& def);
180  void JoinParticleGroups(b2ParticleGroup* groupA, b2ParticleGroup* groupB);
181  void DestroyParticleGroup(b2ParticleGroup* group);
182  void ComputeDepthForGroup(b2ParticleGroup* group);
183 
184  void AddContact(int32 a, int32 b);
185  void UpdateContacts(bool exceptZombie);
186  void UpdateBodyContacts();
187 
188  void Solve(const b2TimeStep& step);
189  void SolveCollision(const b2TimeStep& step);
190  void SolvePressure(const b2TimeStep& step);
191  void SolveDamping(const b2TimeStep& step);
192  void SolveWall(const b2TimeStep& step);
193  void SolveRigid(const b2TimeStep& step);
194  void SolveElastic(const b2TimeStep& step);
195  void SolveSpring(const b2TimeStep& step);
196  void SolveTensile(const b2TimeStep& step);
197  void SolveViscous(const b2TimeStep& step);
198  void SolvePowder(const b2TimeStep& step);
199  void SolveSolid(const b2TimeStep& step);
200  void SolveColorMixing(const b2TimeStep& step);
201  void SolveZombie();
202  void RotateBuffer(int32 start, int32 mid, int32 end);
203 
204  void SetParticleRadius(float32 radius);
205  float32 GetParticleRadius() const;
206  void SetParticleDensity(float32 density);
207  float32 GetParticleDensity() const;
208  void SetParticleGravityScale(float32 gravityScale);
209  float32 GetParticleGravityScale() const;
210  void SetParticleDamping(float32 damping);
211  float32 GetParticleDamping() const;
212  float32 GetCriticalVelocity(const b2TimeStep& step) const;
213  float32 GetCriticalVelocitySquared(const b2TimeStep& step) const;
214  float32 GetCriticalPressure(const b2TimeStep& step) const;
215  float32 GetParticleStride() const;
216  float32 GetParticleMass() const;
217  float32 GetParticleInvMass() const;
218 
219  b2ParticleGroup* GetParticleGroupList();
220  const b2ParticleGroup* GetParticleGroupList() const;
221  int32 GetParticleGroupCount() const;
222  int32 GetParticleCount() const;
223  int32 GetParticleMaxCount() const;
224  void SetParticleMaxCount(int32 count);
225  uint32* GetParticleFlagsBuffer();
226  b2Vec2* GetParticlePositionBuffer();
227  b2Vec2* GetParticleVelocityBuffer();
228  b2ParticleColor* GetParticleColorBuffer();
229  void** GetParticleUserDataBuffer();
230  const uint32* GetParticleFlagsBuffer() const;
231  const b2Vec2* GetParticlePositionBuffer() const;
232  const b2Vec2* GetParticleVelocityBuffer() const;
233  const b2ParticleColor* GetParticleColorBuffer() const;
234  const b2ParticleGroup* const* GetParticleGroupBuffer() const;
235  void* const* GetParticleUserDataBuffer() const;
236  template <typename T> void SetParticleBuffer(ParticleBuffer<T>* buffer, T* newBufferData, int32 newCapacity);
237  void SetParticleFlagsBuffer(uint32* buffer, int32 capacity);
238  void SetParticlePositionBuffer(b2Vec2* buffer, int32 capacity);
239  void SetParticleVelocityBuffer(b2Vec2* buffer, int32 capacity);
240  void SetParticleColorBuffer(b2ParticleColor* buffer, int32 capacity);
241  b2ParticleGroup* const* GetParticleGroupBuffer();
242  void SetParticleUserDataBuffer(void** buffer, int32 capacity);
243 
244  void QueryAABB(b2QueryCallback* callback, const b2AABB& aabb) const;
245  void RayCast(b2RayCastCallback* callback, const b2Vec2& point1, const b2Vec2& point2) const;
246  float32 ComputeParticleCollisionEnergy() const;
247 
248  int32 m_timestamp;
249  int32 m_allParticleFlags;
250  int32 m_allGroupFlags;
251  float32 m_density;
252  float32 m_inverseDensity;
253  float32 m_gravityScale;
254  float32 m_particleDiameter;
255  float32 m_inverseDiameter;
256  float32 m_squaredDiameter;
257 
258  int32 m_count;
259  int32 m_internalAllocatedCapacity;
260  int32 m_maxCount;
261  ParticleBuffer<uint32> m_flagsBuffer;
262  ParticleBuffer<b2Vec2> m_positionBuffer;
263  ParticleBuffer<b2Vec2> m_velocityBuffer;
264  float32* m_accumulationBuffer; // temporary values
265  b2Vec2* m_accumulation2Buffer; // temporary vector values
266  float32* m_depthBuffer; // distance from the surface
267  ParticleBuffer<b2ParticleColor> m_colorBuffer;
268  b2ParticleGroup** m_groupBuffer;
269  ParticleBuffer<void*> m_userDataBuffer;
270 
271  int32 m_proxyCount;
272  int32 m_proxyCapacity;
273  Proxy* m_proxyBuffer;
274 
275  int32 m_contactCount;
276  int32 m_contactCapacity;
277  b2ParticleContact* m_contactBuffer;
278 
279  int32 m_bodyContactCount;
280  int32 m_bodyContactCapacity;
281  b2ParticleBodyContact* m_bodyContactBuffer;
282 
283  int32 m_pairCount;
284  int32 m_pairCapacity;
285  Pair* m_pairBuffer;
286 
287  int32 m_triadCount;
288  int32 m_triadCapacity;
289  Triad* m_triadBuffer;
290 
291  int32 m_groupCount;
292  b2ParticleGroup* m_groupList;
293 
294  float32 m_pressureStrength;
295  float32 m_dampingStrength;
296  float32 m_elasticStrength;
297  float32 m_springStrength;
298  float32 m_viscousStrength;
299  float32 m_surfaceTensionStrengthA;
300  float32 m_surfaceTensionStrengthB;
301  float32 m_powderStrength;
302  float32 m_ejectionStrength;
303  float32 m_colorMixingStrength;
304 
305  b2World* m_world;
306 };
307 
308 inline b2ParticleGroup* b2ParticleSystem::GetParticleGroupList()
309 {
310  return m_groupList;
311 }
312 
313 inline const b2ParticleGroup* b2ParticleSystem::GetParticleGroupList() const
314 {
315  return m_groupList;
316 }
317 
318 inline int32 b2ParticleSystem::GetParticleGroupCount() const
319 {
320  return m_groupCount;
321 }
322 
323 inline int32 b2ParticleSystem::GetParticleCount() const
324 {
325  return m_count;
326 }
327 
328 #endif
Definition: b2Math.h:361
Definition: b2ParticleGroup.h:37
uint32 flags
Definition: b2ParticleSystem.h:46
float32 weight
Definition: b2ParticleSystem.h:49
Definition: b2WorldCallbacks.h:160
float32 weight
Definition: b2ParticleSystem.h:65
Definition: b2StackAllocator.h:37
b2Vec2 normal
Definition: b2ParticleSystem.h:52
Definition: b2World.h:44
Definition: b2ParticleSystem.h:74
Definition: b2BlockAllocator.h:35
Definition: b2Shape.h:43
int32 index
Definition: b2ParticleSystem.h:59
Definition: b2Particle.h:85
b2Vec2 normal
Definition: b2ParticleSystem.h:68
float32 mass
Definition: b2ParticleSystem.h:71
A group of particles. These are created via b2World::CreateParticleGroup.
Definition: b2ParticleGroup.h:93
An axis aligned bounding box.
Definition: b2Collision.h:161
int32 indexA
Definition: b2ParticleSystem.h:43
This is an internal structure.
Definition: b2TimeStep.h:38
Definition: b2ParticleSystem.h:55
A rigid body. These are created via b2World::CreateBody.
Definition: b2Body.h:127
b2Body * body
Definition: b2ParticleSystem.h:62
Small color object for each particle.
Definition: b2Particle.h:42
Definition: b2ParticleSystem.h:39
A 2D column vector.
Definition: b2Math.h:64
Definition: b2WorldCallbacks.h:140