CCPUPlaneCollider.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "CCPUPlaneCollider.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. NS_CC_BEGIN
  25. // Constants
  26. const Vec3 PUPlaneCollider::DEFAULT_NORMAL(0, 0, 0);
  27. //-----------------------------------------------------------------------
  28. PUPlaneCollider::PUPlaneCollider(void) :
  29. PUBaseCollider(),
  30. _normal(DEFAULT_NORMAL)
  31. {
  32. }
  33. PUPlaneCollider::~PUPlaneCollider( void )
  34. {
  35. }
  36. //-----------------------------------------------------------------------
  37. const Vec3 PUPlaneCollider::getNormal(void) const
  38. {
  39. return _normal;
  40. }
  41. //-----------------------------------------------------------------------
  42. void PUPlaneCollider::setNormal(const Vec3& normal)
  43. {
  44. _normal = normal;
  45. _plane.redefine(_normal, getDerivedPosition()); // Changed in 1.3.1
  46. }
  47. //-----------------------------------------------------------------------
  48. void PUPlaneCollider::notifyRescaled(const Vec3& /*scale*/)
  49. {
  50. // Function added in 1.3.1
  51. _plane.redefine(_normal, getDerivedPosition());
  52. }
  53. //-----------------------------------------------------------------------
  54. void PUPlaneCollider::calculateDirectionAfterCollision(PUParticle3D* particle, float timeElapsed)
  55. {
  56. float directionLength = particle->direction.length();
  57. switch (_collisionType)
  58. {
  59. case PUBaseCollider::CT_BOUNCE:
  60. {
  61. /** If the particle is on the plane or at the back of the plane, bounce it.
  62. Make use of the same formula as the sphere collider.
  63. */
  64. particle->direction.normalize();
  65. particle->direction = 2 * (-particle->direction.dot(-_normal)) * -_normal + particle->direction;
  66. // Adjust to original speed
  67. particle->direction *= directionLength;
  68. // Accelerate/slow down, using the bounce value
  69. particle->direction *= _bouncyness;
  70. }
  71. break;
  72. case PUBaseCollider::CT_FLOW:
  73. {
  74. /** Reset the position (just in front of the plane), but keep the direction.
  75. @remarks
  76. This is not really the correct way, because the particle 'jumps'. Maybe it is better to change
  77. the direction parallel to the plane.
  78. */
  79. particle->position += timeElapsed * directionLength * _normal;
  80. }
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. void PUPlaneCollider::updatePUAffector( PUParticle3D *particle, float deltaTime )
  87. {
  88. //for (auto iter : _particleSystem->getParticles())
  89. {
  90. //PUParticle3D *particle = iter;
  91. _predictedPosition = particle->position + _velocityScale * particle->direction;
  92. bool collision = false;
  93. switch(_intersectionType)
  94. {
  95. case PUBaseCollider::IT_POINT:
  96. {
  97. // Validate for a point-plane intersection (on the plane or the back side)
  98. // First determine whether it is now colliding (some affector made the particle move), else
  99. // determine whether it WILL be colliding
  100. if (_plane.getDistance(particle->position) <= 0.0f)
  101. {
  102. // Collision detected (re-position the particle)
  103. particle->position -= _velocityScale * particle->direction;
  104. collision = true;
  105. }
  106. else if (_plane.getDistance(_predictedPosition) <= 0.0f)
  107. {
  108. // Collision detected
  109. collision = true;
  110. }
  111. }
  112. break;
  113. case PUBaseCollider::IT_BOX:
  114. {
  115. AABB box;
  116. populateAlignedBox(box,
  117. particle->position,
  118. particle->width,
  119. particle->height,
  120. particle->depth);
  121. //FIXME
  122. //if (box.intersects(_plane))
  123. //{
  124. // // Collision detected (re-position the particle)
  125. // particle->position -= _velocityScale * particle->direction;
  126. // collision = true;
  127. //}
  128. //else
  129. //{
  130. // populateAlignedBox(box,
  131. // _predictedPosition,
  132. // particle->width,
  133. // particle->height,
  134. // particle->depth);
  135. // if (box.intersects(_plane))
  136. // {
  137. // // Collision detected
  138. // collision = true;
  139. // }
  140. //}
  141. }
  142. break;
  143. }
  144. if (collision)
  145. {
  146. calculateDirectionAfterCollision(particle, deltaTime);
  147. calculateRotationSpeedAfterCollision(particle);
  148. particle->addEventFlags(PUParticle3D::PEF_COLLIDED);
  149. }
  150. }
  151. }
  152. PUPlaneCollider* PUPlaneCollider::create()
  153. {
  154. auto ppc = new (std::nothrow) PUPlaneCollider();
  155. ppc->autorelease();
  156. return ppc;
  157. }
  158. void PUPlaneCollider::copyAttributesTo( PUAffector* affector )
  159. {
  160. PUBaseCollider::copyAttributesTo(affector);
  161. PUPlaneCollider* planeCollider = static_cast<PUPlaneCollider*>(affector);
  162. planeCollider->setNormal(_normal);
  163. }
  164. NS_CC_END