CCPUForceFieldAffector.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. #include "CCPUForceFieldAffector.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. NS_CC_BEGIN
  25. //-----------------------------------------------------------------------
  26. // Constants
  27. const PUForceField::ForceFieldType PUForceFieldAffector::DEFAULT_FORCEFIELD_TYPE = PUForceField::FF_REALTIME_CALC;
  28. const float PUForceFieldAffector::DEFAULT_DELTA = 1.0f;
  29. const float PUForceFieldAffector::DEFAULT_FORCE = 400.0f;
  30. const unsigned short PUForceFieldAffector::DEFAULT_OCTAVES = 2;
  31. const double PUForceFieldAffector::DEFAULT_FREQUENCY = 1.0f;
  32. const double PUForceFieldAffector::DEFAULT_AMPLITUDE = 1.0f;
  33. const double PUForceFieldAffector::DEFAULT_PERSISTENCE = 3.0f;
  34. const unsigned int PUForceFieldAffector::DEFAULT_FORCEFIELDSIZE = 64;
  35. const Vec3 PUForceFieldAffector::DEFAULT_WORLDSIZE(500.0f, 500.0f, 500.0f);
  36. const Vec3 PUForceFieldAffector::DEFAULT_MOVEMENT(500.0f, 0.0f, 0.0f);
  37. const float PUForceFieldAffector::DEFAULT_MOVEMENT_FREQUENCY = 5.0f;
  38. //-----------------------------------------------------------------------
  39. PUForceFieldAffector::PUForceFieldAffector() :
  40. _forceFieldType(PUForceField::FF_REALTIME_CALC),
  41. _delta(DEFAULT_DELTA),
  42. _scaleForce(DEFAULT_FORCE),
  43. _octaves(DEFAULT_OCTAVES),
  44. _frequency(DEFAULT_FREQUENCY),
  45. _amplitude(DEFAULT_AMPLITUDE),
  46. _persistence(DEFAULT_PERSISTENCE),
  47. _forceFieldSize(DEFAULT_FORCEFIELDSIZE),
  48. _worldSize(DEFAULT_WORLDSIZE),
  49. _prepared(true),
  50. _ignoreNegativeX(false),
  51. _ignoreNegativeY(false),
  52. _ignoreNegativeZ(false),
  53. _movement(DEFAULT_MOVEMENT),
  54. _movementSet(false),
  55. _movementFrequency(DEFAULT_MOVEMENT_FREQUENCY),
  56. _movementFrequencyCount(0.0f),
  57. _suppressGeneration(false)
  58. {
  59. };
  60. //-----------------------------------------------------------------------
  61. PUForceFieldAffector::~PUForceFieldAffector()
  62. {
  63. };
  64. //-----------------------------------------------------------------------
  65. PUForceField::ForceFieldType PUForceFieldAffector::getForceFieldType() const
  66. {
  67. return _forceFieldType;
  68. }
  69. //-----------------------------------------------------------------------
  70. void PUForceFieldAffector::setForceFieldType(const PUForceField::ForceFieldType forceFieldType)
  71. {
  72. _forceFieldType = forceFieldType;
  73. if (_suppressGeneration)
  74. return;
  75. _forceField.setForceFieldType(forceFieldType);
  76. }
  77. //-----------------------------------------------------------------------
  78. float PUForceFieldAffector::getDelta(void) const
  79. {
  80. return _delta;
  81. }
  82. //-----------------------------------------------------------------------
  83. void PUForceFieldAffector::setDelta(float delta)
  84. {
  85. _delta = delta;
  86. }
  87. //-----------------------------------------------------------------------
  88. float PUForceFieldAffector::getScaleForce(void) const
  89. {
  90. return _scaleForce;
  91. }
  92. //-----------------------------------------------------------------------
  93. void PUForceFieldAffector::setScaleForce(float scaleForce)
  94. {
  95. _scaleForce = scaleForce;
  96. }
  97. //-----------------------------------------------------------------------
  98. unsigned short PUForceFieldAffector::getOctaves(void) const
  99. {
  100. return _octaves;
  101. }
  102. //-----------------------------------------------------------------------
  103. void PUForceFieldAffector::setOctaves(unsigned short octaves)
  104. {
  105. _octaves = octaves;
  106. if (_suppressGeneration)
  107. return;
  108. _forceField.setOctaves(octaves);
  109. }
  110. //-----------------------------------------------------------------------
  111. double PUForceFieldAffector::getFrequency(void) const
  112. {
  113. return _frequency;
  114. }
  115. //-----------------------------------------------------------------------
  116. void PUForceFieldAffector::setFrequency(double frequency)
  117. {
  118. _frequency = frequency;
  119. if (_suppressGeneration)
  120. return;
  121. _forceField.setFrequency(frequency);
  122. }
  123. //-----------------------------------------------------------------------
  124. double PUForceFieldAffector::getAmplitude(void) const
  125. {
  126. return _amplitude;
  127. }
  128. //-----------------------------------------------------------------------
  129. void PUForceFieldAffector::setAmplitude(double amplitude)
  130. {
  131. _amplitude = amplitude;
  132. if (_suppressGeneration)
  133. return;
  134. _forceField.setAmplitude(amplitude);
  135. }
  136. //-----------------------------------------------------------------------
  137. double PUForceFieldAffector::getPersistence(void) const
  138. {
  139. return _persistence;
  140. }
  141. //-----------------------------------------------------------------------
  142. void PUForceFieldAffector::setPersistence(double persistence)
  143. {
  144. _persistence = persistence;
  145. if (_suppressGeneration)
  146. return;
  147. _forceField.setPersistence(persistence);
  148. }
  149. //-----------------------------------------------------------------------
  150. unsigned int PUForceFieldAffector::getForceFieldSize(void) const
  151. {
  152. return _forceFieldSize;
  153. }
  154. //-----------------------------------------------------------------------
  155. void PUForceFieldAffector::setForceFieldSize(unsigned int forceFieldSize)
  156. {
  157. _forceFieldSize = forceFieldSize;
  158. if (_suppressGeneration)
  159. return;
  160. _forceField.setForceFieldSize(forceFieldSize);
  161. }
  162. //-----------------------------------------------------------------------
  163. Vec3 PUForceFieldAffector::getWorldSize(void) const
  164. {
  165. return _worldSize;
  166. }
  167. //-----------------------------------------------------------------------
  168. void PUForceFieldAffector::setWorldSize(const Vec3& worldSize)
  169. {
  170. _worldSize = worldSize;
  171. if (_suppressGeneration)
  172. return;
  173. _forceField.setWorldSize(worldSize);
  174. }
  175. //-----------------------------------------------------------------------
  176. bool PUForceFieldAffector::getIgnoreNegativeX(void) const
  177. {
  178. return _ignoreNegativeX;
  179. }
  180. //-----------------------------------------------------------------------
  181. void PUForceFieldAffector::setIgnoreNegativeX(bool ignoreNegativeX)
  182. {
  183. _ignoreNegativeX = ignoreNegativeX;
  184. }
  185. //-----------------------------------------------------------------------
  186. bool PUForceFieldAffector::getIgnoreNegativeY(void) const
  187. {
  188. return _ignoreNegativeY;
  189. }
  190. //-----------------------------------------------------------------------
  191. void PUForceFieldAffector::setIgnoreNegativeY(bool ignoreNegativeY)
  192. {
  193. _ignoreNegativeY = ignoreNegativeY;
  194. }
  195. //-----------------------------------------------------------------------
  196. bool PUForceFieldAffector::getIgnoreNegativeZ(void) const
  197. {
  198. return _ignoreNegativeZ;
  199. }
  200. //-----------------------------------------------------------------------
  201. void PUForceFieldAffector::setIgnoreNegativeZ(bool ignoreNegativeZ)
  202. {
  203. _ignoreNegativeZ = ignoreNegativeZ;
  204. }
  205. //-----------------------------------------------------------------------
  206. float PUForceFieldAffector::getMovementFrequency(void) const
  207. {
  208. return _movementFrequency;
  209. }
  210. //-----------------------------------------------------------------------
  211. void PUForceFieldAffector::setMovementFrequency(float movementFrequency)
  212. {
  213. _movementFrequency = movementFrequency;
  214. _movementSet = (movementFrequency > 0.0f);
  215. }
  216. //-----------------------------------------------------------------------
  217. const Vec3& PUForceFieldAffector::getMovement(void) const
  218. {
  219. return _movement;
  220. }
  221. //-----------------------------------------------------------------------
  222. void PUForceFieldAffector::setMovement(const Vec3& movement)
  223. {
  224. _movement = movement;
  225. _movementSet = (movement != Vec3::ZERO);
  226. }
  227. //-----------------------------------------------------------------------
  228. void PUForceFieldAffector::suppressGeneration(bool suppress)
  229. {
  230. _suppressGeneration = suppress;
  231. }
  232. void PUForceFieldAffector::updatePUAffector( PUParticle3D *particle, float deltaTime )
  233. {
  234. //for (auto iter : _particleSystem->getParticles())
  235. {
  236. //PUParticle3D *particle = iter;
  237. _forceField.determineForce(particle->position, _force, _delta);
  238. // If negative values are ignored, set the force to 0.
  239. if (_ignoreNegativeX)
  240. {
  241. _force.x = 0.0f;
  242. }
  243. if (_ignoreNegativeY)
  244. {
  245. _force.y = 0.0f;
  246. }
  247. if (_ignoreNegativeZ)
  248. {
  249. _force.z = 0.0f;
  250. }
  251. particle->direction += deltaTime * _scaleForce * _force;
  252. }
  253. }
  254. void PUForceFieldAffector::notifyStart()
  255. {
  256. _movementFrequencyCount = 0.0f;
  257. }
  258. void PUForceFieldAffector::preUpdateAffector( float deltaTime )
  259. {
  260. if (_movementSet)
  261. {
  262. if (deltaTime > _movementFrequency)
  263. {
  264. // Ignore too large times, because it just blows things up
  265. return;
  266. }
  267. _movementFrequencyCount += deltaTime;
  268. if (_movementFrequencyCount > _movementFrequency)
  269. {
  270. _movementFrequencyCount -= _movementFrequency;
  271. }
  272. _displacement = sin(2.0f * M_PI * _movementFrequencyCount/_movementFrequency) * _movement;
  273. _forceField.setForceFieldPositionBase(_basePosition + _displacement);
  274. }
  275. }
  276. void PUForceFieldAffector::prepare()
  277. {
  278. //if (particleTechnique->getParentSystem())
  279. {
  280. // Forcefield position is same position as particle system position
  281. _forceField.initialise(_forceFieldType,
  282. // _particleSystem->getDerivedPosition()
  283. getDerivedPosition(),
  284. _forceFieldSize,
  285. _octaves,
  286. _frequency,
  287. _amplitude,
  288. _persistence,
  289. _worldSize);
  290. _basePosition = _forceField.getForceFieldPositionBase();
  291. _prepared = true;
  292. }
  293. }
  294. PUForceFieldAffector* PUForceFieldAffector::create()
  295. {
  296. auto pffa = new (std::nothrow) PUForceFieldAffector();
  297. pffa->autorelease();
  298. return pffa;
  299. }
  300. void PUForceFieldAffector::copyAttributesTo( PUAffector* affector )
  301. {
  302. PUAffector::copyAttributesTo(affector);
  303. PUForceFieldAffector* forceFieldAffector = static_cast<PUForceFieldAffector*>(affector);
  304. forceFieldAffector->_forceFieldType = _forceFieldType;
  305. forceFieldAffector->_delta = _delta;
  306. forceFieldAffector->_scaleForce = _scaleForce;
  307. forceFieldAffector->_octaves = _octaves;
  308. forceFieldAffector->_frequency = _frequency;
  309. forceFieldAffector->_amplitude = _amplitude;
  310. forceFieldAffector->_persistence = _persistence;
  311. forceFieldAffector->_forceFieldSize = _forceFieldSize;
  312. forceFieldAffector->_worldSize = _worldSize;
  313. forceFieldAffector->_ignoreNegativeX = _ignoreNegativeX;
  314. forceFieldAffector->_ignoreNegativeY = _ignoreNegativeY;
  315. forceFieldAffector->_ignoreNegativeZ = _ignoreNegativeZ;
  316. forceFieldAffector->_movementSet = _movementSet;
  317. forceFieldAffector->_movementFrequency = _movementFrequency;
  318. forceFieldAffector->_movement = _movement;
  319. }
  320. NS_CC_END