CCPUGeometryRotator.cpp 7.2 KB

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