CCActionCatmullRom.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * Copyright (c) 2008 Radu Gruian
  3. * Copyright (c) 2011 Vit Valentin
  4. * Copyright (c) 2012 cocos2d-x.org
  5. * Copyright (c) 2013-2017 Chukong Technologies Inc.
  6. *
  7. *
  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. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. *
  27. * Original code by Radu Gruian: http://www.codeproject.com/Articles/30838/Overhauser-Catmull-Rom-Splines-for-Camera-Animatio.So
  28. *
  29. * Adapted to cocos2d-x by Vit Valentin
  30. *
  31. * Adapted from cocos2d-x to cocos2d-iphone by Ricardo Quesada
  32. */
  33. #ifndef __CCACTION_CATMULLROM_H__
  34. #define __CCACTION_CATMULLROM_H__
  35. #include <vector>
  36. #include "2d/CCActionInterval.h"
  37. #include "math/CCGeometry.h"
  38. NS_CC_BEGIN;
  39. class Node;
  40. /**
  41. * @addtogroup actions
  42. * @{
  43. */
  44. /** An Array that contain control points.
  45. * Used by CardinalSplineTo and (By) and CatmullRomTo (and By) actions.
  46. * @ingroup Actions
  47. * @js NA
  48. */
  49. class CC_DLL PointArray : public Ref, public Clonable
  50. {
  51. public:
  52. /** Creates and initializes a Points array with capacity.
  53. * @js NA
  54. * @param capacity The size of the array.
  55. */
  56. static PointArray* create(ssize_t capacity);
  57. /**
  58. * @js NA
  59. * @lua NA
  60. */
  61. virtual ~PointArray();
  62. /**
  63. * @js NA
  64. * @lua NA
  65. */
  66. PointArray();
  67. /** Initializes a Catmull Rom config with a capacity hint.
  68. *
  69. * @js NA
  70. * @param capacity The size of the array.
  71. * @return True.
  72. */
  73. bool initWithCapacity(ssize_t capacity);
  74. /** Appends a control point.
  75. *
  76. * @js NA
  77. * @param controlPoint A control point.
  78. */
  79. void addControlPoint(const Vec2& controlPoint);
  80. /** Inserts a controlPoint at index.
  81. *
  82. * @js NA
  83. * @param controlPoint A control point.
  84. * @param index Insert the point to array in index.
  85. */
  86. void insertControlPoint(Vec2 &controlPoint, ssize_t index);
  87. /** Replaces an existing controlPoint at index.
  88. *
  89. * @js NA
  90. * @param controlPoint A control point.
  91. * @param index Replace the point to array in index.
  92. */
  93. void replaceControlPoint(Vec2 &controlPoint, ssize_t index);
  94. /** Get the value of a controlPoint at a given index.
  95. *
  96. * @js NA
  97. * @param index Get the point in index.
  98. * @return A Vec2.
  99. */
  100. Vec2 getControlPointAtIndex(ssize_t index);
  101. /** Deletes a control point at a given index
  102. *
  103. * @js NA
  104. * @param index Remove the point in index.
  105. */
  106. void removeControlPointAtIndex(ssize_t index);
  107. /** Returns the number of objects of the control point array.
  108. *
  109. * @js NA
  110. * @return The number of objects of the control point array.
  111. */
  112. ssize_t count() const;
  113. /** Returns a new copy of the array reversed. User is responsible for releasing this copy.
  114. *
  115. * @js NA
  116. * @return A new copy of the array reversed.
  117. */
  118. PointArray* reverse() const;
  119. /** Reverse the current control point array inline, without generating a new one.
  120. * @js NA
  121. */
  122. void reverseInline();
  123. /**
  124. * @js NA
  125. * @lua NA
  126. */
  127. virtual PointArray* clone() const;
  128. /**
  129. * @js NA
  130. */
  131. const std::vector<Vec2*>* getControlPoints() const;
  132. /**
  133. * @js NA
  134. */
  135. void setControlPoints(std::vector<Vec2*> *controlPoints);
  136. private:
  137. /** Array that contains the control points. */
  138. std::vector<Vec2*> *_controlPoints;
  139. };
  140. /** @class CardinalSplineTo
  141. * Cardinal Spline path.
  142. * http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline
  143. * @ingroup Actions
  144. */
  145. class CC_DLL CardinalSplineTo : public ActionInterval
  146. {
  147. public:
  148. /** Creates an action with a Cardinal Spline array of points and tension.
  149. * @param duration In seconds.
  150. * @param points An PointArray.
  151. * @param tension Goodness of fit.
  152. * @code
  153. * When this function bound to js or lua,the input params are changed.
  154. * In js: var create(var t,var table)
  155. * In lua: local create(local t, local table)
  156. * @endcode
  157. */
  158. static CardinalSplineTo* create(float duration, PointArray* points, float tension);
  159. /**
  160. * @js NA
  161. * @lua NA
  162. */
  163. virtual ~CardinalSplineTo();
  164. /**
  165. * @js ctor
  166. * @lua NA
  167. */
  168. CardinalSplineTo();
  169. /**
  170. * Initializes the action with a duration and an array of points.
  171. *
  172. * @param duration In seconds.
  173. * @param points An PointArray.
  174. * @param tension Goodness of fit.
  175. */
  176. bool initWithDuration(float duration, PointArray* points, float tension);
  177. /** It will update the target position and change the _previousPosition to newPos
  178. *
  179. * @param newPos The new position.
  180. */
  181. virtual void updatePosition(Vec2 &newPos);
  182. /** Return a PointArray.
  183. *
  184. * @return A PointArray.
  185. */
  186. PointArray* getPoints() { return _points; }
  187. /**
  188. * @js NA
  189. * @lua NA
  190. */
  191. void setPoints(PointArray* points)
  192. {
  193. CC_SAFE_RETAIN(points);
  194. CC_SAFE_RELEASE(_points);
  195. _points = points;
  196. }
  197. // Overrides
  198. virtual CardinalSplineTo *clone() const override;
  199. virtual CardinalSplineTo* reverse() const override;
  200. virtual void startWithTarget(Node *target) override;
  201. /**
  202. * @param time In seconds.
  203. */
  204. virtual void update(float time) override;
  205. protected:
  206. /** Array of control points */
  207. PointArray *_points;
  208. float _deltaT;
  209. float _tension;
  210. Vec2 _previousPosition;
  211. Vec2 _accumulatedDiff;
  212. };
  213. /** @class CardinalSplineBy
  214. * Cardinal Spline path.
  215. * http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline
  216. * @ingroup Actions
  217. */
  218. class CC_DLL CardinalSplineBy : public CardinalSplineTo
  219. {
  220. public:
  221. /** Creates an action with a Cardinal Spline array of points and tension.
  222. * @code
  223. * When this function bound to js or lua,the input params are changed.
  224. * In js: var create(var t,var table).
  225. * In lua: local create(local t, local table).
  226. * @param duration In seconds.
  227. * @param point An PointArray.
  228. * @param tension Goodness of fit.
  229. * @endcode
  230. */
  231. static CardinalSplineBy* create(float duration, PointArray* points, float tension);
  232. CardinalSplineBy();
  233. // Overrides
  234. virtual void startWithTarget(Node *target) override;
  235. virtual void updatePosition(Vec2 &newPos) override;
  236. virtual CardinalSplineBy *clone() const override;
  237. virtual CardinalSplineBy* reverse() const override;
  238. protected:
  239. Vec2 _startPosition;
  240. };
  241. /** @class CatmullRomTo
  242. * An action that moves the target with a CatmullRom curve to a destination point.
  243. * A Catmull Rom is a Cardinal Spline with a tension of 0.5.
  244. * http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Catmull.E2.80.93Rom_spline
  245. * @ingroup Actions
  246. */
  247. class CC_DLL CatmullRomTo : public CardinalSplineTo
  248. {
  249. public:
  250. /** Creates an action with a Cardinal Spline array of points and tension.
  251. * @param dt In seconds.
  252. * @param points An PointArray.
  253. * @code
  254. * When this function bound to js or lua,the input params are changed.
  255. * In js: var create(var dt,var table).
  256. * In lua: local create(local dt, local table).
  257. * @endcode
  258. */
  259. static CatmullRomTo* create(float dt, PointArray* points);
  260. /**
  261. * Initializes the action with a duration and an array of points.
  262. *
  263. * @param dt In seconds.
  264. * @param points An PointArray.
  265. */
  266. bool initWithDuration(float dt, PointArray* points);
  267. // Override
  268. virtual CatmullRomTo *clone() const override;
  269. virtual CatmullRomTo *reverse() const override;
  270. };
  271. /** @class CatmullRomBy
  272. * An action that moves the target with a CatmullRom curve by a certain distance.
  273. * A Catmull Rom is a Cardinal Spline with a tension of 0.5.
  274. * http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Catmull.E2.80.93Rom_spline
  275. * @ingroup Actions
  276. */
  277. class CC_DLL CatmullRomBy : public CardinalSplineBy
  278. {
  279. public:
  280. /** Creates an action with a Cardinal Spline array of points and tension.
  281. * @param dt In seconds.
  282. * @param points An PointArray.
  283. * @code
  284. * When this function bound to js or lua,the input params are changed.
  285. * In js: var create(var dt,var table).
  286. * In lua: local create(local dt, local table).
  287. * @endcode
  288. */
  289. static CatmullRomBy* create(float dt, PointArray* points);
  290. /** Initializes the action with a duration and an array of points.
  291. *
  292. * @param dt In seconds.
  293. * @param points An PointArray.
  294. */
  295. bool initWithDuration(float dt, PointArray* points);
  296. // Override
  297. virtual CatmullRomBy *clone() const override;
  298. virtual CatmullRomBy *reverse() const override;
  299. };
  300. /** Returns the Cardinal Spline position for a given set of control points, tension and time */
  301. extern CC_DLL Vec2 ccCardinalSplineAt(Vec2 &p0, Vec2 &p1, Vec2 &p2, Vec2 &p3, float tension, float t);
  302. // end of actions group
  303. /// @}
  304. NS_CC_END;
  305. #endif // __CCACTION_CATMULLROM_H__