CCPUCircleEmitter.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 "CCPUCircleEmitter.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. #include "extensions/Particle3D/PU/CCPUUtil.h"
  25. #include "base/ccRandom.h"
  26. NS_CC_BEGIN
  27. // Constants
  28. const float PUCircleEmitter::DEFAULT_RADIUS = 100.0f;
  29. const float PUCircleEmitter::DEFAULT_STEP = 0.1f;
  30. const float PUCircleEmitter::DEFAULT_ANGLE = 0.0f;
  31. const bool PUCircleEmitter::DEFAULT_RANDOM = true;
  32. const Vec3 PUCircleEmitter::DEFAULT_NORMAL(0, 0, 0);
  33. //-----------------------------------------------------------------------
  34. PUCircleEmitter::PUCircleEmitter(void) :
  35. PUEmitter(),
  36. _radius(DEFAULT_RADIUS),
  37. _circleAngle(DEFAULT_ANGLE),
  38. _originalCircleAngle(DEFAULT_ANGLE),
  39. _step(DEFAULT_STEP),
  40. _x(0.0f),
  41. _z(0.0f),
  42. _random(DEFAULT_RANDOM),
  43. _orientation(),
  44. _normal(DEFAULT_NORMAL)
  45. {
  46. }
  47. //-----------------------------------------------------------------------
  48. float PUCircleEmitter::getRadius() const
  49. {
  50. return _radius;
  51. }
  52. //-----------------------------------------------------------------------
  53. void PUCircleEmitter::setRadius(const float radius)
  54. {
  55. _radius = radius;
  56. }
  57. //-----------------------------------------------------------------------
  58. float PUCircleEmitter::getCircleAngle() const
  59. {
  60. return _originalCircleAngle;
  61. }
  62. //-----------------------------------------------------------------------
  63. void PUCircleEmitter::setCircleAngle(const float circleAngle)
  64. {
  65. _originalCircleAngle = circleAngle;
  66. _circleAngle = circleAngle;
  67. }
  68. //-----------------------------------------------------------------------
  69. float PUCircleEmitter::getStep() const
  70. {
  71. return _step;
  72. }
  73. //-----------------------------------------------------------------------
  74. void PUCircleEmitter::setStep(const float step)
  75. {
  76. _step = step;
  77. }
  78. //-----------------------------------------------------------------------
  79. bool PUCircleEmitter::isRandom() const
  80. {
  81. return _random;
  82. }
  83. //-----------------------------------------------------------------------
  84. void PUCircleEmitter::setRandom(const bool random)
  85. {
  86. _random = random;
  87. }
  88. //-----------------------------------------------------------------------
  89. const Quaternion& PUCircleEmitter::getOrientation(void) const
  90. {
  91. return _orientation;
  92. }
  93. //-----------------------------------------------------------------------
  94. const Vec3& PUCircleEmitter::getNormal(void) const
  95. {
  96. return _normal;
  97. }
  98. //-----------------------------------------------------------------------
  99. void PUCircleEmitter::setNormal(const Vec3& normal)
  100. {
  101. //_orientation = Vec3::UNIT_Y.getRotationTo(normal, Vec3::UNIT_X);
  102. _orientation = getRotationTo(Vec3::UNIT_Y, normal, Vec3::UNIT_X);
  103. _normal = normal;
  104. }
  105. //-----------------------------------------------------------------------
  106. void PUCircleEmitter::notifyStart (void)
  107. {
  108. // Reset the attributes to allow a restart.
  109. _circleAngle = _originalCircleAngle;
  110. }
  111. //-----------------------------------------------------------------------
  112. void PUCircleEmitter::initParticlePosition(PUParticle3D* particle)
  113. {
  114. float angle = 0;
  115. if (_random)
  116. {
  117. // Choose a random position on the circle.
  118. angle = cocos2d::random(0.0, M_PI * 2.0);
  119. }
  120. else
  121. {
  122. // Follow the contour of the circle.
  123. _circleAngle += _step;
  124. _circleAngle = _circleAngle > M_PI * 2.0f ? _circleAngle - (M_PI * 2.0) : _circleAngle;
  125. angle = _circleAngle;
  126. }
  127. _x = cosf(angle);
  128. _z = sinf(angle);
  129. //ParticleSystem* sys = mParentTechnique->getParentSystem();
  130. //if (sys)
  131. {
  132. // Take both orientation of the node and its own orientation, based on the normal, into account
  133. Mat4 rotMat;
  134. Mat4::createRotation(static_cast<PUParticleSystem3D *>(_particleSystem)->getDerivedOrientation() * _orientation, &rotMat);
  135. particle->position = getDerivedPosition() +
  136. /*sys->getDerivedOrientation() * */rotMat * (Vec3(_x * _radius * _emitterScale.x, 0, _z * _radius * _emitterScale.z));
  137. }
  138. //else
  139. //{
  140. // particle->position = getDerivedPosition() + _emitterScale * ( mOrientation * Vec3(mX * mRadius, 0, mZ * mRadius) );
  141. //}
  142. particle->originalPosition = particle->position;
  143. }
  144. //-----------------------------------------------------------------------
  145. void PUCircleEmitter::initParticleDirection(PUParticle3D* particle)
  146. {
  147. if (_autoDirection)
  148. {
  149. // The value of the direction vector that has been set does not have a meaning for
  150. // the circle emitter.
  151. float angle = 0.0f;
  152. generateAngle(angle);
  153. if (angle != 0.0f)
  154. {
  155. //particle->direction = (mOrientation * Vec3(mX, 0, mZ) ).randomDeviant(angle, mUpVector);
  156. Mat4 mat;
  157. Mat4::createRotation(_orientation, &mat);
  158. Vec3 temp = mat * Vec3(_x, 0, _z);
  159. particle->direction = PUUtil::randomDeviant(temp, angle, _upVector);
  160. particle->originalDirection = particle->direction;
  161. }
  162. else
  163. {
  164. Mat4 rotMat;
  165. Mat4::createRotation(_orientation, &rotMat);
  166. particle->direction = rotMat * Vec3(_x, 0, _z);
  167. }
  168. }
  169. else
  170. {
  171. // Use the standard way
  172. PUEmitter::initParticleDirection(particle);
  173. }
  174. }
  175. PUCircleEmitter* PUCircleEmitter::create()
  176. {
  177. auto pe = new (std::nothrow) PUCircleEmitter();
  178. pe->autorelease();
  179. return pe;
  180. }
  181. cocos2d::Quaternion PUCircleEmitter::getRotationTo( const Vec3 &src, const Vec3& dest, const Vec3& fallbackAxis /*= Vec3::ZERO*/ ) const
  182. {
  183. // Based on Stan Melax's article in Game Programming Gems
  184. Quaternion q;
  185. // Copy, since cannot modify local
  186. Vec3 v0 = src;
  187. Vec3 v1 = dest;
  188. v0.normalize();
  189. v1.normalize();
  190. float d = v0.dot(v1);
  191. // If dot == 1, vectors are the same
  192. if (d >= 1.0f)
  193. {
  194. return Quaternion();
  195. }
  196. if (d < (1e-6f - 1.0f))
  197. {
  198. if (fallbackAxis != Vec3::ZERO)
  199. {
  200. // rotate 180 degrees about the fallback axis
  201. q.set(fallbackAxis, (float)M_PI);
  202. //q.FromAngleAxis(Radian(Math::PI), fallbackAxis);
  203. }
  204. else
  205. {
  206. // Generate an axis
  207. Vec3 axis/* = Vec3::UNIT_X.crossProduct(*this)*/;
  208. Vec3::cross(Vec3::UNIT_X, src, &axis);
  209. if (axis.lengthSquared() < (1e-06 * 1e-06)) // pick another if colinear
  210. //axis = Vec3::UNIT_Y.crossProduct(*this);
  211. Vec3::cross(Vec3::UNIT_Y, src, &axis);
  212. axis.normalize();
  213. //q.FromAngleAxis(Radian(Math::PI), axis);
  214. q.set(axis, (float)M_PI);
  215. }
  216. }
  217. else
  218. {
  219. /*float s = Math::Sqrt( (1+d)*2 );*/
  220. float s = sqrtf( (1+d)*2 );
  221. float invs = 1 / s;
  222. Vec3 c /*= v0.crossProduct(v1)*/;
  223. Vec3::cross(v0, v1, &c);
  224. q.x = c.x * invs;
  225. q.y = c.y * invs;
  226. q.z = c.z * invs;
  227. q.w = s * 0.5f;
  228. q.normalize();
  229. }
  230. return q;
  231. }
  232. void PUCircleEmitter::copyAttributesTo( PUEmitter* emitter )
  233. {
  234. PUEmitter::copyAttributesTo(emitter);
  235. PUCircleEmitter* circleEmitter = static_cast<PUCircleEmitter*>(emitter);
  236. circleEmitter->_radius = _radius;
  237. circleEmitter->_circleAngle = _circleAngle;
  238. circleEmitter->_originalCircleAngle = _originalCircleAngle;
  239. circleEmitter->_step = _step;
  240. circleEmitter->_random = _random;
  241. circleEmitter->_normal = _normal;
  242. circleEmitter->_orientation = _orientation;
  243. }
  244. PUCircleEmitter* PUCircleEmitter::clone()
  245. {
  246. auto be = PUCircleEmitter::create();
  247. copyAttributesTo(be);
  248. return be;
  249. }
  250. NS_CC_END