CCPUSphereCollider.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "CCPUSphereCollider.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. NS_CC_BEGIN
  25. // Constants
  26. const float PUSphereCollider::DEFAULT_RADIUS = 100.0f;
  27. //-----------------------------------------------------------------------
  28. PUSphereCollider::PUSphereCollider(void) :
  29. PUBaseCollider(),
  30. _radius(DEFAULT_RADIUS),
  31. _innerCollision(false)
  32. {
  33. }
  34. PUSphereCollider::~PUSphereCollider( void )
  35. {
  36. }
  37. //-----------------------------------------------------------------------
  38. float PUSphereCollider::getRadius() const
  39. {
  40. return _radius;
  41. }
  42. //-----------------------------------------------------------------------
  43. void PUSphereCollider::setRadius(const float radius)
  44. {
  45. _radius = radius;
  46. _sphere.setRadius(_radius);
  47. }
  48. //-----------------------------------------------------------------------
  49. bool PUSphereCollider::isInnerCollision(void) const
  50. {
  51. return _innerCollision;
  52. }
  53. //-----------------------------------------------------------------------
  54. void PUSphereCollider::setInnerCollision(bool innerCollision)
  55. {
  56. _innerCollision = innerCollision;
  57. }
  58. //-----------------------------------------------------------------------
  59. void PUSphereCollider::calculateDirectionAfterCollision(PUParticle3D* particle, Vec3 distance, float distanceLength)
  60. {
  61. switch (_collisionType)
  62. {
  63. case PUBaseCollider::CT_BOUNCE:
  64. {
  65. /** If the particle is on the surface (or just inside the sphere); bounce it
  66. Make use of formula R = 2 * (-I dot N) * N + I, where
  67. R = the new direction vector
  68. I = the old (unit) direction vector before the collision
  69. N = the Normal at the collision point
  70. */
  71. float directionLength = particle->direction.length();
  72. particle->direction.normalize();
  73. distance.normalize();
  74. particle->direction = 2 * (-particle->direction.dot(distance)) * distance + particle->direction;
  75. // Adjust to original speed
  76. particle->direction *= directionLength;
  77. // Accelerate/slow down, using the bounce value
  78. particle->direction *= _bouncyness;
  79. }
  80. break;
  81. case PUBaseCollider::CT_FLOW:
  82. {
  83. /** Reset the position (on the sphere), but keep the direction.
  84. This doesn't really work good for box-type collisions, because it doesn't take the particle
  85. dimensions into account.
  86. */
  87. float scaledRadius = 0.3333f * (_affectorScale.x + _affectorScale.y + _affectorScale.z) * _radius;
  88. particle->position = _derivedPosition + distance * (scaledRadius / distanceLength);
  89. }
  90. break;
  91. default:
  92. break;
  93. }
  94. }
  95. void PUSphereCollider::updatePUAffector( PUParticle3D *particle, float /*deltaTime*/ )
  96. {
  97. //for (auto iter : _particleSystem->getParticles())
  98. {
  99. //PUParticle3D *particle = iter;
  100. _predictedPosition = particle->position + _velocityScale * particle->direction;
  101. bool collision = false;
  102. Vec3 distance = particle->position - _derivedPosition;
  103. float distanceLength = distance.length();
  104. float scaledRadius = 0.3333f * (_affectorScale.x + _affectorScale.y + _affectorScale.z) * _radius; // Scaling changed in V 1.3.1
  105. switch(_intersectionType)
  106. {
  107. case PUBaseCollider::IT_POINT:
  108. {
  109. // Validate for a point-sphere intersection
  110. if (_innerCollision == (distanceLength > scaledRadius))
  111. {
  112. // Collision detected (re-position the particle)
  113. particle->position -= _velocityScale * particle->direction;
  114. collision = true;
  115. }
  116. else
  117. {
  118. distance = _predictedPosition - _derivedPosition;
  119. distanceLength = distance.length();
  120. if (_innerCollision == (distanceLength > scaledRadius))
  121. {
  122. // Collision detected
  123. collision = true;
  124. }
  125. }
  126. }
  127. break;
  128. case PUBaseCollider::IT_BOX:
  129. {
  130. //// Validate for a box-sphere intersection
  131. //if (particle->particleType != Particle::PT_VISUAL)
  132. // break;
  133. AABB box;
  134. populateAlignedBox(box,
  135. particle->position,
  136. particle->width,
  137. particle->height,
  138. particle->depth);
  139. //FIXME
  140. //if (_innerCollision != box.intersects(_sphere))
  141. //{
  142. // // Collision detected (re-position the particle)
  143. // particle->position -= _velocityScale * particle->direction;
  144. // collision = true;
  145. //}
  146. //else
  147. //{
  148. // AABB box;
  149. // populateAlignedBox(box,
  150. // _predictedPosition,
  151. // particle->width,
  152. // particle->height,
  153. // particle->depth);
  154. // if (_innerCollision != box.intersects(_sphere))
  155. // {
  156. // // Collision detected
  157. // collision = true;
  158. // }
  159. //}
  160. }
  161. break;
  162. }
  163. if (collision)
  164. {
  165. calculateDirectionAfterCollision(particle, distance, distanceLength);
  166. calculateRotationSpeedAfterCollision(particle);
  167. particle->addEventFlags(PUParticle3D::PEF_COLLIDED);
  168. }
  169. }
  170. }
  171. void PUSphereCollider::preUpdateAffector( float /*deltaTime*/ )
  172. {
  173. // Calculate the affectors' center position.
  174. _sphere.setCenter(getDerivedPosition());
  175. }
  176. PUSphereCollider* PUSphereCollider::create()
  177. {
  178. auto psc = new (std::nothrow) PUSphereCollider();
  179. psc->autorelease();
  180. return psc;
  181. }
  182. void PUSphereCollider::copyAttributesTo( PUAffector* affector )
  183. {
  184. PUAffector::copyAttributesTo(affector);
  185. PUSphereCollider* sphereCollider = static_cast<PUSphereCollider*>(affector);
  186. sphereCollider->_radius = _radius;
  187. sphereCollider->_sphere = _sphere;
  188. sphereCollider->_innerCollision = _innerCollision;
  189. }
  190. NS_CC_END