CCActionGrid3D.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /****************************************************************************
  2. Copyright (c) 2009 On-Core
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2016 Chukong Technologies Inc.
  5. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "2d/CCActionGrid3D.h"
  24. #include "base/CCDirector.h"
  25. NS_CC_BEGIN
  26. // implementation of Waves3D
  27. Waves3D* Waves3D::create(float duration, const Size& gridSize, unsigned int waves, float amplitude)
  28. {
  29. Waves3D *action = new (std::nothrow) Waves3D();
  30. if (action && action->initWithDuration(duration, gridSize, waves, amplitude))
  31. {
  32. action->autorelease();
  33. return action;
  34. }
  35. delete action;
  36. return nullptr;
  37. }
  38. bool Waves3D::initWithDuration(float duration, const Size& gridSize, unsigned int waves, float amplitude)
  39. {
  40. if (Grid3DAction::initWithDuration(duration, gridSize))
  41. {
  42. _waves = waves;
  43. _amplitude = amplitude;
  44. _amplitudeRate = 1.0f;
  45. return true;
  46. }
  47. return false;
  48. }
  49. Waves3D* Waves3D::clone() const
  50. {
  51. // no copy constructor
  52. return Waves3D::create(_duration, _gridSize, _waves, _amplitude);
  53. }
  54. void Waves3D::update(float time)
  55. {
  56. int i, j;
  57. for (i = 0; i < _gridSize.width + 1; ++i)
  58. {
  59. for (j = 0; j < _gridSize.height + 1; ++j)
  60. {
  61. Vec3 v = getOriginalVertex(Vec2(i ,j));
  62. v.z += (sinf((float)M_PI * time * _waves * 2 + (v.y+v.x) * 0.01f) * _amplitude * _amplitudeRate);
  63. //CCLOG("v.z offset is %f\n", (sinf((float)M_PI * time * _waves * 2 + (v.y+v.x) * .01f) * _amplitude * _amplitudeRate));
  64. setVertex(Vec2(i, j), v);
  65. }
  66. }
  67. }
  68. // implementation of FlipX3D
  69. FlipX3D* FlipX3D::create(float duration)
  70. {
  71. FlipX3D *action = new (std::nothrow) FlipX3D();
  72. if (action && action->initWithDuration(duration))
  73. {
  74. action->autorelease();
  75. return action;
  76. }
  77. delete action;
  78. return nullptr;
  79. }
  80. bool FlipX3D::initWithDuration(float duration)
  81. {
  82. return Grid3DAction::initWithDuration(duration, Size(1, 1));
  83. }
  84. bool FlipX3D::initWithSize(const Size& gridSize, float duration)
  85. {
  86. if (gridSize.width != 1 || gridSize.height != 1)
  87. {
  88. // Grid size must be (1,1)
  89. CCASSERT(0, "Grid size must be (1,1)");
  90. return false;
  91. }
  92. return Grid3DAction::initWithDuration(duration, gridSize);
  93. }
  94. FlipX3D* FlipX3D::clone() const
  95. {
  96. // no copy constructor
  97. auto a = new (std::nothrow) FlipX3D();
  98. a->initWithSize(_gridSize, _duration);
  99. a->autorelease();
  100. return a;
  101. }
  102. void FlipX3D::update(float time)
  103. {
  104. float angle = (float)M_PI * time; // 180 degrees
  105. float mz = sinf(angle);
  106. angle = angle / 2.0f; // x calculates degrees from 0 to 90
  107. float mx = cosf(angle);
  108. Vec3 v0, v1, v, diff;
  109. v0 = getOriginalVertex(Vec2(1.0f, 1.0f));
  110. v1 = getOriginalVertex(Vec2());
  111. float x0 = v0.x;
  112. float x1 = v1.x;
  113. float x;
  114. Vec2 a, b, c, d;
  115. if ( x0 > x1 )
  116. {
  117. // Normal Grid
  118. a.setZero();
  119. b.set(0.0f, 1.0f);
  120. c.set(1.0f, 0.0f);
  121. d.set(1.0f, 1.0f);
  122. x = x0;
  123. }
  124. else
  125. {
  126. // Reversed Grid
  127. c.setZero();
  128. d.set(0.0f, 1.0f);
  129. a.set(1.0f, 0.0f);
  130. b.set(1.0f, 1.0f);
  131. x = x1;
  132. }
  133. diff.x = ( x - x * mx );
  134. diff.z = fabsf( floorf( (x * mz) / 4.0f ) );
  135. // bottom-left
  136. v = getOriginalVertex(a);
  137. v.x = diff.x;
  138. v.z += diff.z;
  139. setVertex(a, v);
  140. // upper-left
  141. v = getOriginalVertex(b);
  142. v.x = diff.x;
  143. v.z += diff.z;
  144. setVertex(b, v);
  145. // bottom-right
  146. v = getOriginalVertex(c);
  147. v.x -= diff.x;
  148. v.z -= diff.z;
  149. setVertex(c, v);
  150. // upper-right
  151. v = getOriginalVertex(d);
  152. v.x -= diff.x;
  153. v.z -= diff.z;
  154. setVertex(d, v);
  155. }
  156. // implementation of FlipY3D
  157. FlipY3D* FlipY3D::clone() const
  158. {
  159. // no copy constructor
  160. auto a = new (std::nothrow) FlipY3D();
  161. a->initWithSize(_gridSize, _duration);
  162. a->autorelease();
  163. return a;
  164. }
  165. FlipY3D* FlipY3D::create(float duration)
  166. {
  167. FlipY3D *action = new (std::nothrow) FlipY3D();
  168. if (action)
  169. {
  170. if (action->initWithDuration(duration))
  171. {
  172. action->autorelease();
  173. }
  174. else
  175. {
  176. CC_SAFE_RELEASE_NULL(action);
  177. }
  178. }
  179. return action;
  180. }
  181. void FlipY3D::update(float time)
  182. {
  183. float angle = (float)M_PI * time; // 180 degrees
  184. float mz = sinf( angle );
  185. angle = angle / 2.0f; // x calculates degrees from 0 to 90
  186. float my = cosf(angle);
  187. Vec3 v0, v1, v, diff;
  188. v0 = getOriginalVertex(Vec2(1.0f, 1.0f));
  189. v1 = getOriginalVertex(Vec2());
  190. float y0 = v0.y;
  191. float y1 = v1.y;
  192. float y;
  193. Vec2 a, b, c, d;
  194. if (y0 > y1)
  195. {
  196. // Normal Grid
  197. a.setZero();
  198. b.set(0.0f, 1.0f);
  199. c.set(1.0f, 0.0f);
  200. d.set(1.0f, 1.0f);
  201. y = y0;
  202. }
  203. else
  204. {
  205. // Reversed Grid
  206. b.setZero();
  207. a.set(0.0f, 1.0f);
  208. d.set(1.0f, 0.0f);
  209. c.set(1.0f, 1.0f);
  210. y = y1;
  211. }
  212. diff.y = y - y * my;
  213. diff.z = fabsf(floorf((y * mz) / 4.0f));
  214. // bottom-left
  215. v = getOriginalVertex(a);
  216. v.y = diff.y;
  217. v.z += diff.z;
  218. setVertex(a, v);
  219. // upper-left
  220. v = getOriginalVertex(b);
  221. v.y -= diff.y;
  222. v.z -= diff.z;
  223. setVertex(b, v);
  224. // bottom-right
  225. v = getOriginalVertex(c);
  226. v.y = diff.y;
  227. v.z += diff.z;
  228. setVertex(c, v);
  229. // upper-right
  230. v = getOriginalVertex(d);
  231. v.y -= diff.y;
  232. v.z -= diff.z;
  233. setVertex(d, v);
  234. }
  235. // implementation of Lens3D
  236. Lens3D* Lens3D::create(float duration, const Size& gridSize, const Vec2& position, float radius)
  237. {
  238. Lens3D *action = new (std::nothrow) Lens3D();
  239. if (action)
  240. {
  241. if (action->initWithDuration(duration, gridSize, position, radius))
  242. {
  243. action->autorelease();
  244. }
  245. else
  246. {
  247. CC_SAFE_RELEASE_NULL(action);
  248. }
  249. }
  250. return action;
  251. }
  252. bool Lens3D::initWithDuration(float duration, const Size& gridSize, const Vec2& position, float radius)
  253. {
  254. if (Grid3DAction::initWithDuration(duration, gridSize))
  255. {
  256. _position.set(-1.0f, -1.0f);
  257. setPosition(position);
  258. _radius = radius;
  259. _lensEffect = 0.7f;
  260. _concave = false;
  261. _dirty = true;
  262. return true;
  263. }
  264. return false;
  265. }
  266. Lens3D* Lens3D::clone() const
  267. {
  268. // no copy constructor
  269. auto a = new (std::nothrow) Lens3D();
  270. a->initWithDuration(_duration, _gridSize, _position, _radius);
  271. a->autorelease();
  272. return a;
  273. }
  274. void Lens3D::setPosition(const Vec2& pos)
  275. {
  276. if( !pos.equals(_position))
  277. {
  278. _position = pos;
  279. _dirty = true;
  280. }
  281. }
  282. void Lens3D::update(float /*time*/)
  283. {
  284. if (_dirty)
  285. {
  286. int i, j;
  287. for (i = 0; i < _gridSize.width + 1; ++i)
  288. {
  289. for (j = 0; j < _gridSize.height + 1; ++j)
  290. {
  291. Vec3 v = getOriginalVertex(Vec2(i, j));
  292. Vec2 vect = _position - Vec2(v.x, v.y);
  293. float r = vect.getLength();
  294. if (r < _radius)
  295. {
  296. r = _radius - r;
  297. float pre_log = r / _radius;
  298. if ( pre_log == 0 )
  299. {
  300. pre_log = 0.001f;
  301. }
  302. float l = logf(pre_log) * _lensEffect;
  303. float new_r = expf( l ) * _radius;
  304. if (vect.getLength() > 0)
  305. {
  306. vect.normalize();
  307. Vec2 new_vect = vect * new_r;
  308. v.z += (_concave ? -1.0f : 1.0f) * new_vect.getLength() * _lensEffect;
  309. }
  310. }
  311. setVertex(Vec2(i, j), v);
  312. }
  313. }
  314. _dirty = false;
  315. }
  316. }
  317. // implementation of Ripple3D
  318. Ripple3D* Ripple3D::create(float duration, const Size& gridSize, const Vec2& position, float radius, unsigned int waves, float amplitude)
  319. {
  320. Ripple3D *action = new (std::nothrow) Ripple3D();
  321. if (action)
  322. {
  323. if (action->initWithDuration(duration, gridSize, position, radius, waves, amplitude))
  324. {
  325. action->autorelease();
  326. }
  327. else
  328. {
  329. CC_SAFE_RELEASE_NULL(action);
  330. }
  331. }
  332. return action;
  333. }
  334. bool Ripple3D::initWithDuration(float duration, const Size& gridSize, const Vec2& position, float radius, unsigned int waves, float amplitude)
  335. {
  336. if (Grid3DAction::initWithDuration(duration, gridSize))
  337. {
  338. setPosition(position);
  339. _radius = radius;
  340. _waves = waves;
  341. _amplitude = amplitude;
  342. _amplitudeRate = 1.0f;
  343. return true;
  344. }
  345. return false;
  346. }
  347. void Ripple3D::setPosition(const Vec2& position)
  348. {
  349. _position = position;
  350. }
  351. Ripple3D* Ripple3D::clone() const
  352. {
  353. // no copy constructor
  354. auto a = new (std::nothrow) Ripple3D();
  355. a->initWithDuration(_duration, _gridSize, _position, _radius, _waves, _amplitude);
  356. a->autorelease();
  357. return a;
  358. }
  359. void Ripple3D::update(float time)
  360. {
  361. int i, j;
  362. for (i = 0; i < (_gridSize.width+1); ++i)
  363. {
  364. for (j = 0; j < (_gridSize.height+1); ++j)
  365. {
  366. Vec3 v = getOriginalVertex(Vec2(i, j));
  367. Vec2 vect = _position - Vec2(v.x,v.y);
  368. float r = vect.getLength();
  369. if (r < _radius)
  370. {
  371. r = _radius - r;
  372. float rate = powf(r / _radius, 2);
  373. v.z += (sinf( time*(float)M_PI * _waves * 2 + r * 0.1f) * _amplitude * _amplitudeRate * rate);
  374. }
  375. setVertex(Vec2(i, j), v);
  376. }
  377. }
  378. }
  379. // implementation of Shaky3D
  380. Shaky3D* Shaky3D::create(float duration, const Size& gridSize, int range, bool shakeZ)
  381. {
  382. Shaky3D *action = new (std::nothrow) Shaky3D();
  383. if (action)
  384. {
  385. if (action->initWithDuration(duration, gridSize, range, shakeZ))
  386. {
  387. action->autorelease();
  388. }
  389. else
  390. {
  391. CC_SAFE_RELEASE_NULL(action);
  392. }
  393. }
  394. return action;
  395. }
  396. bool Shaky3D::initWithDuration(float duration, const Size& gridSize, int range, bool shakeZ)
  397. {
  398. if (Grid3DAction::initWithDuration(duration, gridSize))
  399. {
  400. _randrange = range;
  401. _shakeZ = shakeZ;
  402. return true;
  403. }
  404. return false;
  405. }
  406. Shaky3D* Shaky3D::clone() const
  407. {
  408. // no copy constructor
  409. auto a = new (std::nothrow) Shaky3D();
  410. a->initWithDuration(_duration, _gridSize, _randrange, _shakeZ);
  411. a->autorelease();
  412. return a;
  413. }
  414. void Shaky3D::update(float /*time*/)
  415. {
  416. int i, j;
  417. for (i = 0; i < (_gridSize.width+1); ++i)
  418. {
  419. for (j = 0; j < (_gridSize.height+1); ++j)
  420. {
  421. Vec3 v = getOriginalVertex(Vec2(i ,j));
  422. v.x += (rand() % (_randrange*2)) - _randrange;
  423. v.y += (rand() % (_randrange*2)) - _randrange;
  424. if (_shakeZ)
  425. {
  426. v.z += (rand() % (_randrange*2)) - _randrange;
  427. }
  428. setVertex(Vec2(i, j), v);
  429. }
  430. }
  431. }
  432. // implementation of Liquid
  433. Liquid* Liquid::create(float duration, const Size& gridSize, unsigned int waves, float amplitude)
  434. {
  435. Liquid *action = new (std::nothrow) Liquid();
  436. if (action)
  437. {
  438. if (action->initWithDuration(duration, gridSize, waves, amplitude))
  439. {
  440. action->autorelease();
  441. }
  442. else
  443. {
  444. CC_SAFE_RELEASE_NULL(action);
  445. }
  446. }
  447. return action;
  448. }
  449. bool Liquid::initWithDuration(float duration, const Size& gridSize, unsigned int waves, float amplitude)
  450. {
  451. if (Grid3DAction::initWithDuration(duration, gridSize))
  452. {
  453. _waves = waves;
  454. _amplitude = amplitude;
  455. _amplitudeRate = 1.0f;
  456. return true;
  457. }
  458. return false;
  459. }
  460. Liquid* Liquid::clone() const
  461. {
  462. // no copy constructor
  463. auto a = new (std::nothrow) Liquid();
  464. a->initWithDuration(_duration, _gridSize, _waves, _amplitude);
  465. a->autorelease();
  466. return a;
  467. }
  468. void Liquid::update(float time)
  469. {
  470. int i, j;
  471. for (i = 1; i < _gridSize.width; ++i)
  472. {
  473. for (j = 1; j < _gridSize.height; ++j)
  474. {
  475. Vec3 v = getOriginalVertex(Vec2(i, j));
  476. v.x = (v.x + (sinf(time * (float)M_PI * _waves * 2 + v.x * .01f) * _amplitude * _amplitudeRate));
  477. v.y = (v.y + (sinf(time * (float)M_PI * _waves * 2 + v.y * .01f) * _amplitude * _amplitudeRate));
  478. setVertex(Vec2(i, j), v);
  479. }
  480. }
  481. }
  482. // implementation of Waves
  483. Waves* Waves::create(float duration, const Size& gridSize, unsigned int waves, float amplitude, bool horizontal, bool vertical)
  484. {
  485. Waves *action = new (std::nothrow) Waves();
  486. if (action)
  487. {
  488. if (action->initWithDuration(duration, gridSize, waves, amplitude, horizontal, vertical))
  489. {
  490. action->autorelease();
  491. }
  492. else
  493. {
  494. CC_SAFE_RELEASE_NULL(action);
  495. }
  496. }
  497. return action;
  498. }
  499. bool Waves::initWithDuration(float duration, const Size& gridSize, unsigned int waves, float amplitude, bool horizontal, bool vertical)
  500. {
  501. if (Grid3DAction::initWithDuration(duration, gridSize))
  502. {
  503. _waves = waves;
  504. _amplitude = amplitude;
  505. _amplitudeRate = 1.0f;
  506. _horizontal = horizontal;
  507. _vertical = vertical;
  508. return true;
  509. }
  510. return false;
  511. }
  512. Waves* Waves::clone() const
  513. {
  514. // no copy constructor
  515. auto a = new (std::nothrow) Waves();
  516. a->initWithDuration(_duration, _gridSize, _waves, _amplitude, _horizontal, _vertical);
  517. a->autorelease();
  518. return a;
  519. }
  520. void Waves::update(float time)
  521. {
  522. int i, j;
  523. for (i = 0; i < _gridSize.width + 1; ++i)
  524. {
  525. for (j = 0; j < _gridSize.height + 1; ++j)
  526. {
  527. Vec3 v = getOriginalVertex(Vec2(i, j));
  528. if (_vertical)
  529. {
  530. v.x = (v.x + (sinf(time * (float)M_PI * _waves * 2 + v.y * .01f) * _amplitude * _amplitudeRate));
  531. }
  532. if (_horizontal)
  533. {
  534. v.y = (v.y + (sinf(time * (float)M_PI * _waves * 2 + v.x * .01f) * _amplitude * _amplitudeRate));
  535. }
  536. setVertex(Vec2(i, j), v);
  537. }
  538. }
  539. }
  540. // implementation of Twirl
  541. Twirl* Twirl::create(float duration, const Size& gridSize, const Vec2& position, unsigned int twirls, float amplitude)
  542. {
  543. Twirl *action = new (std::nothrow) Twirl();
  544. if (action)
  545. {
  546. if (action->initWithDuration(duration, gridSize, position, twirls, amplitude))
  547. {
  548. action->autorelease();
  549. }
  550. else
  551. {
  552. CC_SAFE_RELEASE_NULL(action);
  553. }
  554. }
  555. return action;
  556. }
  557. bool Twirl::initWithDuration(float duration, const Size& gridSize, const Vec2& position, unsigned int twirls, float amplitude)
  558. {
  559. if (Grid3DAction::initWithDuration(duration, gridSize))
  560. {
  561. setPosition(position);
  562. _twirls = twirls;
  563. _amplitude = amplitude;
  564. _amplitudeRate = 1.0f;
  565. return true;
  566. }
  567. return false;
  568. }
  569. void Twirl::setPosition(const Vec2& position)
  570. {
  571. _position = position;
  572. }
  573. Twirl *Twirl::clone() const
  574. {
  575. // no copy constructor
  576. auto a = new (std::nothrow) Twirl();
  577. a->initWithDuration(_duration, _gridSize, _position, _twirls, _amplitude);
  578. a->autorelease();
  579. return a;
  580. }
  581. void Twirl::update(float time)
  582. {
  583. int i, j;
  584. Vec2 c = _position;
  585. for (i = 0; i < (_gridSize.width+1); ++i)
  586. {
  587. for (j = 0; j < (_gridSize.height+1); ++j)
  588. {
  589. Vec3 v = getOriginalVertex(Vec2(i ,j));
  590. Vec2 avg(i-(_gridSize.width/2.0f), j-(_gridSize.height/2.0f));
  591. float r = avg.getLength();
  592. float amp = 0.1f * _amplitude * _amplitudeRate;
  593. float a = r * cosf( (float)M_PI/2.0f + time * (float)M_PI * _twirls * 2 ) * amp;
  594. Vec2 d(
  595. sinf(a) * (v.y-c.y) + cosf(a) * (v.x-c.x),
  596. cosf(a) * (v.y-c.y) - sinf(a) * (v.x-c.x));
  597. v.x = c.x + d.x;
  598. v.y = c.y + d.y;
  599. setVertex(Vec2(i ,j), v);
  600. }
  601. }
  602. }
  603. NS_CC_END