CCPUForceFieldAffector.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "CCPUForceFieldAffector.h"
  22. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  23. NS_CC_BEGIN
  24. //-----------------------------------------------------------------------
  25. // Constants
  26. const PUForceField::ForceFieldType PUForceFieldAffector::DEFAULT_FORCEFIELD_TYPE = PUForceField::FF_REALTIME_CALC;
  27. const float PUForceFieldAffector::DEFAULT_DELTA = 1.0f;
  28. const float PUForceFieldAffector::DEFAULT_FORCE = 400.0f;
  29. const unsigned short PUForceFieldAffector::DEFAULT_OCTAVES = 2;
  30. const double PUForceFieldAffector::DEFAULT_FREQUENCY = 1.0f;
  31. const double PUForceFieldAffector::DEFAULT_AMPLITUDE = 1.0f;
  32. const double PUForceFieldAffector::DEFAULT_PERSISTENCE = 3.0f;
  33. const unsigned int PUForceFieldAffector::DEFAULT_FORCEFIELDSIZE = 64;
  34. const Vec3 PUForceFieldAffector::DEFAULT_WORLDSIZE(500.0f, 500.0f, 500.0f);
  35. const Vec3 PUForceFieldAffector::DEFAULT_MOVEMENT(500.0f, 0.0f, 0.0f);
  36. const float PUForceFieldAffector::DEFAULT_MOVEMENT_FREQUENCY = 5.0f;
  37. //-----------------------------------------------------------------------
  38. PUForceFieldAffector::PUForceFieldAffector() :
  39. _forceFieldType(PUForceField::FF_REALTIME_CALC),
  40. _delta(DEFAULT_DELTA),
  41. _scaleForce(DEFAULT_FORCE),
  42. _octaves(DEFAULT_OCTAVES),
  43. _frequency(DEFAULT_FREQUENCY),
  44. _amplitude(DEFAULT_AMPLITUDE),
  45. _persistence(DEFAULT_PERSISTENCE),
  46. _forceFieldSize(DEFAULT_FORCEFIELDSIZE),
  47. _worldSize(DEFAULT_WORLDSIZE),
  48. _prepared(true),
  49. _ignoreNegativeX(false),
  50. _ignoreNegativeY(false),
  51. _ignoreNegativeZ(false),
  52. _movement(DEFAULT_MOVEMENT),
  53. _movementSet(false),
  54. _movementFrequency(DEFAULT_MOVEMENT_FREQUENCY),
  55. _movementFrequencyCount(0.0f),
  56. _suppressGeneration(false)
  57. {
  58. };
  59. //-----------------------------------------------------------------------
  60. PUForceFieldAffector::~PUForceFieldAffector()
  61. {
  62. };
  63. //-----------------------------------------------------------------------
  64. PUForceField::ForceFieldType PUForceFieldAffector::getForceFieldType() const
  65. {
  66. return _forceFieldType;
  67. }
  68. //-----------------------------------------------------------------------
  69. void PUForceFieldAffector::setForceFieldType(const PUForceField::ForceFieldType forceFieldType)
  70. {
  71. _forceFieldType = forceFieldType;
  72. if (_suppressGeneration)
  73. return;
  74. _forceField.setForceFieldType(forceFieldType);
  75. }
  76. //-----------------------------------------------------------------------
  77. float PUForceFieldAffector::getDelta(void) const
  78. {
  79. return _delta;
  80. }
  81. //-----------------------------------------------------------------------
  82. void PUForceFieldAffector::setDelta(float delta)
  83. {
  84. _delta = delta;
  85. }
  86. //-----------------------------------------------------------------------
  87. float PUForceFieldAffector::getScaleForce(void) const
  88. {
  89. return _scaleForce;
  90. }
  91. //-----------------------------------------------------------------------
  92. void PUForceFieldAffector::setScaleForce(float scaleForce)
  93. {
  94. _scaleForce = scaleForce;
  95. }
  96. //-----------------------------------------------------------------------
  97. unsigned short PUForceFieldAffector::getOctaves(void) const
  98. {
  99. return _octaves;
  100. }
  101. //-----------------------------------------------------------------------
  102. void PUForceFieldAffector::setOctaves(unsigned short octaves)
  103. {
  104. _octaves = octaves;
  105. if (_suppressGeneration)
  106. return;
  107. _forceField.setOctaves(octaves);
  108. }
  109. //-----------------------------------------------------------------------
  110. double PUForceFieldAffector::getFrequency(void) const
  111. {
  112. return _frequency;
  113. }
  114. //-----------------------------------------------------------------------
  115. void PUForceFieldAffector::setFrequency(double frequency)
  116. {
  117. _frequency = frequency;
  118. if (_suppressGeneration)
  119. return;
  120. _forceField.setFrequency(frequency);
  121. }
  122. //-----------------------------------------------------------------------
  123. double PUForceFieldAffector::getAmplitude(void) const
  124. {
  125. return _amplitude;
  126. }
  127. //-----------------------------------------------------------------------
  128. void PUForceFieldAffector::setAmplitude(double amplitude)
  129. {
  130. _amplitude = amplitude;
  131. if (_suppressGeneration)
  132. return;
  133. _forceField.setAmplitude(amplitude);
  134. }
  135. //-----------------------------------------------------------------------
  136. double PUForceFieldAffector::getPersistence(void) const
  137. {
  138. return _persistence;
  139. }
  140. //-----------------------------------------------------------------------
  141. void PUForceFieldAffector::setPersistence(double persistence)
  142. {
  143. _persistence = persistence;
  144. if (_suppressGeneration)
  145. return;
  146. _forceField.setPersistence(persistence);
  147. }
  148. //-----------------------------------------------------------------------
  149. unsigned int PUForceFieldAffector::getForceFieldSize(void) const
  150. {
  151. return _forceFieldSize;
  152. }
  153. //-----------------------------------------------------------------------
  154. void PUForceFieldAffector::setForceFieldSize(unsigned int forceFieldSize)
  155. {
  156. _forceFieldSize = forceFieldSize;
  157. if (_suppressGeneration)
  158. return;
  159. _forceField.setForceFieldSize(forceFieldSize);
  160. }
  161. //-----------------------------------------------------------------------
  162. Vec3 PUForceFieldAffector::getWorldSize(void) const
  163. {
  164. return _worldSize;
  165. }
  166. //-----------------------------------------------------------------------
  167. void PUForceFieldAffector::setWorldSize(const Vec3& worldSize)
  168. {
  169. _worldSize = worldSize;
  170. if (_suppressGeneration)
  171. return;
  172. _forceField.setWorldSize(worldSize);
  173. }
  174. //-----------------------------------------------------------------------
  175. bool PUForceFieldAffector::getIgnoreNegativeX(void) const
  176. {
  177. return _ignoreNegativeX;
  178. }
  179. //-----------------------------------------------------------------------
  180. void PUForceFieldAffector::setIgnoreNegativeX(bool ignoreNegativeX)
  181. {
  182. _ignoreNegativeX = ignoreNegativeX;
  183. }
  184. //-----------------------------------------------------------------------
  185. bool PUForceFieldAffector::getIgnoreNegativeY(void) const
  186. {
  187. return _ignoreNegativeY;
  188. }
  189. //-----------------------------------------------------------------------
  190. void PUForceFieldAffector::setIgnoreNegativeY(bool ignoreNegativeY)
  191. {
  192. _ignoreNegativeY = ignoreNegativeY;
  193. }
  194. //-----------------------------------------------------------------------
  195. bool PUForceFieldAffector::getIgnoreNegativeZ(void) const
  196. {
  197. return _ignoreNegativeZ;
  198. }
  199. //-----------------------------------------------------------------------
  200. void PUForceFieldAffector::setIgnoreNegativeZ(bool ignoreNegativeZ)
  201. {
  202. _ignoreNegativeZ = ignoreNegativeZ;
  203. }
  204. //-----------------------------------------------------------------------
  205. float PUForceFieldAffector::getMovementFrequency(void) const
  206. {
  207. return _movementFrequency;
  208. }
  209. //-----------------------------------------------------------------------
  210. void PUForceFieldAffector::setMovementFrequency(float movementFrequency)
  211. {
  212. _movementFrequency = movementFrequency;
  213. _movementSet = (movementFrequency > 0.0f);
  214. }
  215. //-----------------------------------------------------------------------
  216. const Vec3& PUForceFieldAffector::getMovement(void) const
  217. {
  218. return _movement;
  219. }
  220. //-----------------------------------------------------------------------
  221. void PUForceFieldAffector::setMovement(const Vec3& movement)
  222. {
  223. _movement = movement;
  224. _movementSet = (movement != Vec3::ZERO);
  225. }
  226. //-----------------------------------------------------------------------
  227. void PUForceFieldAffector::suppressGeneration(bool suppress)
  228. {
  229. _suppressGeneration = suppress;
  230. }
  231. void PUForceFieldAffector::updatePUAffector( PUParticle3D *particle, float deltaTime )
  232. {
  233. //for (auto iter : _particleSystem->getParticles())
  234. {
  235. //PUParticle3D *particle = iter;
  236. _forceField.determineForce(particle->position, _force, _delta);
  237. // If negative values are ignored, set the force to 0.
  238. if (_ignoreNegativeX)
  239. {
  240. _force.x = 0.0f;
  241. }
  242. if (_ignoreNegativeY)
  243. {
  244. _force.y = 0.0f;
  245. }
  246. if (_ignoreNegativeZ)
  247. {
  248. _force.z = 0.0f;
  249. }
  250. particle->direction += deltaTime * _scaleForce * _force;
  251. }
  252. }
  253. void PUForceFieldAffector::notifyStart()
  254. {
  255. _movementFrequencyCount = 0.0f;
  256. }
  257. void PUForceFieldAffector::preUpdateAffector( float deltaTime )
  258. {
  259. if (_movementSet)
  260. {
  261. if (deltaTime > _movementFrequency)
  262. {
  263. // Ignore too large times, because it just blows things up
  264. return;
  265. }
  266. _movementFrequencyCount += deltaTime;
  267. if (_movementFrequencyCount > _movementFrequency)
  268. {
  269. _movementFrequencyCount -= _movementFrequency;
  270. }
  271. _displacement = sin(2.0f * M_PI * _movementFrequencyCount/_movementFrequency) * _movement;
  272. _forceField.setForceFieldPositionBase(_basePosition + _displacement);
  273. }
  274. }
  275. void PUForceFieldAffector::prepare()
  276. {
  277. //if (particleTechnique->getParentSystem())
  278. {
  279. // Forcefield position is same position as particle system position
  280. _forceField.initialise(_forceFieldType,
  281. // _particleSystem->getDerivedPosition()
  282. getDerivedPosition(),
  283. _forceFieldSize,
  284. _octaves,
  285. _frequency,
  286. _amplitude,
  287. _persistence,
  288. _worldSize);
  289. _basePosition = _forceField.getForceFieldPositionBase();
  290. _prepared = true;
  291. }
  292. }
  293. PUForceFieldAffector* PUForceFieldAffector::create()
  294. {
  295. auto pffa = new (std::nothrow) PUForceFieldAffector();
  296. pffa->autorelease();
  297. return pffa;
  298. }
  299. void PUForceFieldAffector::copyAttributesTo( PUAffector* affector )
  300. {
  301. PUAffector::copyAttributesTo(affector);
  302. PUForceFieldAffector* forceFieldAffector = static_cast<PUForceFieldAffector*>(affector);
  303. forceFieldAffector->_forceFieldType = _forceFieldType;
  304. forceFieldAffector->_delta = _delta;
  305. forceFieldAffector->_scaleForce = _scaleForce;
  306. forceFieldAffector->_octaves = _octaves;
  307. forceFieldAffector->_frequency = _frequency;
  308. forceFieldAffector->_amplitude = _amplitude;
  309. forceFieldAffector->_persistence = _persistence;
  310. forceFieldAffector->_forceFieldSize = _forceFieldSize;
  311. forceFieldAffector->_worldSize = _worldSize;
  312. forceFieldAffector->_ignoreNegativeX = _ignoreNegativeX;
  313. forceFieldAffector->_ignoreNegativeY = _ignoreNegativeY;
  314. forceFieldAffector->_ignoreNegativeZ = _ignoreNegativeZ;
  315. forceFieldAffector->_movementSet = _movementSet;
  316. forceFieldAffector->_movementFrequency = _movementFrequency;
  317. forceFieldAffector->_movement = _movement;
  318. }
  319. NS_CC_END