CCPUPlaneCollider.cpp 6.4 KB

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