CCPUBoxCollider.cpp 10 KB

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