CCPUInterParticleCollider.cpp 7.7 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 "CCPUInterParticleCollider.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. NS_CC_BEGIN
  25. // Constants
  26. const float PUParticle3DInterParticleCollider::DEFAULT_ADJUSTMENT = 1.0f;
  27. const PUParticle3DInterParticleCollider::InterParticleCollisionResponse PUParticle3DInterParticleCollider::DEFAULT_COLLISION_RESPONSE =
  28. PUParticle3DInterParticleCollider::IPCR_AVERAGE_VELOCITY;
  29. //-----------------------------------------------------------------------
  30. PUParticle3DInterParticleCollider::PUParticle3DInterParticleCollider(void) :
  31. PUBaseCollider(),
  32. _adjustment(DEFAULT_ADJUSTMENT),
  33. _interParticleCollisionResponse(DEFAULT_COLLISION_RESPONSE)
  34. {
  35. }
  36. PUParticle3DInterParticleCollider::~PUParticle3DInterParticleCollider( void )
  37. {
  38. }
  39. //-----------------------------------------------------------------------
  40. float PUParticle3DInterParticleCollider::getAdjustment(void) const
  41. {
  42. return _adjustment;
  43. }
  44. //-----------------------------------------------------------------------
  45. void PUParticle3DInterParticleCollider::setAdjustment(float adjustment)
  46. {
  47. _adjustment = adjustment;
  48. }
  49. //-----------------------------------------------------------------------
  50. PUParticle3DInterParticleCollider::InterParticleCollisionResponse PUParticle3DInterParticleCollider::getInterParticleCollisionResponse(void) const
  51. {
  52. return _interParticleCollisionResponse;
  53. }
  54. //-----------------------------------------------------------------------
  55. void PUParticle3DInterParticleCollider::setInterParticleCollisionResponse(PUParticle3DInterParticleCollider::InterParticleCollisionResponse interParticleCollisionResponse)
  56. {
  57. _interParticleCollisionResponse = interParticleCollisionResponse;
  58. }
  59. //-----------------------------------------------------------------------
  60. void PUParticle3DInterParticleCollider::prepare()
  61. {
  62. // Activate spatial hashing
  63. //particleTechnique->setSpatialHashingUsed(true);
  64. }
  65. //-----------------------------------------------------------------------
  66. void PUParticle3DInterParticleCollider::unPrepare()
  67. {
  68. // Deactivate spatial hashing
  69. //particleTechnique->setSpatialHashingUsed(false);
  70. }
  71. //-----------------------------------------------------------------------
  72. bool PUParticle3DInterParticleCollider::validateAndExecuteSphereCollision (PUParticle3D* particle1, PUParticle3D* particle2, float /*timeElapsed*/)
  73. {
  74. PUParticle3D* vp1 = static_cast<PUParticle3D*>(particle1);
  75. PUParticle3D* vp2 = static_cast<PUParticle3D*>(particle2);
  76. if ((vp1->position - vp2->position).length() < _adjustment * (vp1->radius + vp2->radius))
  77. {
  78. /** Collision detected.
  79. @remarks
  80. The collision response calculation isn't accurate, but gives an acceptable result.
  81. */
  82. Vec3 n = vp1->position - vp2->position;
  83. n.normalize();
  84. switch(_interParticleCollisionResponse)
  85. {
  86. case IPCR_AVERAGE_VELOCITY:
  87. {
  88. // Use average velocity; this keeps the particles in movement.
  89. float velocity1 = vp1->direction.length();
  90. float velocity2 = vp2->direction.length();
  91. float averageVelocity = 0.5f * (velocity1 + velocity2);
  92. vp1->direction = averageVelocity * vp2->mass * n;
  93. vp2->direction = averageVelocity * vp1->mass * -n;
  94. }
  95. break;
  96. case IPCR_ANGLE_BASED_VELOCITY:
  97. {
  98. // The new velocity is based on the angle between original direction and new direction.
  99. // Note, that this usually means that the velocity decreases.
  100. float velocity1 = Vec3(std::abs(vp1->direction.x), std::abs(vp1->direction.y), std::abs(vp1->direction.z)).dot(n);
  101. float velocity2 = Vec3(std::abs(vp2->direction.x), std::abs(vp2->direction.y), std::abs(vp2->direction.z)).dot(n);
  102. vp1->direction = velocity1 * vp2->mass * n;
  103. vp2->direction = velocity2 * vp1->mass * -n;
  104. }
  105. break;
  106. }
  107. vp1->direction *= _bouncyness;
  108. vp2->direction *= _bouncyness;
  109. vp1->addEventFlags(PUParticle3D::PEF_COLLIDED);
  110. vp2->addEventFlags(PUParticle3D::PEF_COLLIDED);
  111. return true;
  112. }
  113. return false;
  114. }
  115. //-----------------------------------------------------------------------
  116. void PUParticle3DInterParticleCollider::updatePUAffector( PUParticle3D* /*particle*/, float /*deltaTime*/ )
  117. {
  118. //CCASSERT(0, "nonsupport yet");
  119. //for (auto iter : _particleSystem->getParticles())
  120. //{
  121. // PUParticle3D *particle = iter;
  122. // Fast rejection: only visible, moving particles are able to collide, unless they are colliding already
  123. // Changed && into || in V1.3.1
  124. // if (//particle->particleType != Particle::PT_VISUAL ||
  125. // particle->hasEventFlags(PUParticle3D::PEF_COLLIDED) ||
  126. // particle->direction == Vec3::ZERO)
  127. // {
  128. // return;
  129. // }
  130. // // Determine whether neighbour particles are colliding.
  131. // SpatialHashTable<Particle*>* hashtable = particleTechnique->getSpatialHashTable();
  132. // if (hashtable)
  133. // {
  134. // SpatialHashTable<Particle*>::HashTableCell cell = hashtable->getCell(particle->position);
  135. // if (cell.empty())
  136. // return;
  137. // unsigned int size = static_cast<unsigned int>(cell.size());
  138. // for (unsigned int i = 0; i < size; ++i)
  139. // {
  140. // Particle* p = cell[i];
  141. // // Don't check if it is the same particle or the particle is already colliding.
  142. // if (particle != p && !p->hasEventFlags(PUParticle3D::PEF_COLLIDED))
  143. // {
  144. // // Check for collision
  145. // if (validateAndExecuteSphereCollision(particle, p, deltaTime))
  146. // {
  147. // return;
  148. // }
  149. // }
  150. // }
  151. // }
  152. //}
  153. }
  154. PUParticle3DInterParticleCollider* PUParticle3DInterParticleCollider::create()
  155. {
  156. auto pipc = new (std::nothrow) PUParticle3DInterParticleCollider();
  157. pipc->autorelease();
  158. return pipc;
  159. }
  160. void PUParticle3DInterParticleCollider::copyAttributesTo( PUAffector* affector )
  161. {
  162. PUBaseCollider::copyAttributesTo(affector);
  163. PUParticle3DInterParticleCollider* interParticleCollider = static_cast<PUParticle3DInterParticleCollider*>(affector);
  164. interParticleCollider->_adjustment = _adjustment;
  165. interParticleCollider->_interParticleCollisionResponse = _interParticleCollisionResponse;
  166. }
  167. NS_CC_END