CCActionCatmullRom.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. Copyright (c) 2008 Radu Gruian
  6. Copyright (c) 2011 Vit Valentin
  7. http://www.cocos2d-x.org
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. THE SOFTWARE.
  23. Orignal code by Radu Gruian: http://www.codeproject.com/Articles/30838/Overhauser-Catmull-Rom-Splines-for-Camera-Animatio.So
  24. Adapted to cocos2d-x by Vit Valentin
  25. Adapted from cocos2d-x to cocos2d-iphone by Ricardo Quesada
  26. ****************************************************************************/
  27. /**
  28. * Returns the Cardinal Spline position for a given set of control points, tension and time. <br />
  29. * CatmullRom Spline formula. <br />
  30. * s(-ttt + 2tt - t)P1 + s(-ttt + tt)P2 + (2ttt - 3tt + 1)P2 + s(ttt - 2tt + t)P3 + (-2ttt + 3tt)P3 + s(ttt - tt)P4
  31. *
  32. * @function
  33. * @param {cc.Point} p0
  34. * @param {cc.Point} p1
  35. * @param {cc.Point} p2
  36. * @param {cc.Point} p3
  37. * @param {Number} tension
  38. * @param {Number} t
  39. * @return {cc.Point}
  40. */
  41. cc.cardinalSplineAt = function (p0, p1, p2, p3, tension, t) {
  42. var t2 = t * t;
  43. var t3 = t2 * t;
  44. /*
  45. * Formula: s(-ttt + 2tt - t)P1 + s(-ttt + tt)P2 + (2ttt - 3tt + 1)P2 + s(ttt - 2tt + t)P3 + (-2ttt + 3tt)P3 + s(ttt - tt)P4
  46. */
  47. var s = (1 - tension) / 2;
  48. var b1 = s * ((-t3 + (2 * t2)) - t); // s(-t3 + 2 t2 - t)P1
  49. var b2 = s * (-t3 + t2) + (2 * t3 - 3 * t2 + 1); // s(-t3 + t2)P2 + (2 t3 - 3 t2 + 1)P2
  50. var b3 = s * (t3 - 2 * t2 + t) + (-2 * t3 + 3 * t2); // s(t3 - 2 t2 + t)P3 + (-2 t3 + 3 t2)P3
  51. var b4 = s * (t3 - t2); // s(t3 - t2)P4
  52. var x = (p0.x * b1 + p1.x * b2 + p2.x * b3 + p3.x * b4);
  53. var y = (p0.y * b1 + p1.y * b2 + p2.y * b3 + p3.y * b4);
  54. return cc.p(x, y);
  55. };
  56. /**
  57. * returns a new copy of the array reversed.
  58. *
  59. * @return {Array}
  60. */
  61. cc.reverseControlPoints = function (controlPoints) {
  62. var newArray = [];
  63. for (var i = controlPoints.length - 1; i >= 0; i--) {
  64. newArray.push(cc.p(controlPoints[i].x, controlPoints[i].y));
  65. }
  66. return newArray;
  67. };
  68. /**
  69. * returns a new clone of the controlPoints
  70. *
  71. * @param controlPoints
  72. * @returns {Array}
  73. */
  74. cc.cloneControlPoints = function (controlPoints) {
  75. var newArray = [];
  76. for (var i = 0; i < controlPoints.length; i++)
  77. newArray.push(cc.p(controlPoints[i].x, controlPoints[i].y));
  78. return newArray;
  79. };
  80. /**
  81. * returns a new clone of the controlPoints
  82. * @deprecated since v3.0 please use cc.cloneControlPoints() instead.
  83. * @param controlPoints
  84. * @returns {Array}
  85. */
  86. cc.copyControlPoints = cc.cloneControlPoints;
  87. /**
  88. * returns a point from the array
  89. *
  90. * @param {Array} controlPoints
  91. * @param {Number} pos
  92. * @return {Array}
  93. */
  94. cc.getControlPointAt = function (controlPoints, pos) {
  95. var p = Math.min(controlPoints.length - 1, Math.max(pos, 0));
  96. return controlPoints[p];
  97. };
  98. /**
  99. * reverse the current control point array inline, without generating a new one <br />
  100. *
  101. * @param controlPoints
  102. */
  103. cc.reverseControlPointsInline = function (controlPoints) {
  104. var len = controlPoints.length;
  105. var mid = 0 | (len / 2);
  106. for (var i = 0; i < mid; ++i) {
  107. var temp = controlPoints[i];
  108. controlPoints[i] = controlPoints[len - i - 1];
  109. controlPoints[len - i - 1] = temp;
  110. }
  111. };
  112. /**
  113. * Cardinal Spline path. {@link http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline}
  114. * Absolute coordinates.
  115. *
  116. * @class
  117. * @extends cc.ActionInterval
  118. * @param {Number} duration
  119. * @param {Array} points array of control points
  120. * @param {Number} tension
  121. *
  122. * @example
  123. * //create a cc.CardinalSplineTo
  124. * var action1 = cc.cardinalSplineTo(3, array, 0);
  125. */
  126. cc.CardinalSplineTo = cc.ActionInterval.extend(/** @lends cc.CardinalSplineTo# */{
  127. /** Array of control points */
  128. _points:null,
  129. _deltaT:0,
  130. _tension:0,
  131. _previousPosition:null,
  132. _accumulatedDiff:null,
  133. /**
  134. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  135. * Creates an action with a Cardinal Spline array of points and tension.
  136. * @param {Number} duration
  137. * @param {Array} points array of control points
  138. * @param {Number} tension
  139. */
  140. ctor: function (duration, points, tension) {
  141. cc.ActionInterval.prototype.ctor.call(this);
  142. this._points = [];
  143. tension !== undefined && this.initWithDuration(duration, points, tension);
  144. },
  145. /**
  146. * initializes the action with a duration and an array of points
  147. *
  148. * @param {Number} duration
  149. * @param {Array} points array of control points
  150. * @param {Number} tension
  151. *
  152. * @return {Boolean}
  153. */
  154. initWithDuration:function (duration, points, tension) {
  155. if(!points || points.length == 0)
  156. throw "Invalid configuration. It must at least have one control point";
  157. if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
  158. this.setPoints(points);
  159. this._tension = tension;
  160. return true;
  161. }
  162. return false;
  163. },
  164. /**
  165. * returns a new clone of the action
  166. *
  167. * @returns {cc.CardinalSplineTo}
  168. */
  169. clone:function () {
  170. var action = new cc.CardinalSplineTo();
  171. action.initWithDuration(this._duration, cc.copyControlPoints(this._points), this._tension);
  172. return action;
  173. },
  174. /**
  175. * called before the action start. It will also set the target.
  176. *
  177. * @param {cc.Node} target
  178. */
  179. startWithTarget:function (target) {
  180. cc.ActionInterval.prototype.startWithTarget.call(this, target);
  181. // Issue #1441 from cocos2d-iphone
  182. this._deltaT = 1 / (this._points.length - 1);
  183. this._previousPosition = cc.p(this.target.getPositionX(), this.target.getPositionY());
  184. this._accumulatedDiff = cc.p(0, 0);
  185. },
  186. /**
  187. * Called once per frame. Time is the number of seconds of a frame interval.
  188. *
  189. * @param {Number} dt
  190. */
  191. update:function (dt) {
  192. dt = this._computeEaseTime(dt);
  193. var p, lt;
  194. var ps = this._points;
  195. // eg.
  196. // p..p..p..p..p..p..p
  197. // 1..2..3..4..5..6..7
  198. // want p to be 1, 2, 3, 4, 5, 6
  199. if (dt == 1) {
  200. p = ps.length - 1;
  201. lt = 1;
  202. } else {
  203. var locDT = this._deltaT;
  204. p = 0 | (dt / locDT);
  205. lt = (dt - locDT * p) / locDT;
  206. }
  207. var newPos = cc.cardinalSplineAt(
  208. cc.getControlPointAt(ps, p - 1),
  209. cc.getControlPointAt(ps, p - 0),
  210. cc.getControlPointAt(ps, p + 1),
  211. cc.getControlPointAt(ps, p + 2),
  212. this._tension, lt);
  213. if (cc.ENABLE_STACKABLE_ACTIONS) {
  214. var tempX, tempY;
  215. tempX = this.target.getPositionX() - this._previousPosition.x;
  216. tempY = this.target.getPositionY() - this._previousPosition.y;
  217. if (tempX != 0 || tempY != 0) {
  218. var locAccDiff = this._accumulatedDiff;
  219. tempX = locAccDiff.x + tempX;
  220. tempY = locAccDiff.y + tempY;
  221. locAccDiff.x = tempX;
  222. locAccDiff.y = tempY;
  223. newPos.x += tempX;
  224. newPos.y += tempY;
  225. }
  226. }
  227. this.updatePosition(newPos);
  228. },
  229. /**
  230. * reverse a new cc.CardinalSplineTo. <br />
  231. * Along the track of movement in the opposite.
  232. *
  233. * @return {cc.CardinalSplineTo}
  234. */
  235. reverse:function () {
  236. var reversePoints = cc.reverseControlPoints(this._points);
  237. return cc.cardinalSplineTo(this._duration, reversePoints, this._tension);
  238. },
  239. /**
  240. * update position of target
  241. *
  242. * @param {cc.Point} newPos
  243. */
  244. updatePosition:function (newPos) {
  245. this.target.setPosition(newPos);
  246. this._previousPosition = newPos;
  247. },
  248. /**
  249. * Points getter
  250. *
  251. * @return {Array}
  252. */
  253. getPoints:function () {
  254. return this._points;
  255. },
  256. /**
  257. * Points setter
  258. *
  259. * @param {Array} points
  260. */
  261. setPoints:function (points) {
  262. this._points = points;
  263. }
  264. });
  265. /**
  266. * creates an action with a Cardinal Spline array of points and tension.
  267. *
  268. * @function
  269. * @param {Number} duration
  270. * @param {Array} points array of control points
  271. * @param {Number} tension
  272. * @return {cc.CardinalSplineTo}
  273. *
  274. * @example
  275. * //create a cc.CardinalSplineTo
  276. * var action1 = cc.cardinalSplineTo(3, array, 0);
  277. */
  278. cc.cardinalSplineTo = function (duration, points, tension) {
  279. return new cc.CardinalSplineTo(duration, points, tension);
  280. };
  281. /**
  282. * Please use cc.cardinalSplineTo instead. <br />
  283. * creates an action with a Cardinal Spline array of points and tension
  284. *
  285. * @function
  286. * @param {Number} duration
  287. * @param {Array} points array of control points
  288. * @param {Number} tension
  289. * @return {cc.CardinalSplineTo}
  290. * @static
  291. * @deprecated since v3.0 please use cc.cardinalSplineTo(duration, points, tension) instead.
  292. */
  293. cc.CardinalSplineTo.create = cc.cardinalSplineTo;
  294. /**
  295. * Cardinal Spline path. {@link http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline}
  296. * Relative coordinates.
  297. *
  298. * @class
  299. * @extends cc.CardinalSplineTo
  300. * @param {Number} duration
  301. * @param {Array} points
  302. * @param {Number} tension
  303. *
  304. * @example
  305. * //create a cc.CardinalSplineBy
  306. * var action1 = cc.cardinalSplineBy(3, array, 0);
  307. */
  308. cc.CardinalSplineBy = cc.CardinalSplineTo.extend(/** @lends cc.CardinalSplineBy# */{
  309. _startPosition:null,
  310. /**
  311. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  312. * creates an action with a Cardinal Spline array of points and tension.
  313. * @param {Number} duration
  314. * @param {Array} points
  315. * @param {Number} tension
  316. */
  317. ctor:function (duration, points, tension) {
  318. cc.CardinalSplineTo.prototype.ctor.call(this);
  319. this._startPosition = cc.p(0, 0);
  320. tension !== undefined && this.initWithDuration(duration, points, tension);
  321. },
  322. /**
  323. * called before the action start. It will also set the target.
  324. *
  325. * @param {cc.Node} target
  326. */
  327. startWithTarget:function (target) {
  328. cc.CardinalSplineTo.prototype.startWithTarget.call(this, target);
  329. this._startPosition.x = target.getPositionX();
  330. this._startPosition.y = target.getPositionY();
  331. },
  332. /**
  333. * reverse a new cc.CardinalSplineBy
  334. *
  335. * @return {cc.CardinalSplineBy}
  336. */
  337. reverse:function () {
  338. var copyConfig = this._points.slice();
  339. var current;
  340. //
  341. // convert "absolutes" to "diffs"
  342. //
  343. var p = copyConfig[0];
  344. for (var i = 1; i < copyConfig.length; ++i) {
  345. current = copyConfig[i];
  346. copyConfig[i] = cc.pSub(current, p);
  347. p = current;
  348. }
  349. // convert to "diffs" to "reverse absolute"
  350. var reverseArray = cc.reverseControlPoints(copyConfig);
  351. // 1st element (which should be 0,0) should be here too
  352. p = reverseArray[ reverseArray.length - 1 ];
  353. reverseArray.pop();
  354. p.x = -p.x;
  355. p.y = -p.y;
  356. reverseArray.unshift(p);
  357. for (var i = 1; i < reverseArray.length; ++i) {
  358. current = reverseArray[i];
  359. current.x = -current.x;
  360. current.y = -current.y;
  361. current.x += p.x;
  362. current.y += p.y;
  363. reverseArray[i] = current;
  364. p = current;
  365. }
  366. return cc.cardinalSplineBy(this._duration, reverseArray, this._tension);
  367. },
  368. /**
  369. * update position of target
  370. *
  371. * @param {cc.Point} newPos
  372. */
  373. updatePosition:function (newPos) {
  374. var pos = this._startPosition;
  375. var posX = newPos.x + pos.x;
  376. var posY = newPos.y + pos.y;
  377. this._previousPosition.x = posX;
  378. this._previousPosition.y = posY;
  379. this.target.setPosition(posX, posY);
  380. },
  381. /**
  382. * returns a new clone of the action
  383. *
  384. * @returns {cc.CardinalSplineBy}
  385. */
  386. clone:function () {
  387. var a = new cc.CardinalSplineBy();
  388. a.initWithDuration(this._duration, cc.copyControlPoints(this._points), this._tension);
  389. return a;
  390. }
  391. });
  392. /**
  393. * creates an action with a Cardinal Spline array of points and tension.
  394. *
  395. * @function
  396. * @param {Number} duration
  397. * @param {Array} points
  398. * @param {Number} tension
  399. *
  400. * @return {cc.CardinalSplineBy}
  401. */
  402. cc.cardinalSplineBy = function (duration, points, tension) {
  403. return new cc.CardinalSplineBy(duration, points, tension);
  404. };
  405. /**
  406. * Please use cc.cardinalSplineBy instead.
  407. * creates an action with a Cardinal Spline array of points and tension.
  408. * @function
  409. * @param {Number} duration
  410. * @param {Array} points
  411. * @param {Number} tension
  412. * @return {cc.CardinalSplineBy}
  413. * @static
  414. * @deprecated since v3.0 please use cc.cardinalSplineBy(duration, points, tension);
  415. */
  416. cc.CardinalSplineBy.create = cc.cardinalSplineBy;
  417. /**
  418. * An action that moves the target with a CatmullRom curve to a destination point.<br/>
  419. * A Catmull Rom is a Cardinal Spline with a tension of 0.5. <br/>
  420. * {@link http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Catmull.E2.80.93Rom_spline}
  421. * Absolute coordinates.
  422. *
  423. * @class
  424. * @extends cc.CardinalSplineTo
  425. * @param {Number} dt
  426. * @param {Array} points
  427. *
  428. * @example
  429. * var action1 = cc.catmullRomTo(3, array);
  430. */
  431. cc.CatmullRomTo = cc.CardinalSplineTo.extend(/** @lends cc.CatmullRomTo# */{
  432. /**
  433. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  434. * creates an action with a Cardinal Spline array of points and tension.
  435. * @param {Number} dt
  436. * @param {Array} points
  437. */
  438. ctor: function(dt, points) {
  439. points && this.initWithDuration(dt, points);
  440. },
  441. /**
  442. * Initializes the action with a duration and an array of points
  443. *
  444. * @param {Number} dt
  445. * @param {Array} points
  446. */
  447. initWithDuration:function (dt, points) {
  448. return cc.CardinalSplineTo.prototype.initWithDuration.call(this, dt, points, 0.5);
  449. },
  450. /**
  451. * returns a new clone of the action
  452. * @returns {cc.CatmullRomTo}
  453. */
  454. clone:function () {
  455. var action = new cc.CatmullRomTo();
  456. action.initWithDuration(this._duration, cc.copyControlPoints(this._points));
  457. return action;
  458. }
  459. });
  460. /**
  461. * creates an action with a Cardinal Spline array of points and tension.
  462. *
  463. * @function
  464. * @param {Number} dt
  465. * @param {Array} points
  466. * @return {cc.CatmullRomTo}
  467. *
  468. * @example
  469. * var action1 = cc.catmullRomTo(3, array);
  470. */
  471. cc.catmullRomTo = function (dt, points) {
  472. return new cc.CatmullRomTo(dt, points);
  473. };
  474. /**
  475. * Please use cc.catmullRomTo instead.
  476. * creates an action with a Cardinal Spline array of points and tension.
  477. *
  478. * @param {Number} dt
  479. * @param {Array} points
  480. * @return {cc.CatmullRomTo}
  481. * @static
  482. * @deprecated since v3.0 please use cc.catmullRomTo(dt, points) instead.
  483. */
  484. cc.CatmullRomTo.create = cc.catmullRomTo;
  485. /**
  486. * An action that moves the target with a CatmullRom curve by a certain distance. <br/>
  487. * A Catmull Rom is a Cardinal Spline with a tension of 0.5.<br/>
  488. * http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Catmull.E2.80.93Rom_spline
  489. * Relative coordinates.
  490. *
  491. * @class
  492. * @extends cc.CardinalSplineBy
  493. * @param {Number} dt
  494. * @param {Array} points
  495. *
  496. * @example
  497. * var action1 = cc.catmullRomBy(3, array);
  498. */
  499. cc.CatmullRomBy = cc.CardinalSplineBy.extend({
  500. /**
  501. * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
  502. * Creates an action with a Cardinal Spline array of points and tension.
  503. * @param {Number} dt
  504. * @param {Array} points
  505. */
  506. ctor: function(dt, points) {
  507. cc.CardinalSplineBy.prototype.ctor.call(this);
  508. points && this.initWithDuration(dt, points);
  509. },
  510. /**
  511. * initializes the action with a duration and an array of points
  512. *
  513. * @function
  514. * @param {Number} dt
  515. * @param {Array} points
  516. */
  517. initWithDuration:function (dt, points) {
  518. return cc.CardinalSplineTo.prototype.initWithDuration.call(this, dt, points, 0.5);
  519. },
  520. /**
  521. * returns a new clone of the action
  522. * @returns {cc.CatmullRomBy}
  523. */
  524. clone:function () {
  525. var action = new cc.CatmullRomBy();
  526. action.initWithDuration(this._duration, cc.copyControlPoints(this._points));
  527. return action;
  528. }
  529. });
  530. /**
  531. * Creates an action with a Cardinal Spline array of points and tension
  532. * @function
  533. * @param {Number} dt
  534. * @param {Array} points
  535. * @return {cc.CatmullRomBy}
  536. * @example
  537. * var action1 = cc.catmullRomBy(3, array);
  538. */
  539. cc.catmullRomBy = function (dt, points) {
  540. return new cc.CatmullRomBy(dt, points);
  541. };
  542. /**
  543. * Please use cc.catmullRomBy instead
  544. * Creates an action with a Cardinal Spline array of points and tension
  545. * @static
  546. * @deprecated since v3.0 please cc.catmullRomBy(dt, points) instead.
  547. */
  548. cc.CatmullRomBy.create = cc.catmullRomBy;