CCPUCircleEmitter.cpp 8.7 KB

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