b2ParticleGroup.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #include <Box2D/Particle/b2Particle.h>
  21. class b2Shape;
  22. class b2World;
  23. class b2ParticleSystem;
  24. class b2ParticleGroup;
  25. class b2ParticleColor;
  26. #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
  27. class b2CircleShape;
  28. #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
  29. /// @file
  30. /// The particle group type. Can be combined with the | operator.
  31. enum b2ParticleGroupFlag
  32. {
  33. /// Prevents overlapping or leaking.
  34. b2_solidParticleGroup = 1 << 0,
  35. /// Keeps its shape.
  36. b2_rigidParticleGroup = 1 << 1,
  37. /// Won't be destroyed if it gets empty.
  38. b2_particleGroupCanBeEmpty = 1 << 2,
  39. /// Will be destroyed on next simulation step.
  40. b2_particleGroupWillBeDestroyed = 1 << 3,
  41. /// Updates depth data on next simulation step.
  42. b2_particleGroupNeedsUpdateDepth = 1 << 4,
  43. b2_particleGroupInternalMask =
  44. b2_particleGroupWillBeDestroyed |
  45. b2_particleGroupNeedsUpdateDepth,
  46. };
  47. /// A particle group definition holds all the data needed to construct a
  48. /// particle group. You can safely re-use these definitions.
  49. struct b2ParticleGroupDef
  50. {
  51. b2ParticleGroupDef()
  52. {
  53. flags = 0;
  54. groupFlags = 0;
  55. position = b2Vec2_zero;
  56. angle = 0;
  57. linearVelocity = b2Vec2_zero;
  58. angularVelocity = 0;
  59. color = b2ParticleColor_zero;
  60. strength = 1;
  61. shape = NULL;
  62. shapes = NULL;
  63. shapeCount = 0;
  64. stride = 0;
  65. particleCount = 0;
  66. positionData = NULL;
  67. lifetime = 0.0f;
  68. userData = NULL;
  69. group = NULL;
  70. #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
  71. circleShapes = NULL;
  72. ownShapesArray = false;
  73. #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
  74. }
  75. ~b2ParticleGroupDef()
  76. {
  77. #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
  78. FreeShapesMemory();
  79. #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
  80. }
  81. /// The particle-behavior flags (See #b2ParticleFlag).
  82. uint32 flags;
  83. /// The group-construction flags (See #b2ParticleGroupFlag).
  84. uint32 groupFlags;
  85. /// The world position of the group.
  86. /// Moves the group's shape a distance equal to the value of position.
  87. b2Vec2 position;
  88. /// The world angle of the group in radians.
  89. /// Rotates the shape by an angle equal to the value of angle.
  90. float32 angle;
  91. /// The linear velocity of the group's origin in world co-ordinates.
  92. b2Vec2 linearVelocity;
  93. /// The angular velocity of the group.
  94. float32 angularVelocity;
  95. /// The color of all particles in the group.
  96. b2ParticleColor color;
  97. /// The strength of cohesion among the particles in a group with flag
  98. /// b2_elasticParticle or b2_springParticle.
  99. float32 strength;
  100. /// The shape where particles will be added.
  101. const b2Shape* shape;
  102. /// A array of shapes where particles will be added.
  103. const b2Shape* const* shapes;
  104. /// The number of shapes.
  105. int32 shapeCount;
  106. /// The interval of particles in the shape.
  107. /// If it is 0, b2_particleStride * particleDiameter is used instead.
  108. float32 stride;
  109. /// The number of particles in addition to ones added in the shape.
  110. int32 particleCount;
  111. /// The initial positions of the particleCount particles.
  112. const b2Vec2* positionData;
  113. /// Lifetime of the particle group in seconds. A value <= 0.0f indicates a
  114. /// particle group with infinite lifetime.
  115. float32 lifetime;
  116. /// Use this to store application-specific group data.
  117. void* userData;
  118. /// An existing particle group to which the particles will be added.
  119. b2ParticleGroup* group;
  120. #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
  121. /// Storage for constructed CircleShapes from an incoming vertex list
  122. const b2CircleShape* circleShapes;
  123. /// True if we create the shapes array internally.
  124. bool ownShapesArray;
  125. /// Clean up all memory associated with SetCircleShapesFromVertexList
  126. void FreeShapesMemory();
  127. /// From a vertex list created by an external language API, construct
  128. /// a list of circle shapes that can be used to create a b2ParticleGroup
  129. /// This eliminates cumbersome array-interfaces between languages.
  130. void SetCircleShapesFromVertexList(void* inBuf,
  131. int numShapes,
  132. float radius);
  133. /// Set position with direct floats.
  134. void SetPosition(float32 x, float32 y);
  135. /// Set color with direct ints.
  136. void SetColor(int32 r, int32 g, int32 b, int32 a);
  137. #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
  138. };
  139. /// A group of particles. b2ParticleGroup::CreateParticleGroup creates these.
  140. class b2ParticleGroup
  141. {
  142. public:
  143. /// Get the next particle group from the list in b2_World.
  144. b2ParticleGroup* GetNext();
  145. const b2ParticleGroup* GetNext() const;
  146. /// Get the particle system that holds this particle group.
  147. b2ParticleSystem* GetParticleSystem();
  148. const b2ParticleSystem* GetParticleSystem() const;
  149. /// Get the number of particles.
  150. int32 GetParticleCount() const;
  151. /// Get the offset of this group in the global particle buffer
  152. int32 GetBufferIndex() const;
  153. /// Does this group contain the particle.
  154. bool ContainsParticle(int32 index) const;
  155. /// Get the logical sum of particle flags.
  156. uint32 GetAllParticleFlags() const;
  157. /// Get the construction flags for the group.
  158. uint32 GetGroupFlags() const;
  159. /// Set the construction flags for the group.
  160. void SetGroupFlags(uint32 flags);
  161. /// Get the total mass of the group: the sum of all particles in it.
  162. float32 GetMass() const;
  163. /// Get the moment of inertia for the group.
  164. float32 GetInertia() const;
  165. /// Get the center of gravity for the group.
  166. b2Vec2 GetCenter() const;
  167. /// Get the linear velocity of the group.
  168. b2Vec2 GetLinearVelocity() const;
  169. /// Get the angular velocity of the group.
  170. float32 GetAngularVelocity() const;
  171. /// Get the position of the group's origin and rotation.
  172. /// Used only with groups of rigid particles.
  173. const b2Transform& GetTransform() const;
  174. /// Get position of the particle group as a whole.
  175. /// Used only with groups of rigid particles.
  176. const b2Vec2& GetPosition() const;
  177. /// Get the rotational angle of the particle group as a whole.
  178. /// Used only with groups of rigid particles.
  179. float32 GetAngle() const;
  180. /// Get the world linear velocity of a world point, from the average linear
  181. /// and angular velocities of the particle group.
  182. /// @param a point in world coordinates.
  183. /// @return the world velocity of a point.
  184. b2Vec2 GetLinearVelocityFromWorldPoint(const b2Vec2& worldPoint) const;
  185. /// Get the user data pointer that was provided in the group definition.
  186. void* GetUserData() const;
  187. /// Set the user data. Use this to store your application specific data.
  188. void SetUserData(void* data);
  189. /// Call b2ParticleSystem::ApplyForce for every particle in the group.
  190. void ApplyForce(const b2Vec2& force);
  191. /// Call b2ParticleSystem::ApplyLinearImpulse for every particle in the
  192. /// group.
  193. void ApplyLinearImpulse(const b2Vec2& impulse);
  194. /// Destroy all the particles in this group.
  195. /// This function is locked during callbacks.
  196. /// @param Whether to call the world b2DestructionListener for each
  197. /// particle is destroyed.
  198. /// @warning This function is locked during callbacks.
  199. void DestroyParticles(bool callDestructionListener);
  200. /// Destroy all particles in this group without enabling the destruction
  201. /// callback for destroyed particles.
  202. /// This function is locked during callbacks.
  203. /// @warning This function is locked during callbacks.
  204. void DestroyParticles();
  205. private:
  206. friend class b2ParticleSystem;
  207. b2ParticleSystem* m_system;
  208. int32 m_firstIndex, m_lastIndex;
  209. uint32 m_groupFlags;
  210. float32 m_strength;
  211. b2ParticleGroup* m_prev;
  212. b2ParticleGroup* m_next;
  213. mutable int32 m_timestamp;
  214. mutable float32 m_mass;
  215. mutable float32 m_inertia;
  216. mutable b2Vec2 m_center;
  217. mutable b2Vec2 m_linearVelocity;
  218. mutable float32 m_angularVelocity;
  219. mutable b2Transform m_transform;
  220. void* m_userData;
  221. b2ParticleGroup();
  222. ~b2ParticleGroup();
  223. void UpdateStatistics() const;
  224. };
  225. inline b2ParticleGroup* b2ParticleGroup::GetNext()
  226. {
  227. return m_next;
  228. }
  229. inline const b2ParticleGroup* b2ParticleGroup::GetNext() const
  230. {
  231. return m_next;
  232. }
  233. inline b2ParticleSystem* b2ParticleGroup::GetParticleSystem()
  234. {
  235. return m_system;
  236. }
  237. inline const b2ParticleSystem* b2ParticleGroup::GetParticleSystem() const
  238. {
  239. return m_system;
  240. }
  241. inline int32 b2ParticleGroup::GetParticleCount() const
  242. {
  243. return m_lastIndex - m_firstIndex;
  244. }
  245. inline bool b2ParticleGroup::ContainsParticle(int32 index) const
  246. {
  247. return m_firstIndex <= index && index < m_lastIndex;
  248. }
  249. inline b2ParticleGroup::~b2ParticleGroup()
  250. {
  251. }
  252. inline int32 b2ParticleGroup::GetBufferIndex() const
  253. {
  254. return m_firstIndex;
  255. }
  256. inline uint32 b2ParticleGroup::GetGroupFlags() const
  257. {
  258. return m_groupFlags & ~b2_particleGroupInternalMask;
  259. }
  260. inline float32 b2ParticleGroup::GetMass() const
  261. {
  262. UpdateStatistics();
  263. return m_mass;
  264. }
  265. inline float32 b2ParticleGroup::GetInertia() const
  266. {
  267. UpdateStatistics();
  268. return m_inertia;
  269. }
  270. inline b2Vec2 b2ParticleGroup::GetCenter() const
  271. {
  272. UpdateStatistics();
  273. return m_center;
  274. }
  275. inline b2Vec2 b2ParticleGroup::GetLinearVelocity() const
  276. {
  277. UpdateStatistics();
  278. return m_linearVelocity;
  279. }
  280. inline float32 b2ParticleGroup::GetAngularVelocity() const
  281. {
  282. UpdateStatistics();
  283. return m_angularVelocity;
  284. }
  285. inline const b2Transform& b2ParticleGroup::GetTransform() const
  286. {
  287. return m_transform;
  288. }
  289. inline const b2Vec2& b2ParticleGroup::GetPosition() const
  290. {
  291. return m_transform.p;
  292. }
  293. inline float32 b2ParticleGroup::GetAngle() const
  294. {
  295. return m_transform.q.GetAngle();
  296. }
  297. inline b2Vec2 b2ParticleGroup::GetLinearVelocityFromWorldPoint(
  298. const b2Vec2& worldPoint) const
  299. {
  300. UpdateStatistics();
  301. return m_linearVelocity + b2Cross(m_angularVelocity, worldPoint - m_center);
  302. }
  303. inline void* b2ParticleGroup::GetUserData() const
  304. {
  305. return m_userData;
  306. }
  307. inline void b2ParticleGroup::SetUserData(void* data)
  308. {
  309. m_userData = data;
  310. }
  311. inline void b2ParticleGroup::DestroyParticles()
  312. {
  313. DestroyParticles(false);
  314. }
  315. #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
  316. inline void b2ParticleGroupDef::SetPosition(float32 x, float32 y)
  317. {
  318. position.Set(x, y);
  319. }
  320. inline void b2ParticleGroupDef::SetColor(int32 r, int32 g, int32 b, int32 a)
  321. {
  322. color.Set((uint8)r, (uint8)g, (uint8)b, (uint8)a);
  323. }
  324. #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
  325. #endif