CCPUParticleFollower.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "CCPUParticleFollower.h"
  22. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  23. NS_CC_BEGIN
  24. // Constants
  25. const float PUParticleFollower::DEFAULT_MAX_DISTANCE = 3.40282e+038f;
  26. const float PUParticleFollower::DEFAULT_MIN_DISTANCE = 10.0f;
  27. //-----------------------------------------------------------------------
  28. PUParticleFollower::PUParticleFollower(void) :
  29. PUAffector(),
  30. _minDistance(DEFAULT_MIN_DISTANCE),
  31. _maxDistance(DEFAULT_MAX_DISTANCE),
  32. _first(false)
  33. {
  34. }
  35. PUParticleFollower::~PUParticleFollower( void )
  36. {
  37. }
  38. //-----------------------------------------------------------------------
  39. float PUParticleFollower::getMaxDistance(void) const
  40. {
  41. return _maxDistance;
  42. }
  43. //-----------------------------------------------------------------------
  44. void PUParticleFollower::setMaxDistance(float maxDistance)
  45. {
  46. _maxDistance = maxDistance;
  47. }
  48. //-----------------------------------------------------------------------
  49. float PUParticleFollower::getMinDistance(void) const
  50. {
  51. return _minDistance;
  52. }
  53. //-----------------------------------------------------------------------
  54. void PUParticleFollower::setMinDistance(float minDistance)
  55. {
  56. _minDistance = minDistance;
  57. }
  58. void PUParticleFollower::updatePUAffector( PUParticle3D *particle, float /*deltaTime*/ )
  59. {
  60. //_first = true;
  61. //for (auto iter : _particleSystem->getParticles())
  62. {
  63. //PUParticle3D *particle = iter;
  64. if (!_first)
  65. {
  66. // Change in V 1.3.1
  67. // Only proceed if it isn the first one. Compensate for scaling.
  68. float distance = (particle->position).distance(_positionPreviousParticle);
  69. float avgScale = 0.3333f * (_affectorScale.x + _affectorScale.y + _affectorScale.z);
  70. float scaledMinDistance = avgScale * _minDistance;
  71. if (distance > scaledMinDistance && distance < avgScale * _maxDistance)
  72. {
  73. // This particle drifts too much from the previous one; correct it!
  74. float f = scaledMinDistance/distance;
  75. particle->position = _positionPreviousParticle + f * (particle->position - _positionPreviousParticle);
  76. }
  77. }
  78. _positionPreviousParticle = particle->position;
  79. _first = false;
  80. }
  81. }
  82. void PUParticleFollower::firstParticleUpdate( PUParticle3D* /*particle*/, float /*deltaTime*/ )
  83. {
  84. _first = true;
  85. }
  86. PUParticleFollower* PUParticleFollower::create()
  87. {
  88. auto ppf = new (std::nothrow) PUParticleFollower();
  89. ppf->autorelease();
  90. return ppf;
  91. }
  92. void PUParticleFollower::copyAttributesTo( PUAffector* affector )
  93. {
  94. PUAffector::copyAttributesTo(affector);
  95. PUParticleFollower* particleFollower = static_cast<PUParticleFollower*>(affector);
  96. particleFollower->_maxDistance = _maxDistance;
  97. particleFollower->_minDistance = _minDistance;
  98. }
  99. NS_CC_END