CCPUGeometryRotator.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 "CCPUGeometryRotator.h"
  22. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  23. NS_CC_BEGIN
  24. //-----------------------------------------------------------------------
  25. // Constants
  26. const bool PUGeometryRotator::DEFAULT_USE_OWN = false;
  27. const float PUGeometryRotator::DEFAULT_ROTATION_SPEED = 10.0f;
  28. const Vec3 PUGeometryRotator::DEFAULT_ROTATION_AXIS(0, 0, 0);
  29. //-----------------------------------------------------------------------
  30. PUGeometryRotator::PUGeometryRotator() :
  31. PUAffector(),
  32. _scaledRotationSpeed(0.0f),
  33. _useOwnRotationSpeed(DEFAULT_USE_OWN),
  34. //mQ(Quaternion::IDENTITY),
  35. _rotationAxis(DEFAULT_ROTATION_AXIS),
  36. _rotationAxisSet(false)
  37. {
  38. _dynRotationSpeed = new (std::nothrow) PUDynamicAttributeFixed();
  39. (static_cast<PUDynamicAttributeFixed*>(_dynRotationSpeed))->setValue(DEFAULT_ROTATION_SPEED);
  40. };
  41. //-----------------------------------------------------------------------
  42. PUGeometryRotator::~PUGeometryRotator()
  43. {
  44. if (_dynRotationSpeed)
  45. CC_SAFE_DELETE(_dynRotationSpeed);
  46. }
  47. //-----------------------------------------------------------------------
  48. const Vec3& PUGeometryRotator::getRotationAxis() const
  49. {
  50. return _rotationAxis;
  51. }
  52. //-----------------------------------------------------------------------
  53. void PUGeometryRotator::setRotationAxis(const Vec3& rotationAxis)
  54. {
  55. _rotationAxis = rotationAxis;
  56. _rotationAxisSet = true;
  57. }
  58. //-----------------------------------------------------------------------
  59. void PUGeometryRotator::resetRotationAxis(void)
  60. {
  61. _dynRotationSpeed = new (std::nothrow) PUDynamicAttributeFixed();
  62. (static_cast<PUDynamicAttributeFixed*>(_dynRotationSpeed))->setValue(DEFAULT_ROTATION_SPEED);
  63. _rotationAxisSet = false;
  64. }
  65. //-----------------------------------------------------------------------
  66. PUDynamicAttribute* PUGeometryRotator::getRotationSpeed(void) const
  67. {
  68. return _dynRotationSpeed;
  69. }
  70. //-----------------------------------------------------------------------
  71. void PUGeometryRotator::setRotationSpeed(PUDynamicAttribute* dynRotationSpeed)
  72. {
  73. if (_dynRotationSpeed)
  74. CC_SAFE_DELETE(_dynRotationSpeed);
  75. _dynRotationSpeed = dynRotationSpeed;
  76. }
  77. //-----------------------------------------------------------------------
  78. bool PUGeometryRotator::useOwnRotationSpeed (void) const
  79. {
  80. return _useOwnRotationSpeed;
  81. }
  82. //-----------------------------------------------------------------------
  83. void PUGeometryRotator::setUseOwnRotationSpeed (bool useOwnRotationSpeed)
  84. {
  85. _useOwnRotationSpeed = useOwnRotationSpeed;
  86. }
  87. //-----------------------------------------------------------------------
  88. float PUGeometryRotator::calculateRotationSpeed(PUParticle3D* particle)
  89. {
  90. return _dynamicAttributeHelper.calculate(_dynRotationSpeed, particle->timeFraction);
  91. }
  92. //-----------------------------------------------------------------------
  93. void PUGeometryRotator::initParticleForEmission(PUParticle3D* particle)
  94. {
  95. //// Only continue if the particle is a visual particle
  96. //if (particle->particleType != Particle::PT_VISUAL)
  97. // return;
  98. //for (auto iter : _particleSystem->getParticlePool().getActiveParticleList())
  99. {
  100. //PUParticle3D *particle = static_cast<PUParticle3D*>(iter);
  101. if (!_rotationAxisSet)
  102. {
  103. // Set initial random rotation axis and orientation(PU 1.4)
  104. particle->orientation.x = CCRANDOM_MINUS1_1();
  105. particle->orientation.y = CCRANDOM_MINUS1_1();
  106. particle->orientation.z = CCRANDOM_MINUS1_1();
  107. particle->orientation.w = CCRANDOM_MINUS1_1();
  108. particle->orientation.normalize();
  109. particle->rotationAxis.x = CCRANDOM_0_1();
  110. particle->rotationAxis.y = CCRANDOM_0_1();
  111. particle->rotationAxis.z = CCRANDOM_0_1();
  112. particle->rotationAxis.normalize();
  113. }
  114. if (_useOwnRotationSpeed)
  115. {
  116. // Use the rotation speed of the particle itself
  117. particle->rotationSpeed = calculateRotationSpeed(particle);
  118. }
  119. }
  120. }
  121. //-----------------------------------------------------------------------
  122. void PUGeometryRotator::updatePUAffector( PUParticle3D *particle, float deltaTime )
  123. {
  124. //for (auto iter : _particleSystem->getParticles())
  125. {
  126. //PUParticle3D *particle = iter;
  127. // Rotate the geometry
  128. if (_useOwnRotationSpeed)
  129. {
  130. // Use scaled rotationspeed and adjust the speed according to the velocity
  131. _scaledRotationSpeed = particle->rotationSpeed * deltaTime;
  132. }
  133. else
  134. {
  135. // Scale speed (beware that dynamic values don't result in a rotation; use a fixed value instead)
  136. _scaledRotationSpeed = calculateRotationSpeed(particle) * deltaTime;
  137. }
  138. _q.set(0.0f, 0.0f, 0.0f, 1.0f);
  139. //_q = Quaternion::IDENTITY;
  140. if (_rotationAxisSet)
  141. {
  142. _q.set(_rotationAxis, _scaledRotationSpeed);
  143. //_q.FromAngleAxis(Radian(_scaledRotationSpeed), _rotationAxis);
  144. }
  145. else
  146. {
  147. _q.set(particle->rotationAxis, _scaledRotationSpeed);
  148. //_q.FromAngleAxis(Radian(_scaledRotationSpeed), visualParticle->rotationAxis);
  149. }
  150. particle->orientation = _q * particle->orientation;
  151. }
  152. }
  153. PUGeometryRotator* PUGeometryRotator::create()
  154. {
  155. auto pgr = new (std::nothrow) PUGeometryRotator();
  156. pgr->autorelease();
  157. return pgr;
  158. }
  159. void PUGeometryRotator::copyAttributesTo( PUAffector* affector )
  160. {
  161. PUAffector::copyAttributesTo(affector);
  162. PUGeometryRotator* geometryRotator = static_cast<PUGeometryRotator*>(affector);
  163. geometryRotator->setRotationSpeed(getRotationSpeed()->clone());
  164. geometryRotator->_useOwnRotationSpeed = _useOwnRotationSpeed;
  165. geometryRotator->_rotationAxis = _rotationAxis;
  166. geometryRotator->_rotationAxisSet = _rotationAxisSet;
  167. }
  168. //-----------------------------------------------------------------------
  169. NS_CC_END