CCPULineAffector.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "CCPULineAffector.h"
  22. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  23. NS_CC_BEGIN
  24. // Constants
  25. const float PULineAffector::DEFAULT_MAX_DEVIATION = 1.0f;
  26. const float PULineAffector::DEFAULT_TIME_STEP = 0.1f;
  27. const Vec3 PULineAffector::DEFAULT_END(0, 0, 0);
  28. const float PULineAffector::DEFAULT_DRIFT = 0.0f;
  29. //-----------------------------------------------------------------------
  30. PULineAffector::PULineAffector(void) :
  31. PUAffector(),
  32. _maxDeviation(DEFAULT_MAX_DEVIATION),
  33. _scaledMaxDeviation(1.0f),
  34. _end(DEFAULT_END),
  35. _timeSinceLastUpdate(0.0f),
  36. _timeStep(DEFAULT_TIME_STEP),
  37. _drift(DEFAULT_DRIFT),
  38. _oneMinusDrift(1.0f),
  39. _update(true),
  40. _first(true)
  41. {
  42. }
  43. PULineAffector::~PULineAffector( void )
  44. {
  45. }
  46. //-----------------------------------------------------------------------
  47. float PULineAffector::getMaxDeviation(void) const
  48. {
  49. return _maxDeviation;
  50. }
  51. //-----------------------------------------------------------------------
  52. void PULineAffector::setMaxDeviation(float maxDeviation)
  53. {
  54. _maxDeviation = maxDeviation;
  55. _scaledMaxDeviation = _maxDeviation * _affectorScale.length();
  56. }
  57. //-----------------------------------------------------------------------
  58. const Vec3& PULineAffector::getEnd(void) const
  59. {
  60. return _end;
  61. }
  62. //-----------------------------------------------------------------------
  63. void PULineAffector::setEnd(const Vec3& end)
  64. {
  65. _end = end;
  66. }
  67. //-----------------------------------------------------------------------
  68. float PULineAffector::getTimeStep(void) const
  69. {
  70. return _timeStep;
  71. }
  72. //-----------------------------------------------------------------------
  73. void PULineAffector::setTimeStep(float timeStep)
  74. {
  75. _timeStep = timeStep;
  76. }
  77. //-----------------------------------------------------------------------
  78. float PULineAffector::getDrift(void) const
  79. {
  80. return _drift;
  81. }
  82. //-----------------------------------------------------------------------
  83. void PULineAffector::setDrift(float drift)
  84. {
  85. _drift = drift;
  86. _oneMinusDrift = 1.0f - drift;
  87. }
  88. //-----------------------------------------------------------------------
  89. void PULineAffector::notifyRescaled(const Vec3& scale)
  90. {
  91. _scaledMaxDeviation = _maxDeviation * scale.length();
  92. }
  93. //-----------------------------------------------------------------------
  94. void PULineAffector::preUpdateAffector(float deltaTime)
  95. {
  96. if (/*technique->getNumberOfEmittedParticles()*/static_cast<PUParticleSystem3D *>(_particleSystem)->getAliveParticleCount() > 0)
  97. {
  98. _timeSinceLastUpdate += deltaTime;
  99. while (_timeSinceLastUpdate > _timeStep)
  100. {
  101. _timeSinceLastUpdate -= _timeStep;
  102. _update = true;
  103. }
  104. }
  105. (static_cast<PUParticleSystem3D *>(_particleSystem))->rotationOffset(_end); // Always update
  106. }
  107. //-----------------------------------------------------------------------
  108. void PULineAffector::updatePUAffector( PUParticle3D *particle, float /*deltaTime*/ )
  109. {
  110. //_first = true;
  111. //for (auto iter : _particleSystem->getParticles())
  112. {
  113. //PUParticle3D *particle = iter;
  114. (static_cast<PUParticleSystem3D *>(_particleSystem))->rotationOffset(particle->originalPosition); // Always update
  115. if (_update && CCRANDOM_0_1() > 0.5f && !_first)
  116. {
  117. // Generate a random vector perpendicular on the line
  118. Vec3 perpendicular;
  119. Vec3::cross(_end, Vec3(CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1(), CCRANDOM_MINUS1_1()), &perpendicular);
  120. perpendicular.normalize();
  121. // Determine a random point near the line.
  122. Vec3 targetPosition = particle->originalPosition + _scaledMaxDeviation * CCRANDOM_0_1() * perpendicular;
  123. /** Set the new position.
  124. @remarks
  125. This affector already takes rotational offset of the particle system into account. This means that there is no need
  126. to set the particle system to keep_local to 'true'. The reason is that this is a specialized affector that calculates
  127. a new particle position instead of a direction.
  128. */
  129. particle->position = _drift * targetPosition + _oneMinusDrift * particle->position;
  130. (static_cast<PUParticleSystem3D *>(_particleSystem))->rotationOffset(particle->position);
  131. }
  132. _first = false;
  133. }
  134. }
  135. //-----------------------------------------------------------------------
  136. void PULineAffector::postUpdateAffector(float /*deltaTime*/)
  137. {
  138. _update = false;
  139. }
  140. void PULineAffector::firstParticleUpdate( PUParticle3D* /*particle*/, float /*deltaTime*/ )
  141. {
  142. _first = true;
  143. }
  144. PULineAffector* PULineAffector::create()
  145. {
  146. auto pla = new (std::nothrow) PULineAffector();
  147. pla->autorelease();
  148. return pla;
  149. }
  150. void PULineAffector::copyAttributesTo( PUAffector* affector )
  151. {
  152. PUAffector::copyAttributesTo(affector);
  153. PULineAffector* lineAffector = static_cast<PULineAffector*>(affector);
  154. lineAffector->setMaxDeviation(_maxDeviation);
  155. lineAffector->_end = _end;
  156. lineAffector->_timeStep = _timeStep;
  157. lineAffector->_drift = _drift;
  158. lineAffector->_oneMinusDrift = _oneMinusDrift;
  159. }
  160. NS_CC_END