CCPUForceField.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CC_PU_PARTICLE_3D_FORCE_FIELD_H__
  23. #define __CC_PU_PARTICLE_3D_FORCE_FIELD_H__
  24. #include "base/CCRef.h"
  25. #include "math/CCMath.h"
  26. #include "CCPUNoise.h"
  27. NS_CC_BEGIN
  28. /** Pure virtual ForceFieldCalculationFactory
  29. */
  30. class PUForceFieldCalculationFactory
  31. {
  32. public:
  33. // Const
  34. static const Vec3 DEFAULT_WORLDSIZE;
  35. PUForceFieldCalculationFactory(void) :
  36. _octaves(2),
  37. _frequency(1.0f),
  38. _amplitude(1.0f),
  39. _persistence(1.0f),
  40. _worldSize(DEFAULT_WORLDSIZE)
  41. {
  42. }
  43. virtual ~PUForceFieldCalculationFactory(void){};
  44. /** Generates the force field
  45. @remarks
  46. This function takes several arguments.
  47. - forceFieldSize defines the internal dimensions of the force field.
  48. - octaves, frequency, amplitude and persistence define the noise that is being generated.
  49. - worldSize defines the dimensions in the real world (scene).
  50. */
  51. virtual void generate(unsigned int forceFieldSize,
  52. unsigned short octaves,
  53. double frequency,
  54. double amplitude,
  55. double persistence,
  56. const Vec3& worldSize) = 0;
  57. /** Determine force, based on the position of a particle.
  58. #remarks
  59. The position is the position of a particle. The argument 'delta' defines the radius around the position that contributes to the
  60. calculation of the force.
  61. */
  62. virtual void determineForce(const Vec3& position, Vec3& force, float delta) = 0;
  63. /** Default Getters/Setters
  64. */
  65. virtual unsigned short getOctaves(void) const;
  66. virtual void setOctaves(unsigned short octaves);
  67. virtual double getFrequency(void) const;
  68. virtual void setFrequency(double frequency);
  69. virtual double getAmplitude(void) const;
  70. virtual void setAmplitude(double amplitude);
  71. virtual double getPersistence(void) const;
  72. virtual void setPersistence(double persistence);
  73. virtual unsigned int getForceFieldSize(void) const;
  74. virtual void setForceFieldSize(unsigned int forceFieldSize);
  75. virtual Vec3 getWorldSize(void) const;
  76. virtual void setWorldSize(const Vec3& worldSize);
  77. protected:
  78. PUNoise3D _noise3D;
  79. unsigned short _octaves;
  80. double _frequency;
  81. double _amplitude;
  82. double _persistence;
  83. Vec3 _mapScale;
  84. Vec3 _worldSize;
  85. Vec3 _mappedPosition;
  86. };
  87. //-------------------------------------------------------------------------
  88. //-------------------------------------------------------------------------
  89. //-------------------------------------------------------------------------
  90. /** Factory class to calculate forces in realtime
  91. */
  92. class PURealTimeForceFieldCalculationFactory : public PUForceFieldCalculationFactory
  93. {
  94. public:
  95. PURealTimeForceFieldCalculationFactory(void) : PUForceFieldCalculationFactory() {};
  96. virtual ~PURealTimeForceFieldCalculationFactory(void){};
  97. /** Override from ForceFieldCalculationFactory
  98. */
  99. virtual void generate(unsigned int forceFieldSize,
  100. unsigned short octaves,
  101. double frequency,
  102. double amplitude,
  103. double persistence,
  104. const Vec3& worldSize);
  105. /** Override from ForceFieldCalculationFactory
  106. */
  107. virtual void determineForce(const Vec3& position, Vec3& force, float delta);
  108. };
  109. //-------------------------------------------------------------------------
  110. //-------------------------------------------------------------------------
  111. //-------------------------------------------------------------------------
  112. /** Force Field Affector Class:
  113. This class defines a force field to affect the particle direction. The force field is based on 3D noise. The force can be calculated in
  114. realtime or based on a precreated 3D force field matrix, which essentially involves one lookup. To speed things up, the 3d matrix can be
  115. precreated in a separate thread (optionally).
  116. */
  117. class PUForceField
  118. {
  119. public:
  120. enum ForceFieldType
  121. {
  122. FF_REALTIME_CALC,
  123. FF_MATRIX_CALC
  124. };
  125. PUForceField();
  126. virtual ~PUForceField();
  127. /** Initialises a ForceField */
  128. virtual void initialise(ForceFieldType type,
  129. const Vec3& position,
  130. unsigned int forceFieldSize,
  131. unsigned short octaves,
  132. double frequency,
  133. double amplitude,
  134. double persistence,
  135. const Vec3& worldSize);
  136. /** Initialises a ForceField */
  137. virtual void initialise(ForceFieldType type,
  138. unsigned int forceFieldSize,
  139. unsigned short octaves,
  140. double frequency,
  141. double amplitude,
  142. double persistence,
  143. const Vec3& worldSize);
  144. /** Get/Set the base position of the force field */
  145. const Vec3& getForceFieldPositionBase(void) const;
  146. void setForceFieldPositionBase(const Vec3& position);
  147. /** Calculate the force, based on a certain position */
  148. void determineForce(const Vec3& position, Vec3& force, float delta);
  149. /** Getters/Setters
  150. */
  151. virtual unsigned short getOctaves(void) const;
  152. virtual void setOctaves(unsigned short octaves);
  153. virtual double getFrequency(void) const;
  154. virtual void setFrequency(double frequency);
  155. virtual double getAmplitude(void) const;
  156. virtual void setAmplitude(double amplitude);
  157. virtual double getPersistence(void) const;
  158. virtual void setPersistence(double persistence);
  159. virtual unsigned int getForceFieldSize(void) const;
  160. virtual void setForceFieldSize(unsigned int forceFieldSize);
  161. virtual Vec3 getWorldSize(void) const;
  162. virtual void setWorldSize(const Vec3& worldSize);
  163. /** Get/Set the Forcefield type
  164. */
  165. ForceFieldType getForceFieldType() const;
  166. void setForceFieldType(const ForceFieldType forceFieldType);
  167. protected:
  168. unsigned short _octaves;
  169. double _frequency;
  170. double _amplitude;
  171. double _persistence;
  172. Vec3 _worldSize;
  173. unsigned int _forceFieldSize;
  174. PUForceFieldCalculationFactory* _forceFieldCalculationFactory; // Internal factory that creates a certain force field type
  175. Vec3 _forceFieldPositionBase; // Position of the force field
  176. ForceFieldType _forceFieldType;
  177. /** Get/Set/Create ForceFieldCalculationFactory */
  178. PUForceFieldCalculationFactory* getForceFieldCalculationFactory() const;
  179. void setForceFieldCalculationFactory(PUForceFieldCalculationFactory* forceFieldCalculationFactory);
  180. PUForceFieldCalculationFactory* createForceFieldCalculationFactory(ForceFieldType type);
  181. };
  182. //-------------------------------------------------------------------------
  183. //-------------------------------------------------------------------------
  184. //-------------------------------------------------------------------------
  185. NS_CC_END
  186. #endif