这个文件中主要实现了样条曲线动作和插值曲线动作。
1.点数组
样条曲线和插值曲线都需要传入控制点数组。通过,修改控制点,来控制样条曲线和插值曲线的轨迹。
代码:
/** An Array that contain control points.
* Used by CardinalSplineTo and (By) and CatmullRomTo (and By) actions.
* @ingroup Actions
* @js NA
*/
/// 一个包含控制点的数组
/// 在CardinalSplineTo/By,CatmullRomTo/By动作中用到
/// 继承自Ref和Clonable
class CC_DLL PointArray : public Ref, public Clonable
{
public:
/** Creates and initializes a Points array with capacity.
* @js NA
* @param capacity The size of the array.
*/
/// 通过容量产生并初始化一个点数组
static PointArray* create(ssize_t capacity);
/**
* @js NA
* @lua NA
*/
virtual ~PointArray();
/**
* @js NA
* @lua NA
*/
PointArray();
/** Initializes a Catmull Rom config with a capacity hint.
*
* @js NA
* @param capacity The size of the array.
* @return True.
*/
/// 初始化点数组
bool initWithCapacity(ssize_t capacity);
/** Appends a control point.
*
* @js NA
* @param controlPoint A control point.
*/
/// 添加控制点
void addControlPoint(Vec2 controlPoint);
/** Inserts a controlPoint at index.
*
* @js NA
* @param controlPoint A control point.
* @param index Insert the point to array in index.
*/
/// 在指定的位置插入控制点
void insertControlPoint(Vec2 &controlPoint, ssize_t index);
/** Replaces an existing controlPoint at index.
*
* @js NA
* @param controlPoint A control point.
* @param index Replace the point to array in index.
*/
/// 替换指定位置的控制点
void replaceControlPoint(Vec2 &controlPoint, ssize_t index);
/** Get the value of a controlPoint at a given index.
*
* @js NA
* @param index Get the point in index.
* @return A Vec2.
*/
/// 得到指定位置的控制点
Vec2 getControlPointAtIndex(ssize_t index);
/** Deletes a control point at a given index
*
* @js NA
* @param index Remove the point in index.
*/
/// 删除指定位置的控制点
void removeControlPointAtIndex(ssize_t index);
/** Returns the number of objects of the control point array.
*
* @js NA
* @return The number of objects of the control point array.
*/
/// 返回当前控制点的数量
ssize_t count() const;
/** Returns a new copy of the array reversed. User is responsible for releasing this copy.
*
* @js NA
* @return A new copy of the array reversed.
*/
/// 返回控制点数组的一个新副本,使用者负责释放这个副本
PointArray* reverse() const;
/** Reverse the current control point array inline, without generating a new one.
* @js NA
*/
/// 通过内联的方式将数组反向,不产生新的数组
void reverseInline();
/**
* @js NA
* @lua NA
*/
/// 克隆,产生一个新副本
virtual PointArray* clone() const;
/**
* @js NA
*/
/// 得到控制点
const std::vector<Vec2*>* getControlPoints() const;
/**
* @js NA
*/
/// 设置控制点
void setControlPoints(std::vector<Vec2*> *controlPoints);
private:
/** Array that contains the control points. */
/// 包含的控制点的数组
std::vector<Vec2*> *_controlPoints;
};
2.样条曲线动作
样条曲线有两个动作,分别是CardinalSplineTo和CardinalSplineBy
代码:
/** @class CardinalSplineTo
* Cardinal Spline path.
* http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline
* @ingroup Actions
*/
/// 样条曲线规矩动作
class CC_DLL CardinalSplineTo : public ActionInterval
{
public:
/** Creates an action with a Cardinal Spline array of points and tension.
* @param duration In seconds.
* @param points An PointArray.
* @param tension Goodness of fit.
* @code
* When this function bound to js or lua,the input params are changed.
* In js: var create(var t,var table)
* In lua: lcaol create(local t, local table)
* @endcode
*/
/// 通过一个点数组和拉力系数产生一个样条曲线动作
static CardinalSplineTo* create(float duration, PointArray* points, float tension);
/**
* @js NA
* @lua NA
*/
virtual ~CardinalSplineTo();
/**
* @js ctor
* @lua NA
*/
CardinalSplineTo();
/**
* Initializes the action with a duration and an array of points.
*
* @param duration In seconds.
* @param points An PointArray.
* @param tension Goodness of fit.
*/
/// 初始化动作
bool initWithDuration(float duration, PointArray* points, float tension);
/** It will update the target position and change the _previousPosition to newPos
*
* @param newPos The new position.
*/
/// 更新位置
virtual void updatePosition(Vec2 &newPos);
/** Return a PointArray.
*
* @return A PointArray.
*/
/// 得到于给新的点数组
inline PointArray* getPoints() { return _points; }
/**
* @js NA
* @lua NA
*/
/// 返回一个点数组
inline void setPoints(PointArray* points)
{
CC_SAFE_RETAIN(points);
CC_SAFE_RELEASE(_points);
_points = points;
}
// Overrides
/// 继承自Action的方法
virtual CardinalSplineTo *clone() const override;
virtual CardinalSplineTo* reverse() const override;
virtual void startWithTarget(Node *target) override;
/**
* @param time In seconds.
*/
virtual void update(float time) override;
protected:
/** Array of control points */
PointArray *_points;
float _deltaT;
float _tension;
Vec2 _previousPosition;
Vec2 _accumulatedDiff;
};
/** @class CardinalSplineBy
* Cardinal Spline path.
* http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Cardinal_spline
* @ingroup Actions
*/
class CC_DLL CardinalSplineBy : public CardinalSplineTo
{
public:
/** Creates an action with a Cardinal Spline array of points and tension.
* @code
* When this function bound to js or lua,the input params are changed.
* In js: var create(var t,var table).
* In lua: lcaol create(local t, local table).
* @param duration In seconds.
* @param point An PointArray.
* @param tension Goodness of fit.
* @endcode
*/
/// 根据弹力系数和点数组创建一个样条曲线动作
/// 当这个函数绑定到js和lua的时候,输入的参数将发生变化
static CardinalSplineBy* create(float duration, PointArray* points, float tension);
CardinalSplineBy();
// Overrides
/// 继承自Action的方法
virtual void startWithTarget(Node *target) override;
virtual void updatePosition(Vec2 &newPos) override;
virtual CardinalSplineBy *clone() const override;
virtual CardinalSplineBy* reverse() const override;
protected:
Vec2 _startPosition;
};
3.样条插值曲线动作
这个动作也有两个,分别是CatmullRomTo和CatmullRomBy
代码:
/** @class CatmullRomTo
* An action that moves the target with a CatmullRom curve to a destination point.
* A Catmull Rom is a Cardinal Spline with a tension of 0.5.
* http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Catmull.E2.80.93Rom_spline
* @ingroup Actions
*/
/// 样条插值轨迹动作
class CC_DLL CatmullRomTo : public CardinalSplineTo
{
public:
/** Creates an action with a Cardinal Spline array of points and tension.
* @param dt In seconds.
* @param points An PointArray.
* @code
* When this function bound to js or lua,the input params are changed.
* In js: var create(var dt,var table).
* In lua: lcaol create(local dt, local table).
* @endcode
*/
/// 创建一个样条插值轨迹动作,通过点数组和弹力系数
static CatmullRomTo* create(float dt, PointArray* points);
/**
* Initializes the action with a duration and an array of points.
*
* @param dt In seconds.
* @param points An PointArray.
*/
/// 初始化动作
bool initWithDuration(float dt, PointArray* points);
// Override
virtual CatmullRomTo *clone() const override;
virtual CatmullRomTo *reverse() const override;
};
/** @class CatmullRomBy
* An action that moves the target with a CatmullRom curve by a certain distance.
* A Catmull Rom is a Cardinal Spline with a tension of 0.5.
* http://en.wikipedia.org/wiki/Cubic_Hermite_spline#Catmull.E2.80.93Rom_spline
* @ingroup Actions
*/
class CC_DLL CatmullRomBy : public CardinalSplineBy
{
public:
/** Creates an action with a Cardinal Spline array of points and tension.
* @param dt In seconds.
* @param points An PointArray.
* @code
* When this function bound to js or lua,the input params are changed.
* In js: var create(var dt,var table).
* In lua: lcaol create(local dt, local table).
* @endcode
*/
/// 创建一个样条插值动作
static CatmullRomBy* create(float dt, PointArray* points);
/** Initializes the action with a duration and an array of points.
*
* @param dt In seconds.
* @param points An PointArray.
*/
/// 初始化
bool initWithDuration(float dt, PointArray* points);
// Override
/// 继承自Action的方法
virtual CatmullRomBy *clone() const override;
virtual CatmullRomBy *reverse() const override;
};
/** Returns the Cardinal Spline position for a given set of control points, tension and time */
/// 得到一个样条曲线的位置,通过一个点数组,弹力系数和时间
extern CC_DLL Vec2 ccCardinalSplineAt(Vec2 &p0, Vec2 &p1, Vec2 &p2, Vec2 &p3, float tension, float t);
// end of actions group
/// @}