CCPUBoxCollider.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 "CCPUBoxCollider.h"
  23. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  24. NS_CC_BEGIN
  25. // Constants
  26. const float PUBoxCollider::DEFAULT_WIDTH = 100.0f;
  27. const float PUBoxCollider::DEFAULT_HEIGHT = 100.0f;
  28. const float PUBoxCollider::DEFAULT_DEPTH = 100.0f;
  29. //-----------------------------------------------------------------------
  30. PUBoxCollider::PUBoxCollider() :
  31. PUBaseCollider(),
  32. _width(DEFAULT_WIDTH),
  33. _height(DEFAULT_HEIGHT),
  34. _depth(DEFAULT_DEPTH),
  35. _xmin(0.0f),
  36. _xmax(0.0f),
  37. _ymin(0.0f),
  38. _ymax(0.0f),
  39. _zmin(0.0f),
  40. _zmax(0.0f),
  41. _innerCollision(false)
  42. {
  43. }
  44. PUBoxCollider::~PUBoxCollider()
  45. {
  46. }
  47. //-----------------------------------------------------------------------
  48. float PUBoxCollider::getWidth() const
  49. {
  50. return _width;
  51. }
  52. //-----------------------------------------------------------------------
  53. void PUBoxCollider::setWidth(const float width)
  54. {
  55. _width = width;
  56. }
  57. //-----------------------------------------------------------------------
  58. float PUBoxCollider::getHeight() const
  59. {
  60. return _height;
  61. }
  62. //-----------------------------------------------------------------------
  63. void PUBoxCollider::setHeight(const float height)
  64. {
  65. _height = height;
  66. }
  67. //-----------------------------------------------------------------------
  68. float PUBoxCollider::getDepth() const
  69. {
  70. return _depth;
  71. }
  72. //-----------------------------------------------------------------------
  73. void PUBoxCollider::setDepth(const float depth)
  74. {
  75. _depth = depth;
  76. }
  77. //-----------------------------------------------------------------------
  78. bool PUBoxCollider::isInnerCollision(void) const
  79. {
  80. return _innerCollision;
  81. }
  82. //-----------------------------------------------------------------------
  83. void PUBoxCollider::setInnerCollision(bool innerCollision)
  84. {
  85. _innerCollision = innerCollision;
  86. }
  87. //-----------------------------------------------------------------------
  88. void PUBoxCollider::calculateDirectionAfterCollision(PUParticle3D* particle)
  89. {
  90. switch (_collisionType)
  91. {
  92. case PUBaseCollider::CT_BOUNCE:
  93. {
  94. // Determine the nearest side and reverse the direction
  95. if (isSmallestValue (particle->position.x - _xmin, particle->position))
  96. {
  97. particle->direction.x *= -1;
  98. }
  99. else if (isSmallestValue (_xmax - particle->position.x, particle->position))
  100. {
  101. particle->direction.x *= -1;
  102. }
  103. else if (isSmallestValue (particle->position.y - _ymin, particle->position))
  104. {
  105. particle->direction.y *= -1;
  106. }
  107. else if (isSmallestValue (_ymax - particle->position.y, particle->position))
  108. {
  109. particle->direction.y *= -1;
  110. }
  111. else if (isSmallestValue (particle->position.z - _zmin, particle->position))
  112. {
  113. particle->direction.z *= -1;
  114. }
  115. else if (isSmallestValue (_zmax - particle->position.z, particle->position))
  116. {
  117. particle->direction.z *= -1;
  118. }
  119. particle->direction *= _bouncyness;
  120. }
  121. break;
  122. case PUBaseCollider::CT_FLOW:
  123. {
  124. if (isSmallestValue (particle->position.x - _xmin, particle->position))
  125. {
  126. particle->direction.x = 0;
  127. }
  128. else if (isSmallestValue (_xmax - particle->position.x, particle->position))
  129. {
  130. particle->direction.x = 0;
  131. }
  132. else if (isSmallestValue (particle->position.y - _ymin, particle->position))
  133. {
  134. particle->direction.y = 0;
  135. }
  136. else if (isSmallestValue (_ymax - particle->position.y, particle->position))
  137. {
  138. particle->direction.y = 0;
  139. }
  140. else if (isSmallestValue (particle->position.z - _zmin, particle->position))
  141. {
  142. particle->direction.z = 0;
  143. }
  144. else if (isSmallestValue (_zmax - particle->position.z, particle->position))
  145. {
  146. particle->direction.z = 0;
  147. }
  148. particle->direction *= -_friction;
  149. }
  150. break;
  151. default:
  152. break;
  153. }
  154. }
  155. //-----------------------------------------------------------------------
  156. void PUBoxCollider::calculateBounds ()
  157. {
  158. float scaledWidth = _affectorScale.x * _width;
  159. float scaledHeight = _affectorScale.y * _height;
  160. float scaledDepth = _affectorScale.z * _depth;
  161. _xmin = _derivedPosition.x - 0.5f * scaledWidth;
  162. _xmax = _derivedPosition.x + 0.5f * scaledWidth;
  163. _ymin = _derivedPosition.y - 0.5f * scaledHeight;
  164. _ymax = _derivedPosition.y + 0.5f * scaledHeight;
  165. _zmin = _derivedPosition.z - 0.5f * scaledDepth;
  166. _zmax = _derivedPosition.z + 0.5f * scaledDepth;
  167. }
  168. //-----------------------------------------------------------------------
  169. bool PUBoxCollider::isSmallestValue(float value, const Vec3& particlePosition)
  170. {
  171. float value1 = particlePosition.x - _xmin;
  172. float value2 = _xmax - particlePosition.x;
  173. float value3 = particlePosition.y - _ymin;
  174. float value4 = _ymax - particlePosition.y;
  175. float value5 = particlePosition.z - _zmin;
  176. float value6 = _zmax - particlePosition.z;
  177. return (
  178. value <= value1 &&
  179. value <= value2 &&
  180. value <= value3 &&
  181. value <= value4 &&
  182. value <= value5 &&
  183. value <= value6);
  184. }
  185. void PUBoxCollider::updatePUAffector( PUParticle3D *particle, float /*deltaTime*/ )
  186. {
  187. //for (auto iter : _particleSystem->getParticles())
  188. {
  189. //PUParticle3D *particle = iter;
  190. _predictedPosition = particle->position + _velocityScale * particle->direction;
  191. bool collision = false;
  192. /** Collision detection is a two-step. First, we determine whether the particle is now colliding.
  193. If it is, the particle is re-positioned. However, a timeElapsed value is used, which is not the same
  194. as the one that was used at the moment before the particle was colliding. Therefore, we rather
  195. want to predict particle collision in front. This probably isn't the fastest solution.
  196. The same approach was used for the other colliders.
  197. */
  198. switch(_intersectionType)
  199. {
  200. case PUBaseCollider::IT_POINT:
  201. {
  202. // Validate for a point-box intersection
  203. if (_innerCollision != _box.containPoint(particle->position))
  204. {
  205. // Collision detected (re-position the particle)
  206. particle->position -= _velocityScale * particle->direction;
  207. collision = true;
  208. }
  209. else if (_innerCollision != _box.containPoint(_predictedPosition))
  210. {
  211. // Collision detected
  212. collision = true;
  213. }
  214. }
  215. break;
  216. case PUBaseCollider::IT_BOX:
  217. {
  218. AABB box;
  219. populateAlignedBox(box,
  220. particle->position,
  221. particle->width,
  222. particle->height,
  223. particle->depth);
  224. if (_innerCollision != box.intersects(_box))
  225. {
  226. // Collision detected (re-position the particle)
  227. particle->position -= _velocityScale * particle->direction;
  228. collision = true;
  229. }
  230. else
  231. {
  232. populateAlignedBox(box,
  233. _predictedPosition,
  234. particle->width,
  235. particle->height,
  236. particle->depth);
  237. if (_innerCollision != box.intersects(_box))
  238. {
  239. // Collision detected
  240. collision = true;
  241. }
  242. }
  243. }
  244. break;
  245. }
  246. if (collision)
  247. {
  248. calculateDirectionAfterCollision(particle);
  249. calculateRotationSpeedAfterCollision(particle);
  250. particle->addEventFlags(PUParticle3D::PEF_COLLIDED);
  251. }
  252. }
  253. }
  254. void PUBoxCollider::preUpdateAffector( float deltaTime )
  255. {
  256. PUBaseCollider::preUpdateAffector(deltaTime);
  257. // Calculate the affectors' center position in worldspace, set the box and calculate the bounds
  258. // Applied scaling in V 1.3.1.
  259. populateAlignedBox(_box, getDerivedPosition(), _affectorScale.x * _width, _affectorScale.y * _height, _affectorScale.z * _depth);
  260. calculateBounds();
  261. }
  262. PUBoxCollider* PUBoxCollider::create()
  263. {
  264. auto pbc = new (std::nothrow) PUBoxCollider();
  265. pbc->autorelease();
  266. return pbc;
  267. }
  268. void PUBoxCollider::copyAttributesTo( PUAffector* affector )
  269. {
  270. PUBaseCollider::copyAttributesTo(affector);
  271. PUBoxCollider* boxCollider = static_cast<PUBoxCollider*>(affector);
  272. boxCollider->_width = _width;
  273. boxCollider->_height = _height;
  274. boxCollider->_depth = _depth;
  275. boxCollider->_innerCollision = _innerCollision;
  276. }
  277. NS_CC_END